In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied 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
Automated theorem proving (also known as ATP or automated deduction) is a subfield of automated reasoning and mathematical logic dealing with proving mathematical theorems by computer programs. Automated reasoning over mathematical proof was a majo ...
and, more specifically, in the
Vampire theorem prover
Vampire is an automatic theorem prover for first-order classical logic developed in the Department of Computer Science at the University of Manchester. Up to Version 3, it was developed by Andrei Voronkov together with Kryštof Hoder and prev ...
project.
The idea is inspired by the use of
partial evaluation
In computing, partial evaluation is a technique for several different types of program optimization by specialization. The most straightforward application is to produce new programs that run faster than the originals while being guaranteed to ...
in optimising program translation.
Many core operations in theorem provers exhibit the following pattern.
Suppose that we need to execute some algorithm
in a situation where a value of
''is fixed for potentially many different values of''
. In order to do this efficiently, we can try to find a specialization of
for every fixed
, i.e., such an algorithm
, that executing
is equivalent to executing
.
The specialized algorithm may be more efficient than the generic one, since it can ''exploit some particular properties'' of the fixed value
. Typically,
can avoid some operations that
would have to perform, if they are known to be redundant for this particular parameter
.
In particular, we can often identify some tests that are true or false for
, unroll loops and recursion, etc.
Difference from partial evaluation
The key difference between run-time specialization and partial evaluation is that the values of
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
. 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
and
, 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
is to be computed on many values of
in a row,
can be written as machine code instructions for a special
abstract machine
In computer science, an abstract machine is a theoretical model that allows for a detailed and precise analysis of how a computer system functions. It is similar to a mathematical function in that it receives inputs and produces outputs based on p ...
, and it is typically said that
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
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
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
In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
such as an
array
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
,
linked list
In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whi ...
, or
tree
In botany, a tree is a perennial plant with an elongated stem, or trunk, usually supporting branches and leaves. In some usages, the definition of a tree may be narrower, e.g., including only woody plants with secondary growth, only ...
.
''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
in the
-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
are intended for long-term storage and the calls of
occur with different
in an unpredictable order.
For example, we may have to check
first, then
, then
, 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
for every
, that can be stored with, or instead of,
.
We also define a variant
that works on this representation
and any call to
is replaced by
, intended to do the same job faster.
See also
*
Psyco
Psyco is an unmaintained specializing just-in-time compiler for pre-2.7 Python originally developed by Armin Rigo and further maintained and developed by Christian Tismer. Development ceased in December, 2011.
Psyco ran on BSD-derived operat ...
, a specializing run-time compiler for
Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (prog ...
*
multi-stage programming Multi-stage programming (MSP) is a variety of metaprogramming in which compilation is divided into a series of intermediate phases, allowing typesafe run-time code generation.
Statically defined types are used to verify that dynamically constructe ...
References
A. Voronkov, "The Anatomy of Vampire: Implementing Bottom-Up Procedures with Code Trees", ''Journal of Automated Reasoning'', 15(2), 1995(''original idea'')
Further reading
* A. Riazanov and
A. Voronkov, "Efficient Checking of Term Ordering Constraints", ''Proc.
IJCAR
The International Joint Conference on Automated Reasoning (IJCAR) is a series of conferences on the topics of automated reasoning, automated deduction, and related fields. It is organized semi-regularly as a merger of other meetings. IJCAR repl ...
'' 2004, Lecture Notes in Artificial Intelligence 3097, 2004 (''compact but self-contained illustration of the method'')
* A. Riazanov and A. Voronkov
Efficient Instance Retrieval with Standard and Relational Path Indexing Information and Computation
''Information and Computation'' is a closed-access computer science journal published by Elsevier (formerly Academic Press). The journal was founded in 1957 under its former name ''Information and Control'' and given its current title in 1987. , t ...
, 199(1-2), 2005 (''contains another illustration of the method'')
* A. Riazanov
"Implementing an Efficient Theorem Prover" PhD thesis, The University of Manchester, 2003 (''contains the most comprehensive description of the method and many examples'')
Algorithms
Software optimization