]> gitweb.factorcode.org Git - factor.git/commitdiff
namespaces: make set-global/get-global foldable
authorJoe Groff <arcata@gmail.com>
Sun, 27 Nov 2011 04:20:55 +0000 (20:20 -0800)
committerJoe Groff <arcata@gmail.com>
Tue, 29 Nov 2011 02:25:27 +0000 (18:25 -0800)
Store the globals hashtable as an array of boxes so that the key-to-reference mapping is constant. Use a singleton and an unfoldable "box-at" word so that get-global and set-global optimize to direct operations on the associated box when the variable name is a compile-time constant. Fixes #200.

basis/bootstrap/image/image.factor
core/namespaces/namespaces-tests.factor
core/namespaces/namespaces.factor

index dc8343e6fae6c3177175ff2c504623ee8f157457..f162cd71fe9f3eceecbf034d0bca7adc166b64ee 100755 (executable)
@@ -503,11 +503,11 @@ M: quotation '
     {
         dictionary source-files builtins
         update-map implementors-map
-    } [ [ bootstrap-word ] [ get ] bi ] H{ } map>assoc
+    } [ [ bootstrap-word ] [ get 1array ] bi ] H{ } map>assoc
     {
         class<=-cache class-not-cache classes-intersect-cache
         class-and-cache class-or-cache next-method-quot-cache
-    } [ H{ } clone ] H{ } map>assoc assoc-union
+    } [ H{ } clone 1array ] H{ } map>assoc assoc-union
     bootstrap-global set ;
 
 : emit-jit-data ( -- )
index 1397028f24aad7989bafea5a8e8a93b87d50e538..12ca3f6a2021b66e98a693908b28bebc955e3930 100644 (file)
@@ -1,4 +1,5 @@
-USING: kernel namespaces tools.test words ;
+USING: assocs compiler.tree.debugger kernel namespaces
+tools.test words ;
 IN: namespaces.tests
 
 H{ } clone "test-namespace" set
@@ -35,3 +36,5 @@ SYMBOL: toggle-test
 [ t ] [ toggle-test [ on ] [ get ] bi ] unit-test
 [ f ] [ toggle-test [ off ] [ get ] bi ] unit-test
 
+[ t ] [ [ test-initialize get-global ] { at* set-at } inlined? ] unit-test
+[ t ] [ [ test-initialize set-global ] { at* set-at } inlined? ] unit-test
index 6e2256806e4394cb4a9b5c907ded9236d388d2aa..dc2b34a21b9ec143ac2055b5b8325576c0d3abf8 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2003, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel vectors sequences hashtables
+USING: kernel vectors sequences sequences.private hashtables
 arrays kernel.private math strings assocs ;
 IN: namespaces
 
@@ -11,13 +11,37 @@ IN: namespaces
 : >n ( namespace -- ) namestack* push ;
 : ndrop ( -- ) namestack* pop* ;
 
+SINGLETON: +globals+
+
+: get-global-hashtable ( -- table )
+    OBJ-GLOBAL special-object { hashtable } declare ; inline
+
+: box-at ( key -- box )
+    get-global-hashtable
+    2dup at [ 2nip ] [ [ f 1array ] 2dip [ set-at ] 2curry keep ] if* ; foldable
+
+: box> ( box -- value )
+    0 swap nth-unsafe ; inline
+
+: >box ( value box -- )
+    0 swap set-nth-unsafe ; inline
+
+M: +globals+ at*
+    drop box-at box> dup ; inline
+
+M: +globals+ set-at
+    drop box-at >box ; inline
+
+M: +globals+ delete-at
+    drop box-at f swap >box ; inline
+
 PRIVATE>
 
 : namespace ( -- namespace ) namestack* last ; inline
 : namestack ( -- namestack ) namestack* clone ;
 : set-namestack ( namestack -- )
     >vector CONTEXT-OBJ-NAMESTACK set-context-object ;
-: global ( -- g ) OBJ-GLOBAL special-object { hashtable } declare ; inline
+: global ( -- g ) +globals+ ; inline
 : init-namespaces ( -- ) global 1array set-namestack ;
 : get ( variable -- value ) namestack* assoc-stack ; inline
 : set ( value variable -- ) namespace set-at ;