kb/data/en.wikipedia.org/wiki/Hash_consing-0.md

2.1 KiB
Raw Blame History

title chunk source category tags date_saved instance
Hash consing 1/1 https://en.wikipedia.org/wiki/Hash_consing reference science, encyclopedia 2026-05-05T11:34:20.230463+00:00 kb-cron

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. 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.

== Example == The following is a simple (though inefficient) demonstration of a memoizer by means of hash table and weak references in Scheme. The bwp-object function returns true if the reference given is a broken weak pointer, i.e., the target has been garbage-collected.

== History == A hash consing compilation technique was presented by A.P. Ershov in 1958. The term "hash consing" originates from implementations in the context of Lisp in the 1970s.

== See also == String interning Flyweight pattern Merkle tree Hashlife Interning

== References ==

=== Further reading === Goto, Eiichi (1974-05-01), Monocopy and Associative Algorithms in an Extended Lisp, retrieved 2025-08-09 Ershov, A.P. (1958). "On programming of arithmetic operations". Communications of the ACM. 1 (8): 36. doi:10.1145/368892.368907. S2CID 15986378. Jean Goubault. Implementing Functional Languages with Fast Equality, Sets and Maps: an Exercise in Hash Consing. In Journées Francophones des Langages Applicatifs (JFLA93), pages 222238, Annecy, February 1993.