]> gitweb.factorcode.org Git - factor.git/commitdiff
hashtables: slightly faster associate and collect-pairs.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 7 Feb 2014 06:39:43 +0000 (22:39 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 7 Feb 2014 06:39:43 +0000 (22:39 -0800)
core/hashtables/hashtables.factor

index 0b6590d131b0bb75ebfcf8c016a0f55604c1510a..648f3da537a62152a5593fc9293f98a4184fb2f1 100644 (file)
@@ -97,7 +97,7 @@ TUPLE: hashtable
     [ count>> 3 fixnum*fast ]
     [ array>> length>> ] bi fixnum>= ; inline
 
-: each-pair ( array quot: ( key value -- ) -- )
+: each-pair ( ... array quot: ( ... key value -- ... ) -- ... )
     [
         [ length 2/ ] keep [
             [ 1 fixnum-shift-fast ] dip [ array-nth ] 2keep
@@ -145,20 +145,16 @@ M: hashtable set-at
     dup ?grow-hash (set-at) ;
 
 : associate ( value key -- hash )
-    1 <hashtable> [ set-at ] keep ; inline
+    [ 1 0 ] 2dip 1 <hash-array>
+    [ 2dup hash@ set-nth-pair ] keep
+    hashtable boa ; inline
 
 <PRIVATE
 
-: push-unsafe ( elt seq -- )
-    [ length ] keep
-    [ underlying>> set-array-nth ]
-    [ [ 1 fixnum+fast { array-capacity } declare ] dip length<< ]
-    2bi ; inline
-
 : collect-pairs ( hash quot: ( key value -- elt ) -- seq )
-    [ [ array>> ] [ assoc-size <vector> ] bi ] dip swap [
-        [ push-unsafe ] curry compose each-pair
-    ] keep { } like ; inline
+    [ [ array>> 0 swap ] [ assoc-size f <array> ] bi ] dip swap [
+        [ [ over ] dip set-nth-unsafe 1 + ] curry compose each-pair
+    ] keep nip ; inline
 
 PRIVATE>