help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] Patch: speed up adding to a hash


From: Derek Zhou
Subject: [Help-smalltalk] Patch: speed up adding to a hash
Date: Sat, 31 Jan 2009 23:49:40 -0800
User-agent: KMail/1.9.9

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 [
  




reply via email to

[Prev in Thread] Current Thread [Next in Thread]