Compiler specialization hints
Factor handbook > The implementation > Optimizing compiler

Prev:Compiler errors
Next:Calling the optimizing compiler

Specialization hints help the compiler generate efficient code.

Specialization hints can help words which call a lot of generic words on the same object - perhaps in a loop - and in most cases, it is anticipated that this object is of a certain class, or even eq? to some literal. Using specialization hints, the compiler can be instructed to compile a branch at the beginning of the word; if the branch is taken, the input object has the assumed class or value, and inlining of generic methods can take place.

Specialization hints are not declarations; if the inputs do not match what is specified, the word will still run, possibly slower if the compiled code cannot inline methods because of insufficient static type information.

In some cases, specialization will not help at all, and can make generated code slower from the increase in code size. The compiler is capable of inferring enough static type information to generate efficient code in many cases without explicit help from the programmer. Specializers should be used as a last resort, after profiling shows that a critical loop makes a lot of repeated calls to generic words which dispatch on the same class.

Type hints are declared with a parsing word:

The specialized version of a word which will be compiled by the compiler can be inspected:
specialized-def ( word -- quot )