* what's "weakness" in elisp's hash table?
@ 2008-01-03 16:43 Xah Lee
2008-01-03 21:15 ` Tassilo Horn
2008-01-06 9:45 ` =?ISO-8859-1?Q?what's_"weakness"_in_elisp's_hash_table??= Tim X
0 siblings, 2 replies; 3+ messages in thread
From: Xah Lee @ 2008-01-03 16:43 UTC (permalink / raw)
To: help-gnu-emacs
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??
Xah
xah@xahlee.org
\xAD\xF4 http://xahlee.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: what's "weakness" in elisp's hash table?
2008-01-03 16:43 what's "weakness" in elisp's hash table? Xah Lee
@ 2008-01-03 21:15 ` Tassilo Horn
2008-01-06 9:45 ` =?ISO-8859-1?Q?what's_"weakness"_in_elisp's_hash_table??= Tim X
1 sibling, 0 replies; 3+ messages in thread
From: Tassilo Horn @ 2008-01-03 21:15 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: =?ISO-8859-1?Q?what's_"weakness"_in_elisp's_hash_table??=
2008-01-03 16:43 what's "weakness" in elisp's hash table? Xah Lee
2008-01-03 21:15 ` Tassilo Horn
@ 2008-01-06 9:45 ` Tim X
1 sibling, 0 replies; 3+ messages in thread
From: Tim X @ 2008-01-06 9:45 UTC (permalink / raw)
To: help-gnu-emacs
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??
>
The entry with key 'mary' and value 19 will be removed from the hash table
if it has no references i.e. it will be garbage collected. Normally, with
:weakness set to nil, keys and values in a hash table are protected from
garbage collection.
--
tcross (at) rapttech dot com dot au
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-01-06 9:45 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-03 16:43 what's "weakness" in elisp's hash table? Xah Lee
2008-01-03 21:15 ` Tassilo Horn
2008-01-06 9:45 ` =?ISO-8859-1?Q?what's_"weakness"_in_elisp's_hash_table??= Tim X
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.