All Topics  
Memoization

 

   Email Print
   Bookmark   Link






 

Memoization



 
 
In computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
, memoization is an optimization
Optimization (computer science)

In computing, optimization is the process of modifying a system to make some aspect of it work more efficiently or use fewer resources. For instance, a computer program may be optimized so that it executes more rapidly, or is capable of operating with less Computer data storage or other resources, or draw less power....
 technique used primarily to speed up computer programs by having function calls
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 avoid repeating the calculation of results for previously-processed inputs. Memoization has also been used in other contexts (and for other purposes other than speed gains), such as in simple mutually-recursive
Mutual recursion

Mutual recursion is a form of recursion where two mathematical or computational functions are defined in terms of each other.For instance, consider two functions even? and odd? defined as follows:...
 descent parsing in a general top-down
Top-down parsing

Top-down parsing is a strategy of analyzing unknown data relationships by hypothesizing general parse tree structures and then considering whether the known fundamental structures are compatible with the hypothesis....
 parsing
Parsing

In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of lexical analysis#Token to determine their grammatical structure with respect to a given formal grammar....
 algorithm that accommodates ambiguity and left recursion
Left recursion

In computer science, left recursion is a special case of recursion.In terms of context-free grammar, a non-terminal r is left-recursive if the left-most symbol in any of r?s ?alternatives? either immediately or through some other non-terminal definitions rewrites to r again....
 in polynomial time and space.






Discussion
Ask a question about 'Memoization'
Start a new discussion about 'Memoization'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
, memoization is an optimization
Optimization (computer science)

In computing, optimization is the process of modifying a system to make some aspect of it work more efficiently or use fewer resources. For instance, a computer program may be optimized so that it executes more rapidly, or is capable of operating with less Computer data storage or other resources, or draw less power....
 technique used primarily to speed up computer programs by having function calls
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 avoid repeating the calculation of results for previously-processed inputs. Memoization has also been used in other contexts (and for other purposes other than speed gains), such as in simple mutually-recursive
Mutual recursion

Mutual recursion is a form of recursion where two mathematical or computational functions are defined in terms of each other.For instance, consider two functions even? and odd? defined as follows:...
 descent parsing in a general top-down
Top-down parsing

Top-down parsing is a strategy of analyzing unknown data relationships by hypothesizing general parse tree structures and then considering whether the known fundamental structures are compatible with the hypothesis....
 parsing
Parsing

In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of lexical analysis#Token to determine their grammatical structure with respect to a given formal grammar....
 algorithm that accommodates ambiguity and left recursion
Left recursion

In computer science, left recursion is a special case of recursion.In terms of context-free grammar, a non-terminal r is left-recursive if the left-most symbol in any of r?s ?alternatives? either immediately or through some other non-terminal definitions rewrites to r again....
 in polynomial time and space. Although related to caching
Cache

In computer science, a cache is a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch or to compute, compared to the cost of reading the cache....
, memoization refers to a specific case of this optimization, distinguishing it from forms of caching such as buffering
Buffer (computer science)

In computing, a buffer is a region of Memory used to temporarily hold data while it is being moved from one place to another. Typically, the data is stored in a buffer as it is retrieved from an input device or just before it is sent to an output device ....
 or page replacement
Page replacement algorithm

In a computer operating system that utilizes paging for virtual memory memory management, page replacement algorithms decide which memory pages to page out when a page of memory needs to be allocated....
.

Overview


The term "memoization" was coined by Donald Michie
Donald Michie

Donald Michie was a British researcher in artificial intelligence. During World War II, Michie worked at Bletchley Park, contributing to the effort to solve "Lorenz SZ 40/42," a German teleprinter cipher....
 in 1968 and is derived from the Latin
Latin

Latin is an Italic language, historically spoken in Latium and Ancient Rome. Through the Military history of the Roman Empire, Latin spread throughout the Mediterranean and a large part of Europe....
 word memorandum
Memorandum

A memorandum or memo is a document or other communication that aids the memory by recording events or observations on a topic, such as may be used in a business office....
 (to be remembered), and thus carries the meaning of turning [the results of] a function into something to be remembered. While memoization might be confused with memorization (because of the shared cognate
Cognate

Cognates in linguistics are words that have a common etymology origin.An example of cognates within the same language would be English shirt vs....
), memoization has a specialized meaning in computing.

A memoized function "remembers" the results corresponding to some set of specific inputs. Subsequent calls with remembered inputs return the remembered result rather than recalculating it, thus moving the primary cost of a call with given parameters to the first call made to the function with those parameters. The set of remembered associations may be a fixed-size set controlled by a replacement algorithm or a fixed set, depending on the nature of the function and its use. A function can only be memoized if it is referentially transparent; that is, only if calling the function has the exact same effect as replacing that function call with its return value. (Special case exceptions to this restriction exist, however.) While related to lookup table
Lookup table

In computer science, a lookup table is a data structure, usually an array or associative array, often used to replace a runtime computation with a simpler array indexing operation....
s, since memoization often uses such tables in its implementation, memoization differs from pure table lookup in that the tables which memoization might use are populated transparently on an as-needed
On the fly

Colloquial usageIn colloquial use, on the fly means something created when needed. The phrase is used: to explain that something wasn't planned ahead, or...
 basis.

Memoization is a means of lowering a function's time cost in exchange for space cost; that is, memoized functions become optimized for speed in exchange for a higher use of computer memory
Computer memory

Computer memory is usually meant to refer to the semiconductor technology that is used to store information in Electronics devices. Current primary computer memory makes use of integrated circuits consisting of silicon-based transistors....
 space. The time/space "cost" of algorithm
Algorithm

In mathematics, computing, linguistics and related subjects, an algorithm is a sequence of finite instructions, often used for calculation and data processing....
s has a specific name in computing: computational complexity
Computational complexity theory

Computational complexity theory, as a branch of the theory of computation in computer science, investigates the problems related to the Computational resource required for the execution of algorithms , and the inherent difficulty in providing efficient algorithms for specific computational problems....
. All functions have a computational complexity in time (i.e. they take time to execute) and in space.

Although a trade-off
Trade-off

A trade-off is a situation that involves losing one quality or aspect of something in return for gaining another quality or aspect. It implies a decision to be made with full comprehension of both the upside and downside of a particular choice....
 occurs (i.e., space used is speed gained), this differs from some other optimizations that involve time-space trade-off, such as strength reduction
Strength reduction

Strength reduction is a compiler optimization where a costly operation is replaced with an equivalent but less expensive operation.Operator strength reduction involves using mathematical identities to replace slow math operations with faster operations....
, in that memoization is a runtime
Runtime

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination ....
 rather than compile time
Compile time

In computer science, compile time refers to either the operations performed by a compiler , programming language requirements that must be met by source code for it to be successfully compiled , or properties of the program that can be reasoned about at compile time....
 optimization. Moreover, strength reduction potentially replaces an expensive operation such as multiplication with a less expensive operation such as addition, and the results in savings can be highly non-portable across machines
Machine-dependent

Machine-dependent is a term for application software that runs only on a particular type of computer. Conversely, applications that run on a variety of different types of computers are called machine-independent ....
, whereas memoization is a machine-independent strategy.

Consider the following pseudocode
Pseudocode

Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading....
 function to calculate the factorial
Factorial

In mathematics, the factorial of a negative and non-negative numbers integer n, denoted by n!, is the Product of all positive integers less than or equal to n....
 of n:

function factorial (n is a non-negative integer) if n is 0 then return 1 [by the convention that 0! = 1] else return factorial(n - 1) times n [recursively invoke factorial with the parameter 1 less than n] end if end function

For every integer
Integer

The integers are natural numbers including 0 and their negative and non-negative numberss . They are numbers that can be written without a fractional or decimal component, and fall within the set ....
 n such that , the final result of the function factorial is invariant
Invariant (computer science)

In Computer science, a predicate that, if true, will remain true throughout a specific sequence of Operation , is called invariant to that sequence....
; if invoked as x = factorial(3), the result is such that x will always be assigned the value 6. A non-memoized version of the above, given the nature of the recursive
Recursion

Recursion, in mathematics and computer science, is a method of defining Function in which the function being defined is applied within its own definition....
 algorithm
Algorithm

In mathematics, computing, linguistics and related subjects, an algorithm is a sequence of finite instructions, often used for calculation and data processing....
 involved, would require n + 1 invocations of factorial to arrive at a result, and each of these invocations, in turn, has an associated cost in the time it takes the function to return the value computed. Depending on the machine, this cost might be the sum of:

  1. The cost to set up the functional call stack frame.
  2. The cost to compare n to 0.
  3. The cost to subtract 1 from n.
  4. The cost to set up the recursive call stack frame. (As above.)
  5. The cost to multiply the result of the recursive call to factorial by n.
  6. The cost to store the return result so that it may be used by the calling context.


In a non-memoized implementation, every top-level call to factorial includes the cumulative cost of steps 2 through 6 proportional to the initial value of n.

A memoized version of the factorial function follows:

function factorial (n is a non-negative integer) allocate temporary integer variable x

if n is in lookup-table then return lookup-table-value-for-n; else if n is 0 then return 1 [by the convention that 0! = 1] else x = factorial(n - 1) times n [recursively invoke factorial with the parameter 1 less than n]

store x in lookup-table in the nth slot [remember the result of n! for later]

return x end if

In this particular example, if factorial is first invoked with 5, and then invoked later with any value less than or equal to five, those return values will also have been memoized, since factorial will have been called recursively with the values 5, 4, 3, 2, 1, and 0, and the return values for each of those will have been stored. If it is then called with a number greater than 5, such as 7, only 2 recursive calls will be made (7 and 6), and the value for 5! will have been stored from the previous call. In this way, memoization allows a function to become more time-efficient the more often it is called, thus resulting in eventual overall speed up.

Some other considerations


Automatic memoization


While memoization may be added to functions internally and explicitly by a computer programmer in much the same way the above memoized version of factorial is implemented, referentially transparent functions may also be automatically memoized externally. The techniques employed by Norvig have application not only in Common Lisp
Common Lisp

Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
 (the language in which his paper demonstrated automatic memoization), but in various other programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s. Applications of automatic memoization have also been formally explored in the study of term rewriting and artificial intelligence
Artificial intelligence

Artificial intelligence is the intelligence of machines and the branch of computer science which aims to create it. Major AI textbooks define the field as "the study and design of intelligent agents,"...
.

In those programming languages where functions are first or second-class objects
First-class object

In computing, a first-class object , in the context of a particular programming language, is an entity which can be used in programs without restriction ....
 (such as Lua, with its first-class functions, or Perl
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
 ), automatic memoization can be implemented by replacing (at runtime
Runtime

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination ....
) a function with its calculated value once a value has been calculated for a given set of parameters. The function that does this value-for-function-object replacement can generically wrap any referentially transparent function. Consider the following pseudocode
Pseudocode

Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading....
 (where it is assumed that functions are first-class values):

function memoized-call (F is a function object parameter) if F has no attached array values then allocate an associative array
Associative array

An associative array is an abstract data type composed of a Collection of unique keys and a collection of values, where each key is associated with one value ....
 called values; attach values to F; end if;

if F.values[arguments] is empty then F.values[arguments] = F(arguments); end if;

return F.values[arguments]; end function

In order to call an automatically memoized version of factorial using the above strategy, rather than calling factorial directly, code invokes memoized-call(factorial(n)). Each such call first checks to see if a holder array has been allocated to store results, and if not, attaches that array. If no entry exists at the position values[arguments] (where arguments are used as the key of the associative array), a real call is made to factorial with the supplied arguments. Finally, the entry in the array at the key position is returned to the caller.

The above strategy requires explicit wrapping at each call to a function that is to be memoized. In those languages that allow closures
Closure (computer science)

In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables....
, memoization can be effected implicitly by a functor
Function object

A function object, also called a functor, functional or functionoid, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function , usually with the same syntax....
 factory that returns a wrapped memoized function object. In pseudocode, this can be expressed as follows:

function construct-memoized-functor (F is a function object parameter) allocate a function object called memoized-version;

let memoized-version(arguments) be if self has no attached array values then [self is a reference to this
This (computer science)

In many object-oriented programming programming languages, this is a keyword that is used in instance methods to refer to the object on which they are working....
 object] allocate an associative array called values; attach values to self; end if;

if self.
values[arguments] is empty then self.values[arguments] = F(arguments); end if;

return self.
values[arguments]; end let;

return
memoized-version; end function

Rather than call factorial, a new function object memfact is created as follows:

memfact = construct-memoized-functor(factorial)

The above example assumes that the function factorial has already been defined
before the call to construct-memoized-functor is made. From this point forward, memfact(n) is called whenever the factorial of n is desired. In languages such as Lua, more sophisticated techniques exist which allow a function to be replaced by a new function with the same name, which would permit:

factorial = construct-memoized-functor(factorial)

Essentially, such techniques involve attaching the
original function object to the created functor and forwarding calls to the original function being memoized via an alias when a call to the actual function is required (to avoid endless recursion
Recursion

Recursion, in mathematics and computer science, is a method of defining Function in which the function being defined is applied within its own definition....
), as illustrated below:

function construct-memoized-functor (
F is a function object parameter) allocate a function object called memoized-version;

let
memoized-version(arguments) be if self has no attached array values then [
self
is a reference to this
This (computer science)

In many object-oriented programming programming languages, this is a keyword that is used in instance methods to refer to the object on which they are working....
 object
] allocate an associative array called values; attach values to self; allocate a new function object called alias; attach alias to self; [for later ability to invoke F indirectly] self.alias = F; end if;

if self.values[arguments] is empty then self.values[arguments] = self.alias(arguments); [not a direct call to F] end if;

return self.values[arguments]; end let;

return memoized-version; end function

(Note: Some of the steps shown above may be implicitly managed by the implementation language and are provided for illustration.)

Parsers

When a top-down parser
Top-down parsing

Top-down parsing is a strategy of analyzing unknown data relationships by hypothesizing general parse tree structures and then considering whether the known fundamental structures are compatible with the hypothesis....
 tries to parse an ambiguous input with respect to an ambiguous CFG
Context-free grammar

In formal language theory, a context-free grammar is a formal grammar in which every Production rule is of the formwhere V is a single nonterminal symbol, and w is a string of Terminal and nonterminal symbolss and/or nonterminals ....
, it may need an exponential number of steps (with respect to the length of the input) to try all alternatives of the CFG in order to produce all possible parse trees. This eventually would require exponential memory space. Memoization was explored as a parsing
Parsing

In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of lexical analysis#Token to determine their grammatical structure with respect to a given formal grammar....
 strategy in 1991 by Norvig
Peter Norvig

Peter Norvig is an United States computer science. He is currently the Director of Research at GoogleHe is a Fellow and Councilor of the American Association for Artificial Intelligence and co-author, with Stuart J....
, who demonstrated that an algorithm similar to the use of dynamic programming and state-sets in Earley's algorithm
Earley parser

The Earley parser is a type of chart parser mainly used for parsing in computational linguistics, named after its inventor, Jay Earley. The algorithm uses dynamic programming....
 (1970), and tables in the CYK algorithm
CYK algorithm

The Cocke-Younger-Kasami algorithm determines whether astring can be generated by a given context-free grammar and, if so, how it can be generated....
 of Cocke, Younger and Kasami, could be generated by introducing automatic memoization to a simple backtracking
Backtracking

Backtracking is a general algorithm for finding all solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c as soon as it determines that c cannot possibly be completed to a valid solution ....
 recursive descent parser
Recursive descent parser

A recursive descent parser is a top-down parsing built from a set of Mutual recursion procedures where each such procedure usually implements one of the production rules of the formal grammar....
 to solve the problem of exponential time complexity. The basic idea in Norvig’s approach is that when a parser is applied to the input, the result is stored in a memotable for subsequent reuse if the same parser is ever reapplied to the same input. Richard Frost also used memoization to reduce the exponential time complexity of parser combinators
Parser Combinator

In mathematics and functional programming, Higher-order function are defined as the functions that can take functions as their input and can also produce functions as their output....
, which can be viewed as “Purely Functional Top-Down Backtracking” parsing technique. He showed that basic memoized parser combinators can be used as building blocks to construct complex parsers as executable specifications of CFGs. It was again explored in the context of parsing in 1995 by Johnson and Dörre. In 2002, it was examined in considerable depth by Ford in the form called packrat parsing.

In 2007, Frost, Hafiz and Callaghan described a top-down parsing algorithm that uses memoization
Memoization

In computing, memoization is an Optimization technique used primarily to speed up computer programs by having Subroutine avoid repeating the calculation of results for previously-processed inputs....
 for refraining redundant computations to accommodate any form of ambiguous CFG
CFG

CFG is an acronym that has two meanings in computer science, both related to compilers:* Context-free grammar* Control flow graphCFG can also refer to:...
 in polynomial
Polynomial

In mathematics, a polynomial is an expression constructed from variables and constants, using the operations of addition, subtraction, multiplication, and constant non-negative whole number exponents....
 time (T
Big O notation

In mathematics, big O notation describes the asymptotic analysis of a function when the argument tends towards a particular value or infinity, usually in terms of simpler functions....
(n4) for left-recursive
Left recursion

In computer science, left recursion is a special case of recursion.In terms of context-free grammar, a non-terminal r is left-recursive if the left-most symbol in any of r?s ?alternatives? either immediately or through some other non-terminal definitions rewrites to r again....
 grammars and T
Big O notation

In mathematics, big O notation describes the asymptotic analysis of a function when the argument tends towards a particular value or infinity, usually in terms of simpler functions....
(n3) for non left-recursive grammars). Their top-down parsing algorithm also requires polynomial space for potentially exponential ambiguous parse trees by 'compact representation' and 'local ambiguities grouping'. Their compact representation is comparable with Tomita’s compact representation of bottom-up parsing
Bottom-up parsing

Bottom-up parsing is a strategy for analyzing unknown data relationships that attempts to identify the most fundamental units first, and then to infer higher-order structures from them....
. Their use of memoization is not only limited to retrieving the previously-computed results when a parser is applied to a same input position repeatedly (which is essential for polynomial time requirment); it is specialized to perform the following additional tasks:
  • The memoization process (which could be viewed as a ‘wrapper’ around any parser execution) accommodates an ever-growing direct left-recursive parse by imposing depth restrictions w.r.t. input length and current input position.
  • The algorithm’s memo-table ‘lookup’ procedure also determines the reusability of saved result by comparing saved result’s computational context with the parser’s current context. This contextual comparison is the key to accommodate indirect (or hidden) left-recursion.
  • When performing a successful lookup in memotable, instead of returning the complete result-set, the process only returns the references of the actual result and eventually speeds up the overall computation.
  • During updating the memotable, the meoization process groups the (potentially exponential) ambiguous results and ensures the polynomial space requirement.


Frost, Hafiz and Callaghan also described the implementation of the algorithm in PADL’08 as a set of higher-order function
Higher-order function

In mathematics and computer science, higher-order functions or functional are function s which do at least one of the following:*take one or more functions as an input...
s (called parser combinators) in Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
, which enables the construction of directly executable specifications of CFG
CFG

CFG is an acronym that has two meanings in computer science, both related to compilers:* Context-free grammar* Control flow graphCFG can also refer to:...
s as language processors. The importance of their polynomial algorithm’s power to accommodate ‘any form of ambiguous CFG’ with top-down parsing is vital with respect to the syntax and semantics analysis during Natural Language Processing
Natural language processing

Natural language processing is a field of computer science concerned with the interactions between computers and human languages. Natural language generation systems convert information from computer databases into readable human language....
. The site has more about the algorithm and implementation details.

While Norvig increased the power of the parser through memoization, the augmented parser was still as time complex as Earley's algorithm, which demonstrates a case of the use of memoization for something other than speed optimization. Johnson and Dörre demonstrate another such non-speed related application of memoization: the use of memoization to delay linguistic constraint resolution to a point in a parse where sufficient information has been accumulated to resolve those constraints. By contrast, in the speed optimization application of memoization, Ford demonstrated that memoization could guarantee that parsing expression grammar
Parsing expression grammar

A parsing expression grammar, or PEG, is a type of analytic grammar formal grammar that describes a formal language in terms of a set of rules for recognizing string in the language....
s could parse in linear
Big O notation

In mathematics, big O notation describes the asymptotic analysis of a function when the argument tends towards a particular value or infinity, usually in terms of simpler functions....
 time even those languages
Formal language

A formal language is a set of words, i.e. finite string of letters, or symbols. The inventory from which these letters are taken is called the alphabet over which the language is defined....
 that resulted in worst-case backtracking behavior.

Consider the following grammar
Formal grammar

In formal language theory, grammars, also called formal grammars or generative grammars, are a formalism used to describe formal languages – i.e....
:

S ? (A c) | (B d) A ? X (a|b) B ? X b X ? x [X]

(Notation note: In the above example, the production S ? (A c) | (B d) reads: "An S is either an A followed by a c or a B followed by a d." The production X ? x [X] reads "An X is an x followed by an optional X.")

This grammar generates one of the following three variations of string
String (computer science)

In computer programming and some branches of mathematics, a string is an ordered sequence of symbols. These symbols are chosen from a predetermined set or alphabet....
: xac, xbc, or xbd (where x here is understood to mean one or more x's.) Next, consider how this grammar, used as a parse specification, might effect a top-down, left-right parse of the string xxxxxbd:

The rule A will recognize xxxxxb (by first descending into X to recognize one x, and again descending into X until all the x's are consumed, and then recognizing the b), and then return to S, and fail to recognize a c. The next clause of S will then descend into B, which in turn again descends into X and recognizes the x's by means of many recursive calls to X, and then a b, and returns to S and finally recognizes a d.


The key concept here is inherent in the phrase
again descends into X
. The process of looking forward, failing, backing up, and then retrying the next alternative is known in parsing as backtracking
Backtracking

Backtracking is a general algorithm for finding all solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c as soon as it determines that c cannot possibly be completed to a valid solution ....
, and it is primarily backtracking that presents opportunities for memoization in parsing. Consider a function RuleAcceptsSomeInput(Rule, Position, Input), where the parameters are as follows:

  • Rule is the name of the rule under consideration.
  • Position is the offset currently under consideration in the input.
  • Input is the input under consideration.


Let the return value of the function RuleAcceptsSomeInput be the length of the input accepted by Rule, or 0 if that rule does not accept any input at that offset in the string. In a backtracking scenario with such memoization, the parsing process is as follows:

When the rule A descends into X at offset 0, it memoizes the length 5 against that position and the rule X. After having failed at d, B then, rather than descending again into X, queries the position 0 against rule X in the memoization engine, and is returned a length of 5, thus saving having to actually descend again into X, and carries on as if it had descended into X as many times as before.


In the above example, one or many descents into X may occur, allowing for strings such as xxxxxxxxxxxxxxxxbd. In fact, there may be any number of x's before the b. While the call to S must recursively descend into X as many times as there are x's, B will never have to descend into X at all, since the return value of RuleAcceptsSomeInput(X, 0, xxxxxxxxxxxxxxxxbd) will be 16 (in this particular case).

Those parsers that make use of syntactic predicate
Syntactic predicate

A syntactic predicate specifies the syntactic validity of applying a production in a formal grammar and is analogous to a semantic predicate that specifies the semantic validity of applying a production....
s are also able to memoize the results of predicate parses, as well, thereby reducing such constructions as:

S ? (A)? A A ? /* some rule */

to one descent into A.

If a parser builds a parse tree
Parse tree

A parse tree or concrete syntax tree is an tree that represents the syntax structure of a string according to some formal grammar. In a parse tree, the interior nodes are labeled by nonterminals of the grammar, while the leaf nodes are labeled by terminal symbol of the grammar....
 during a parse, it must memoize not only the length of the input that matches at some offset against a given rule, but also must store the sub-tree that is generated by that rule at that offset in the input, since subsequent calls to the rule by the parser will not actually descend and rebuild that tree. For the same reason, memoized parser algorithms that generate calls to external code (sometimes called a semantic action routine) when a rule matches must use some scheme to ensure that such rules are invoked in a predictable order.

Since, for any given backtracking or syntactic predicate capable parser not every grammar will need backtracking or predicate checks, the overhead of storing each rule's parse results against every offset in the input (and storing the parse tree if the parsing process does that implicitly) may actually slow down a parser. This effect can be mitigated by explicit selection of those rules the parser will memoize.

See also


  • Computational complexity theory
    Computational complexity theory

    Computational complexity theory, as a branch of the theory of computation in computer science, investigates the problems related to the Computational resource required for the execution of algorithms , and the inherent difficulty in providing efficient algorithms for specific computational problems....
     - More information on algorithm complexity.
  • Strength reduction
    Strength reduction

    Strength reduction is a compiler optimization where a costly operation is replaced with an equivalent but less expensive operation.Operator strength reduction involves using mathematical identities to replace slow math operations with faster operations....
     - A compiler
    Compiler

    A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program....
     optimization that replaces an expensive operation with an equivalent, less expensive one.
  • Partial evaluation
    Partial evaluation

    In computing, partial evaluation is a technique for optimization by specialization.A computer program, prog, is seen as a mapping of input data into output data:...
     - A related technique for automatic program optimization
  • Lazy evaluation
    Lazy evaluation

    In computer programming, lazy evaluation is the technique of delaying a computation until such time as the result of the computation is known to be needed....
     - Shares some concepts with memoization.
  • Lookup table
    Lookup table

    In computer science, a lookup table is a data structure, usually an array or associative array, often used to replace a runtime computation with a simpler array indexing operation....
     - A key data structure
    Data structure

    A data structure in computer science is a way of storing data in a computer so that it can be used efficiently. It is an organization of mathematical and logical concepts of data....
     used in memoization.
  • Flyweight pattern
    Flyweight pattern

    Flyweight is a software design pattern . A Flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory....
     - an object programming design pattern
    Design pattern (computer science)

    In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code ....
    , that also uses kind of memoization


External links


Examples of memoization in various programming languages
  • - Memoize is a small library, written by Tim Bradshaw, for performing memoization in Common Lisp
    Common Lisp

    Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
    .
  • Marty Hall's in Common Lisp
    Common Lisp

    Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
  • - a Perl
    Perl

    In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
     module
    Perl module

    A Perl module is a discrete component of software for the Perl programming language. A module is distinguished by a unique namespace, e.g. "CGI" or "Net::FTP" or "XML::Parser" and a filename similarly named ....
     that implements memoized functions.
  • - an example in Java using dynamic proxy classes to create a generic memoization pattern.
  • - although C++
    C++

    C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
     doesn't support first-class functions, here is a toolkit that supports automated memoization (via pre-processing).
  • - Open source Java memoizer using annotations and pluggable cache implementations.
  • - A Ruby module that implements memoized methods.
  • - A Python
    Python (programming language)

    Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
     example of memoization.
  • - Implemented as a Camlp4
    Camlp4

    Camlp4 is a software system for writing extensible parsers for programming languages. It provides a set of Objective Caml libraries that are used to define grammars as well as loadable syntax extensions of such grammars....
     syntax extension.
  • - Two example implementations of a general memoize function in Lua.
  • - Extending the Function prototype in JavaScript
    JavaScript

    JavaScript is a scripting language widely used for client-side web development. It was the originating Programming language dialect of the ECMAScript standard....
    .
  • - eXecutable SpecificAtIons of GrAmmars. Contains publications related to top-down parsing algorithm that supports left-recursion and ambiguity in polynomial time and space.