]> gitweb.factorcode.org Git - factor.git/commitdiff
classes.union: speed up instance? on unions of tuple-classes.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 24 Sep 2020 02:32:15 +0000 (19:32 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 24 Sep 2020 02:32:15 +0000 (19:32 -0700)
core/classes/tuple/tuple.factor
core/classes/union/union.factor

index ba0b9c5704d32b6d0460e3a86da39b388aa45ed7..8cc87a0306e941979aefa892e0723d108a3e9556 100644 (file)
@@ -1,11 +1,18 @@
 ! Copyright (C) 2005, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
+IN: classes.tuple
+! for classes.union mutual dependency
+DEFER: tuple-class?
+<PRIVATE
+DEFER: echelon-of
+DEFER: layout-of
+DEFER: layout-class-offset
+PRIVATE>
 USING: accessors arrays assocs classes classes.algebra
 classes.algebra.private classes.builtin classes.private
 combinators definitions effects generic kernel kernel.private
 make math math.private memory namespaces quotations
 sequences sequences.private slots slots.private strings words ;
-IN: classes.tuple
 
 <PRIVATE
 PRIMITIVE: <tuple> ( layout -- tuple )
index 2482a72149ac0c382de61cfa863f2290acbd5f98..d40f3a0e13f6ec07823ab0fb83e067d3454ef294 100644 (file)
@@ -2,8 +2,9 @@
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors assocs classes classes.algebra
 classes.algebra.private classes.builtin classes.private
-combinators definitions kernel kernel.private math math.private
-quotations sequences sets sorting words ;
+classes.tuple classes.tuple.private combinators definitions
+kernel kernel.private math math.private quotations sequences
+slots.private sorting words ;
 IN: classes.union
 
 PREDICATE: union-class < class
@@ -24,32 +25,66 @@ M: class union-of-builtins?
 : empty-union-predicate-quot ( class -- quot )
     drop [ drop f ] ;
 
-: fast-union-mask ( class/builtin-classes -- n )
-    dup sequence? [ flatten-class ] unless
-    0 [ class>type 2^ bitor ] reduce ;
+: flatten-builtins ( class/builtin-classes -- seq )
+    dup sequence? [
+        [ flatten-class ] map concat
+    ] [
+        flatten-class
+    ] if ;
 
-: fast-union-predicate-quot ( class/builtin-classes -- quot )
-    fast-union-mask 1quotation
-    [ tag 1 swap fixnum-shift-fast ]
-    [ fixnum-bitand 0 eq? not ]
-    surround ;
+: builtin-union-mask ( builtin-classes -- n )
+    0 [ class>type 2^ bitor ] reduce ;
 
-: slow-union-predicate-quot ( class -- quot )
-    class-members
-    dup [ builtin-class? ] count 1 > [
-        [ builtin-class? ] partition
-        [ predicate-def ] map swap
-        [ fast-union-predicate-quot suffix ] unless-empty
+: builtin-union-predicate-quot ( class/builtin-classes -- quot )
+    flatten-builtins dup length 1 = [
+        first class>type [ eq? ] curry [ tag ] prepose
     ] [
-        [ predicate-def ] map
-    ] if unclip swap
+        builtin-union-mask 1quotation
+        [ tag 1 swap fixnum-shift-fast ]
+        [ fixnum-bitand 0 eq? not ]
+        surround
+    ] if ;
+
+: predicate-quot ( predicates -- quot )
+    unclip swap
     [ [ dup ] prepend [ drop t ] ] { } map>assoc alist>quot ;
 
+! this replicates logic in classes.tuple, keep in sync
+: tuple-union-predicate-quot/1 ( tuple-classes -- quot )
+    [ [ eq? ] curry ] map predicate-quot
+    [ 7 slot ] prepose ;
+
+: tuple-union-predicate-quot/n ( echelon tuple-classes -- quot )
+    [ layout-class-offset ] dip
+    [ [ eq? ] curry ] map predicate-quot
+    over [ slot ] curry prepose [ drop f ] [ if ] 2curry
+    swap [ fixnum>= ] curry [ dup 1 slot ] prepose prepose ;
+
+: tuple-union-predicate-quot ( tuple-classes -- quot )
+    [ echelon-of 1 = ] partition
+    [ [ f ] [ tuple-union-predicate-quot/1 ] if-empty ] dip
+    [ echelon-of ] collect-by sort-keys
+    [ tuple-union-predicate-quot/n ] { } assoc>map
+    swap [ suffix ] when* predicate-quot
+    [ layout-of ] prepose [ drop f ] [ if ] 2curry
+    [ dup tuple? ] prepose ;
+
+: full-union-predicate-quot ( class -- quot )
+    class-members
+    [ union-of-builtins? ] partition
+    [ [ f ] [ builtin-union-predicate-quot ] if-empty ] dip
+    [ tuple-class? ] partition
+    [ [ f ] [ tuple-union-predicate-quot ] if-empty ] dip
+    [ predicate-def ] map
+    swap [ suffix ] when*
+    swap [ suffix ] when*
+    predicate-quot ;
+
 : union-predicate-quot ( class -- quot )
     {
         { [ dup class-members empty? ] [ empty-union-predicate-quot ] }
-        { [ dup union-of-builtins? ] [ fast-union-predicate-quot ] }
-        [ slow-union-predicate-quot ]
+        { [ dup union-of-builtins? ] [ builtin-union-predicate-quot ] }
+        [ full-union-predicate-quot ]
     } cond ;
 
 : define-union-predicate ( class -- )