Hash consing explained
In computer science, particularly in functional programming, hash consing is a technique used to share values that are structurally equal. When a value is constructed, such as a cons cell, the technique checks if such a value has been constructed before, and if so reuses the previous value, avoiding a new memory allocation. A useful property of hash consing is that two structures can be tested for equality in constant time via pointer equality, which in turn can improve efficiency of divide and conquer algorithms when data sets contain overlapping blocks.[1] Hash consing has been shown to give dramatic performance improvements - both space and time - for symbolic and dynamic programming algorithms.
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.[2]
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:
- 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)))))
History
A hash consing compilation technique was presented by A.P. Ershov in 1958.[3] [4] The term "hash consing" originates from implementations in the context of Lisp in the 1970s.[5] [6]
See also
References
Further reading
- 10.1145/368892.368907. Ershov, A.P.. On programming of arithmetic operations. Communications of the ACM. 1. 8. 3 - 6. 1958. 15986378 . free.
- 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.
Notes and References
- Liljenzin. Olle. Confluently Persistent Sets and Maps. 1301.3388. 2013. cs.DS .
- Book: Allen, John. Anatomy of Lisp. McGraw Hill. 1978. 0-07-001115-X.
- Ershov . A. P. . On programming of arithmetic operations . Communications of the ACM . 1 August 1958 . 1 . 8 . 3–6 . 10.1145/368892.368907 . 15986378 . 0001-0782. free .
- Web site: Sharing and Common Subexpression Elimination in EDSL compilation . okmij.org . 27 April 2023.
- Laurence Peter. Deutsch. L Peter Deutsch. An Interactive Program Verifier. Palo Alto. Phd. University of California-Berkeley-->. Xerox Palo Alto Research Center Technical Report CSL-73-1. 1973.
- Eiichi. Goto. Eiichi Goto. Monocopy and associative algorithms in extended Lisp. Tokyo. University of Tokyo Technical Report TR 74-03. 1974.