Try to optimize generic dispatch to speed up + on fixnums, nth on arrays for example

db4
Slava Pestov 2008-11-13 03:51:04 -06:00
parent cfa82cb474
commit ef6206d4bb
2 changed files with 17 additions and 12 deletions

View File

@ -3,7 +3,7 @@
USING: arrays generic hashtables kernel kernel.private math
namespaces make sequences words quotations layouts combinators
sequences.private classes classes.builtin classes.algebra
definitions math.order ;
definitions math.order math.private ;
IN: generic.math
PREDICATE: math-class < class
@ -62,13 +62,17 @@ ERROR: no-math-method left right generic ;
2drop object-method
] if ;
SYMBOL: picker
: math-vtable ( picker quot -- quot )
[
>r
, \ tag ,
num-tags get [ bootstrap-type>class ]
r> compose map ,
\ dispatch ,
swap picker set
picker get , [ tag 0 eq? ] %
num-tags get swap [ bootstrap-type>class ] prepose map
unclip ,
[
picker get , [ tag 1 fixnum-fast ] % , \ dispatch ,
] [ ] make , \ if ,
] [ ] make ; inline
TUPLE: math-combination ;
@ -85,8 +89,7 @@ M: math-combination perform-combination
] [
over object-method
] if nip
] math-vtable nip
define ;
] math-vtable nip define ;
PREDICATE: math-generic < generic ( word -- ? )
"combination" word-prop math-combination? ;

View File

@ -22,13 +22,14 @@ C: <lo-tag-dispatch-engine> lo-tag-dispatch-engine
"type" word-prop
] if ;
: sort-tags ( assoc -- alist ) >alist sort-keys reverse ;
M: lo-tag-dispatch-engine engine>quot
methods>> engines>quots*
[ >r lo-tag-number r> ] assoc-map
[
picker % [ tag ] % [
>alist sort-keys reverse
linear-dispatch-quot
sort-tags linear-dispatch-quot
] [
num-tags get direct-dispatch-quot
] if-small? %
@ -51,10 +52,11 @@ C: <hi-tag-dispatch-engine> hi-tag-dispatch-engine
\ hi-tag def>> ;
M: hi-tag-dispatch-engine engine>quot
methods>> engines>quots* [ >r hi-tag-number r> ] assoc-map
methods>> engines>quots*
[ >r hi-tag-number r> ] assoc-map
[
picker % hi-tag-quot % [
linear-dispatch-quot
sort-tags linear-dispatch-quot
] [
num-tags get , \ fixnum-fast ,
[ >r num-tags get - r> ] assoc-map