Factor Language Blog

Adding a new compiler optimization

Saturday, September 16, 2006

I noticed that after inlining, the following type of code was coming up in a few words:
dup array? [ dup array? [ ... ] [ ... ] if ] [ ... ] if
That is, a value being tested against a type twice. Implementing a compiler optimization to deal with this was easy:

: known-type? ( node -- ? )
    0 node-class# types length 1 number= ;

: fold-known-type ( node -- node )
    dup 0 node-class# types first 1array inline-literals ;

\ type [
    { [ dup known-type? ] [ fold-known-type ] }
] define-optimizers