IT lexicon › Programming › Hash consing

Hash consing

Programming På svenska → Updated: 2026-11-06

Never building the same immutable value twice: the constructor first checks whether an identical structure already exists, and if so hands that one out.

Ask for the node add(x, 1) and the operator plus the children's addresses get hashed; if the node is already in the table, you get the existing pointer back. Structurally equal expressions become the same object, and equality drops from a recursive tree walk to a pointer comparison. The idea goes back to Andrei Ershov's work on common subexpressions in the late 1950s. The name comes from Lisp, where Eiichi Goto made it systematic in HLISP in the 1970s.

Compilers do it everywhere without calling it anything. LLVM uniques types and constants, so i32 7 exists exactly once per context. Value numbering is essentially hash consing of computations, and e-graphs owe their efficiency to it. The catch is a global table that needs cleaning out, or it becomes a memory leak with a clear conscience.

← Back to the lexicon