help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: what's "weakness" in elisp's hash table?


From: Tassilo Horn
Subject: Re: what's "weakness" in elisp's hash table?
Date: Thu, 03 Jan 2008 22:15:58 +0100
User-agent: Gnus/5.110007 (No Gnus v0.7) Emacs/23.0.50 (gnu/linux)

Xah Lee <xah@xahlee.org> writes:

> in emacs lisp, when creating a hash table, there's the “:weakness”
> thing.
>
> See
> http://xahlee.org/elisp/Creating-Hash.html
>
> what does that mean? Suppose if i do
>
>   (setq myhash (make-hash-table :test 'equal :weakness 'key))
>   (puthash "mary" "19" myhash)
>   ...
>
> Then, the "mary" would disappear if i don't access it??

No, if you don't hold a reference to it.  Here an example:

,----
| *** Welcome to IELM ***  Type (describe-mode) for help.
| ELISP> (setq myhash (make-hash-table :test 'equal :weakness 'key))
| #<hash-table 'equal key 0/65 0xa840798>
| ELISP> (puthash "mary" "19" myhash)
| "19"
| ELISP> (gethash "mary" myhash)
| "19"
| ELISP> (garbage-collect)
| ((896892 . 77250)
|  (104815 . 0)
|  (5412 . 7941)
|  7026294 1304097
|  (916 . 862)
|  (23843 . 4126)
|  (224711 . 21115))
| 
| ELISP> (gethash "mary" myhash)
| nil
| ELISP> ;; It's gone cause the key "mary" was not referenced.
| ELISP> (setq mary "mary")
| "mary"
| ELISP> (puthash mary "19" myhash)
| "19"
| ELISP> (gethash mary myhash)
| "19"
| ELISP> (garbage-collect)
| ((902678 . 85905)
|  (104976 . 1)
|  (5422 . 7889)
|  7040124 1307039
|  (916 . 738)
|  (24339 . 3594)
|  (225190 . 20636))
| 
| ELISP> (gethash mary myhash)
| "19"
| ELISP> ;; Still there cause the variable mary holds a reference to the
| ELISP> ;; key "mary".
`----

And here are the docs that clearly state that.

,----[ (info "(elisp)Creating Hash") ]
|     `:weakness WEAK'
|           The weakness of a hash table specifies whether the presence
|           of a key or value in the hash table preserves it from garbage
|           collection.
| 
|           The value, WEAK, must be one of `nil', `key', `value',
|           `key-or-value', `key-and-value', or `t' which is an alias for
|           `key-and-value'.  If WEAK is `key' then the hash table does
|           not prevent its keys from being collected as garbage (if they
|           are not referenced anywhere else); if a particular key does
|           get collected, the corresponding association is removed from
|           the hash table.
| 
|           If WEAK is `value', then the hash table does not prevent
|           values from being collected as garbage (if they are not
|           referenced anywhere else); if a particular value does get
|           collected, the corresponding association is removed from the
|           hash table.
| 
|           If WEAK is `key-and-value' or `t', both the key and the value
|           must be live in order to preserve the association.  Thus, the
|           hash table does not protect either keys or values from garbage
|           collection; if either one is collected as garbage, that
|           removes the association.
| 
|           If WEAK is `key-or-value', either the key or the value can
|           preserve the association.  Thus, associations are removed
|           from the hash table when both their key and value would be
|           collected as garbage (if not for references from weak hash
|           tables).
| 
|           The default for WEAK is `nil', so that all keys and values
|           referenced in the hash table are preserved from garbage
|           collection.
`----

Bye,
Tassilo





reply via email to

[Prev in Thread] Current Thread [Next in Thread]