]> gitweb.factorcode.org Git - factor.git/blob - core/optimizer/specializers/specializers-docs.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / core / optimizer / specializers / specializers-docs.factor
1 IN: optimizer.specializers\r
2 USING: help.markup help.syntax sequences words quotations ;\r
3 \r
4 ARTICLE: "specializers" "Word specializers"\r
5 "The optimizer can be passed hints as to the classes of parameters a word is expected to be called with. The optimizer will then generate multiple versions of word when compiling, specialized to each class."\r
6 $nl\r
7 "Specialization hints are stored in the " { $snippet "\"specializer\"" } " word property. The value of this property is either a sequence of classes, or a sequence of sequences of classes. Each element in the sequence (or the sequence itself, in the former case) is a specialization hint."\r
8 $nl\r
9 "Specialization can help in the case where a word calls 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. 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, and inlining of generic methods can take place."\r
10 $nl\r
11 "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."\r
12 $nl\r
13 "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."\r
14 $nl\r
15 "For example, the " { $link append } " word has a specializer for the very common case where two strings or two arrays are appended:"\r
16 { $code\r
17 "\\ append"\r
18 "{ { string string } { array array } }"\r
19 "\"specializer\" set-word-prop"\r
20 }\r
21 "The specialized version of a word which will be compiled by the compiler can be inspected:"\r
22 { $subsection specialized-def } ;\r
23 \r
24 HELP: specialized-def\r
25 { $values { "word" word } { "quot" quotation } }\r
26 { $description "Outputs the definition of a word after it has been split into specialized branches. This is the definition which will actually be compiled by the compiler." } ;\r