Run-time algorithm specialization

In computer science, run-time algorithm specialization is a methodology for creating efficient algorithms for costly computation tasks of certain kinds. The methodology originates in the field of automated theorem proving and, more specifically, in the Vampire theorem prover project.

The idea is inspired by the use of partial evaluation in optimising program translation. Many core operations in theorem provers exhibit the following pattern. Suppose that we need to execute some algorithm 𝑎𝑙𝑔(A,B) in a situation where a value of A is fixed for potentially many different values of B. In order to do this efficiently, we can try to find a specialization of 𝑎𝑙𝑔 for every fixed A, i.e., such an algorithm 𝑎𝑙𝑔A, that executing 𝑎𝑙𝑔A(B) is equivalent to executing 𝑎𝑙𝑔(A,B).

The specialized algorithm may be more efficient than the generic one, since it can exploit some particular properties of the fixed value A. Typically, 𝑎𝑙𝑔A(B) can avoid some operations that 𝑎𝑙𝑔(A,B) would have to perform, if they are known to be redundant for this particular parameter A. In particular, we can often identify some tests that are true or false for A, unroll loops and recursion, etc.

Difference from partial evaluation

The key difference between run-time specialization and partial evaluation is that the values of A on which 𝑎𝑙𝑔 is specialised are not known statically, so the specialization takes place at run-time.

There is also an important technical difference. Partial evaluation is applied to algorithms explicitly represented as codes in some programming language. At run-time, we do not need any concrete representation of 𝑎𝑙𝑔. We only have to imagine 𝑎𝑙𝑔 when we program the specialization procedure. All we need is a concrete representation of the specialized version 𝑎𝑙𝑔A. This also means that we cannot use any universal methods for specializing algorithms, which is usually the case with partial evaluation. Instead, we have to program a specialization procedure for every particular algorithm 𝑎𝑙𝑔. An important advantage of doing so is that we can use some powerful ad hoc tricks exploiting peculiarities of 𝑎𝑙𝑔 and the representation of A and B, which are beyond the reach of any universal specialization methods.

Specialization with compilation

The specialized algorithm has to be represented in a form that can be interpreted.

In many situations, usually when 𝑎𝑙𝑔A(B) is to be computed on many values of B in a row, 𝑎𝑙𝑔A can be written as machine code instructions for a special abstract machine, and it is typically said that A is compiled. The code itself can then be additionally optimized by answer-preserving transformations that rely only on the semantics of instructions of the abstract machine.

The instructions of the abstract machine can usually be represented as records. One field of such a record, an instruction identifier (or instruction tag), would identify the instruction type, e.g. an integer field may be used, with particular integer values corresponding to particular instructions. Other fields may be used for storing additional parameters of the instruction, e.g. a pointer field may point to another instruction representing a label, if the semantics of the instruction require a jump. All instructions of the code can be stored in a traversable data structure such as an array, linked list, or tree.

Interpretation (or execution) proceeds by fetching instructions in some order, identifying their type, and executing the actions associated with said type.

In many programming languages, such as C and C++, a simple switch statement may be used to associate actions with different instruction identifiers. Modern compilers usually compile a switch statement with constant (e.g. integer) labels from a narrow range by storing the address of the statement corresponding to a value i in the i-th cell of a special array, as a means of efficient optimisation. This can be exploited by taking values for instruction identifiers from a small interval of values.

Data-and-algorithm specialization

There are situations when many instances of A are intended for long-term storage and the calls of 𝑎𝑙𝑔(A,B) occur with different B in an unpredictable order. For example, we may have to check 𝑎𝑙𝑔(A1,B1) first, then 𝑎𝑙𝑔(A2,B2), then 𝑎𝑙𝑔(A1,B3), and so on. In such circumstances, full-scale specialization with compilation may not be suitable due to excessive memory usage. However, we can sometimes find a compact specialized representation A′ for every A, that can be stored with, or instead of, A. We also define a variant 𝑎𝑙𝑔′ that works on this representation and any call to 𝑎𝑙𝑔(A,B) is replaced by 𝑎𝑙𝑔′(A′,B), intended to do the same job faster.

See also

References

Further reading