21 lines · 1.1 KB · Text
RawDownload
Example application of weak pointers: weak hash consing------------------------------------------------------- One application of weak pointers is to implement hash consing withoutspace leaks.  The idea in hash consing is to re-use pairs: whenever anew pair (a, b) is to be built, an auxiliary hash table is checked tosee whether such a pair exists already.  If so, the old pair isreused.  In some applications, this may conserve much space and time.However, there is a danger of running out of memory because of a spaceleak: the pair (a, b) cannot be deallocated by the garbage collectorbecause it remains forever reachable from the auxiliary table.  Tocircumvent this problem, one creates a weak pointer from the auxiliarytable to the pair, so that the auxiliary table in itself cannot keepthe pair alive. The hashcons.sml example demonstrates how hash consing reuses conscells, and how a cons cell is allowed to die (to be deallocated)although it is still referred to from the hash table. --------------------------------------------File mosml/examples/weak/README * 1998-04-19