Consider the Shen definition in figure 1.
The corresponding KLambda is:
This code is not optimal; several tests like (cons? V1925) are potentially repeated in the course of evaluation. The problem arises from the overlapping patterns in the orginal definition which generate repeated tests. TBoS 3rd edition remarks (p 180).
The motivation of the Griffon compiler is to factorise these repeated tests in the object code by replacing code sections with GOTO calls.
The Griffon depends for its optimisations on two operations: inlining and expansion, and the degree to which both operations are carried out is determined by the user. The SP top level contains two entries labelled inline and expand which are both set to 0 (the default value). At these settings the Griffon is not activated. Changing the values in these entries to a positive integer invokes the Griffon. We will explain these two operations.
Positive values for expand
or for inline cause Shen to search for
overlapping patterns in pattern matching and to expand
code in search of optimisations. These optimisations may
result in the generation of GOTO calls.
Expansion causes Shen to trade (if (and p q) r s) for (if p (if q r s) s) to a depth equal to the expand setting. Both inlining and expansion create optimisation opportunities at the expense of repeating code.
A good way of appreciating the action of the Griffin is to observe the object code produced from the definition in figure 1 at different compiler settings. First inline = expansion = 0.
Second inline = expansion = 1
Finally inline = expansion = 10.
Figure 2 shows the timings on the code for 108 iterations of (f [a b c]).
There is no upper limit on the value for inline and expand, but higher settings can cause exponential runaways during compilation and out of memory messages. Generally it is best to start with lower settings and observe the performance changes before increasing the values for these settings.