classes.union: unions of built-in types now have more efficient predicates. Fixes #292

db4
Slava Pestov 2011-10-29 17:09:56 -07:00
parent e5a308d56a
commit c14f0ef698
2 changed files with 59 additions and 17 deletions

View File

@ -1,9 +1,10 @@
USING: alien arrays definitions generic assocs hashtables io USING: accessors alien arrays definitions generic assocs
kernel math namespaces parser prettyprint sequences strings hashtables io kernel math namespaces parser prettyprint
tools.test vectors words quotations classes sequences strings tools.test vectors words quotations classes
classes.private classes.union classes.mixin classes.predicate classes.private classes.union classes.mixin classes.predicate
classes.algebra source-files compiler.units kernel.private classes.algebra classes.union.private source-files
sorting vocabs io.streams.string eval see ; compiler.units kernel.private sorting vocabs io.streams.string
eval see math.private ;
IN: classes.union.tests IN: classes.union.tests
! DEFER: bah ! DEFER: bah
@ -49,6 +50,8 @@ UNION: empty-union-2 ;
M: empty-union-2 empty-union-test ; M: empty-union-2 empty-union-test ;
[ [ drop f ] ] [ \ empty-union-1? def>> ] unit-test
! Redefining a class didn't update containing unions ! Redefining a class didn't update containing unions
UNION: redefine-bug-1 fixnum ; UNION: redefine-bug-1 fixnum ;
@ -90,3 +93,17 @@ M: a-union test-generic ;
[ ] [ [ \ a-tuple forget-class ] with-compilation-unit ] unit-test [ ] [ [ \ a-tuple forget-class ] with-compilation-unit ] unit-test
[ t ] [ \ test-generic "methods" word-prop assoc-empty? ] unit-test [ t ] [ \ test-generic "methods" word-prop assoc-empty? ] unit-test
! Fast union predicates
[ t ] [ integer union-of-builtins? ] unit-test
[ t ] [ \ integer? def>> \ fixnum-bitand swap member? ] unit-test
[ ] [ "IN: classes.union.tests USE: math UNION: fast-union-1 fixnum ; UNION: fast-union-2 fast-union-1 bignum ;" eval( -- ) ] unit-test
[ t ] [ "fast-union-2?" "classes.union.tests" lookup def>> \ fixnum-bitand swap member? ] unit-test
[ ] [ "IN: classes.union.tests USE: vectors UNION: fast-union-1 vector ;" eval( -- ) ] unit-test
[ f ] [ "fast-union-2?" "classes.union.tests" lookup def>> \ fixnum-bitand swap member? ] unit-test

View File

@ -1,8 +1,9 @@
! Copyright (C) 2004, 2010 Slava Pestov. ! Copyright (C) 2004, 2011 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: words sequences kernel assocs combinators classes USING: words sequences kernel assocs combinators classes
classes.private classes.algebra classes.algebra.private classes.private classes.algebra classes.algebra.private
namespaces arrays math quotations definitions ; classes.builtin kernel.private math.private namespaces arrays
math quotations definitions ;
IN: classes.union IN: classes.union
PREDICATE: union-class < class PREDICATE: union-class < class
@ -10,18 +11,42 @@ PREDICATE: union-class < class
<PRIVATE <PRIVATE
: union-predicate-quot ( members -- quot ) GENERIC: union-of-builtins? ( class -- ? )
[
[ drop f ] M: builtin-class union-of-builtins? drop t ;
] [
unclip "predicate" word-prop swap [ M: union-class union-of-builtins?
"predicate" word-prop [ dup ] prepend members [ union-of-builtins? ] all? ;
[ drop t ]
] { } map>assoc alist>quot M: class union-of-builtins?
] if-empty ; drop f ;
: fast-union-mask ( class -- n )
[ 0 ] dip flatten-class
[ drop class>type 2^ bitor ] assoc-each ;
: empty-union-predicate-quot ( class -- quot )
drop [ drop f ] ;
: fast-union-predicate-quot ( class -- quot )
fast-union-mask 1quotation
[ tag 1 swap fixnum-shift-fast ]
[ fixnum-bitand 0 eq? not ]
surround ;
: slow-union-predicate-quot ( class -- quot )
members [ "predicate" word-prop ] map unclip swap
[ [ dup ] prepend [ drop t ] ] { } map>assoc alist>quot ;
: union-predicate-quot ( class -- quot )
{
{ [ dup members empty? ] [ empty-union-predicate-quot ] }
{ [ dup union-of-builtins? ] [ fast-union-predicate-quot ] }
[ slow-union-predicate-quot ]
} cond ;
: define-union-predicate ( class -- ) : define-union-predicate ( class -- )
dup members union-predicate-quot define-predicate ; dup union-predicate-quot define-predicate ;
M: union-class update-class define-union-predicate ; M: union-class update-class define-union-predicate ;