all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tassilo@member.fsf.org>
To: help-gnu-emacs@gnu.org
Subject: Re: what's "weakness" in elisp's hash table?
Date: Thu, 03 Jan 2008 22:15:58 +0100	[thread overview]
Message-ID: <87y7b6mwz5.fsf@member.fsf.org> (raw)
In-Reply-To: be001af5-0978-48c1-975e-2fe461f712e3@s8g2000prg.googlegroups.com

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	other threads:[~2008-01-03 21:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-03 16:43 what's "weakness" in elisp's hash table? Xah Lee
2008-01-03 21:15 ` Tassilo Horn [this message]
2008-01-06  9:45 ` =?ISO-8859-1?Q?what's_"weakness"_in_elisp's_hash_table??= Tim X

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y7b6mwz5.fsf@member.fsf.org \
    --to=tassilo@member.fsf.org \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.