encyclopegia.org

Hash consing

In computer science, particularly in functional programming, hash consing is a technique used to share values that are structurally equal.[src] 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.[src] 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.[src] Hash consing has been shown to give dramatic performance improvements—both space and time—for symbolic and dynamic programming algorithms.[src] 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.[src]

In computer science, particularly in functional programming, hash consing is a technique used to share values that are structurally equal.[src] 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.[src] 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.[src] Hash consing has been shown to give dramatic performance improvements—both space and time—for symbolic and dynamic programming algorithms.[src] 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.[src] ==Example== The following is a simple (though inefficient) demonstration of a memoizer by means of hash table and weak references in Scheme.[src] The bwp-object function returns true if the reference given is a broken weak pointer, i.e., the target has been garbage-collected.[src] weak hashes (require 'hash-table) (define (make-weak-table .[src] args) (apply make-hash-table args)) (define (weak-table-set![src] table key data) (let ((w (hash-table-ref table key #f))) (if w (vector-set![src] w 0 data) (let ((w (make-weak-vector 1))) (vector-set![src] w 0 data) (hash-table-set![src] 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?[src] 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?[src] x) (let ((r (apply proc args))) (weak-table-set![src] cache args r) r) x))))) ==History== A hash consing compilation technique was presented by A.P.[src] Ershov in 1958.[src] The term "hash consing" originates from implementations in the context of Lisp in the 1970s.[src] ==See also== String interning Flyweight pattern Merkle tree Hashlife Interning ===Further reading=== Jean Goubault.[src] Implementing Functional Languages with Fast Equality, Sets and Maps: an Exercise in Hash Consing.[src] In Journées Francophones des Langages Applicatifs (JFLA’93), pages 222–238, Annecy, February 1993.[src]

26 sentences · 26 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Hash consing." encyclopegia.org, 2025, https://encyclopegia.org/wiki/hash-consing. Accessed 26 November 2026.

Chicago

"Hash consing." encyclopegia.org. Accessed November 26, 2026. https://encyclopegia.org/wiki/hash-consing.

APA

encyclopegia.org. (2025). Hash consing. Retrieved November 26, 2026, from https://encyclopegia.org/wiki/hash-consing

Accessed November 26, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Hash Chain

article · references available

N-Hash

stub · references available

All About Hash

stub · references available

Hash Bash

stub · references available

hash list

stub · references available

Maher-shalal-hash-baz

stub · references available

Of Cash and Hash

stub · references available

Template:Cryptography hash

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1314849526 · 0.5 · snapshot enwiki-001314849526

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.