Patch: speed up adding to a hash

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Patch: speed up adding to a hash

Derek Zhou
Paolo and all:
This patch reduces a "full" block to a "copying" block in "add:" method of HashedCollection, thus speed up the execution. One of my program speeds up by 20%. The down side is that it change a private API a little but since it is private no one outside kernel should use it anyhow.
Diffed against 3.0.5
Derek

--- orig/kernel/WeakObjects.st
+++ mod/kernel/WeakObjects.st
@@ -227,8 +227,8 @@
  | index |
  index := self findIndex: anObject
     ifAbsent:
- [:index |
- self incrementTally ifTrue: [self findIndex: anObject] ifFalse: [index]].
+ [:index :obj |
+ self incrementTally ifTrue: [self findIndex: obj] ifFalse: [index]].
  self primAt: index put: (self newAssociation: anObject).
  ^anObject
     ]
--- orig/kernel/HashedColl.st
+++ mod/kernel/HashedColl.st
@@ -90,8 +90,8 @@
  newObject isNil ifTrue: [^newObject].
  index := self findIndex: newObject
     ifAbsent:
- [:index |
- self incrementTally ifTrue: [self findIndex: newObject] ifFalse: [index]].
+ [:index :obj |
+ self incrementTally ifTrue: [self findIndex: obj] ifFalse: [index]].
  self primAt: index put: newObject.
  ^newObject
     ]
@@ -348,13 +348,13 @@
 
     findIndex: anObject ifAbsent: aBlock [
  "Finds the given object in the set and returns its index.  If the set
- doesn't contain the object, aBlock is evaluated passing the index,
- and the result of evaluating aBlock is returned."
+ doesn't contain the object, aBlock is evaluated passing the index and
+         the object, the result of evaluating aBlock is returned."
 
  <category: 'private methods'>
  | index |
  index := self findIndex: anObject.
- ^(self primAt: index) isNil ifTrue: [aBlock value: index] ifFalse: [index]
+ ^(self primAt: index) isNil ifTrue: [aBlock value: index value: anObject] ifFalse: [index]
     ]
 
     findIndexOrNil: anObject [
 


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Patch: speed up adding to a hash

Paolo Bonzini-2
Derek Zhou wrote:
> Paolo and all:
> This patch reduces a "full" block to a "copying" block in "add:"

The idea is great, though probably there are other places to adjust
(LookupTable, IdentityDictionary).  I'll take a look.  Using #cull:cull:
in 3.1 is a way to avoid breaking places still using a 1-argument block.

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Patch: speed up adding to a hash

Paolo Bonzini-2
In reply to this post by Derek Zhou
Derek Zhou wrote:
> Paolo and all:
> This patch reduces a "full" block to a "copying" block

I was overcautious and the patch was correct.  I inlined
#findIndex:ifAbsent: and eliminated it altogether.  Thanks!

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk