Hash consing

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

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[1][2] 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.[3][4] Hash consing has been shown to give dramatic performance improvements—both space and time—for symbolic and dynamic programming algorithms.[citation needed] An interesting property of hash consing is that two structures can be tested for equality in constant time, which in turn can improve complexity of divide and conquer algorithms when data sets contain overlapping blocks.[5]

In other communities a similar idea is known as the Flyweight pattern. When applied to strings this technique is also known as string interning.

Examples

Scheme

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

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Lua error in package.lua at line 80: module 'strict' not found.

Further reading

  • Lua error in package.lua at line 80: module 'strict' not found.
  • 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.


<templatestyles src="Asbox/styles.css"></templatestyles>