HOME

TheInfoList



OR:

In computer science, particularly in functional programming, hash consing is a technique used to share values that are structurally equal. The term ''hash consing'' originates from implementations of
Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lisping ...
that attempt to reuse cons cells that have been constructed before, avoiding the penalty of memory allocation. Hash consing is most commonly implemented with hash tables storing weak references that may be garbage-collected when the data stored therein contains no references from outside the table. Hash consing has been shown to give dramatic performance improvements—both space and time—for
symbolic Symbolic may refer to: * Symbol, something that represents an idea, a process, or a physical entity Mathematics, logic, and computing * Symbolic computation, a scientific area concerned with computing with mathematical formulas * Symbolic dynamic ...
and dynamic programming algorithms. An interesting property of hash consing is that two structures can be tested for equality in constant time, which in turn can improve efficiency of
divide and conquer Divide and rule policy ( la, divide et impera), or divide and conquer, in politics and sociology is gaining and maintaining power divisively. Historically, this strategy was used in many different ways by empires seeking to expand their terr ...
algorithms when data sets contain overlapping blocks.


Example

Simple, not very efficient, but suitable for demonstration of the concept implementation of a memoizer by means of hash table and weak references in
Scheme A scheme is a systematic plan for the implementation of a certain idea. Scheme or schemer may refer to: Arts and entertainment * ''The Scheme'' (TV series), a BBC Scotland documentary series * The Scheme (band), an English pop band * ''The Schem ...
: ;; weak hashes ;; (require 'hash-table) (define (make-weak-table . args) (apply make-hash-table args)) (define (weak-table-set! table key data) (let ((w (hash-table-ref table key #f))) (if w (vector-set! w 0 data) (let ((w (make-weak-vector 1))) (vector-set! w 0 data) (hash-table-set! table key w))))) (define (weak-table-ref table key) (let ((w (hash-table-ref table key #f))) (if w (vector-ref w 0) #f))) ;; memoizer factory: for given (side-effect-free) procedure, ;; return a procedure which does the same memoizing some of results ;; in the sense of equal? on the whole list of args ;; (define (make-weak-memoizer proc) (let ((cache (make-weak-table equal?))) (lambda args (let ((x (weak-table-ref cache args))) (if (bwp-object? x) (let ((r (apply proc args))) (weak-table-set! cache args r) r) x)))))


See also

* String interning * Flyweight pattern * Merkle tree * Hashlife * Interning


References


Further reading

* * Jean Goubault. Implementing Functional Languages with Fast Equality, Sets and Maps: an Exercise in Hash Consing. In ''Journées Francophones des Langages Applicatifs'' (JFLA’93), pages 222–238, Annecy, February 1993. Implementation of functional programming languages Hashing Articles with example Scheme (programming language) code {{comp-sci-stub