Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Example  





2 History  





3 See also  





4 References  



4.1  Further reading  
















Hash consing






Français
Magyar
 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 

(Redirected from Hashcons)

Incomputer 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.[citation needed]

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][3]

Example[edit]

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[edit]

A hash consing compilation technique was presented by A.P. Ershov in 1958.[4][5] The term "hash consing" originates from implementations in the context of Lisp in the 1970s.[6][7]

See also[edit]

References[edit]

  1. ^ Liljenzin, Olle (2013). "Confluently Persistent Sets and Maps". arXiv:1301.3388 [cs.DS].
  • ^ Allen, John (1978). Anatomy of Lisp. McGraw Hill. ISBN 0-07-001115-X.
  • ^ Filliâtre, Jean-Christophe; Conchon, Sylvain (2006). "Type-Safe Modular Hash-Consing". Workshop on ML. ACM.
  • ^ Ershov, A. P. (1 August 1958). "On programming of arithmetic operations". Communications of the ACM. 1 (8): 3–6. doi:10.1145/368892.368907. ISSN 0001-0782. S2CID 15986378.
  • ^ "Sharing and Common Subexpression Elimination in EDSL compilation". okmij.org. Retrieved 27 April 2023.
  • ^ Deutsch, Laurence Peter (1973). An Interactive Program Verifier (PDF) (Phd). Palo Alto: Xerox Palo Alto Research Center Technical Report CSL-73-1.
  • ^ Goto, Eiichi (1974). Monocopy and associative algorithms in extended Lisp (PDF) (Technical report). Tokyo: University of Tokyo Technical Report TR 74-03.
  • Further reading[edit]


  • t
  • e

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Hash_consing&oldid=1170167676"

    Categories: 
    Implementation of functional programming languages
    Hashing
    Computer science stubs
    Hidden categories: 
    All articles with unsourced statements
    Articles with unsourced statements from March 2009
    Articles with example Scheme (programming language) code
    All stub articles
     



    This page was last edited on 13 August 2023, at 14:02 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki