unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Mikael Djurfeldt <djurfeldt@nada.kth.se>
Cc: djurfeldt@nada.kth.se
Subject: Re: weak key hash versus display
Date: Thu, 30 Jan 2003 10:27:31 +0100	[thread overview]
Message-ID: <xy7fzrbgf7g.fsf@nada.kth.se> (raw)
In-Reply-To: <877kcn5yqc.fsf@zip.com.au> (Kevin Ryde's message of "Thu, 30 Jan 2003 09:21:15 +1000")

Kevin Ryde <user42@zip.com.au> writes:

> In guile 1.6.1 or the cvs on a recent i386 debian, I noticed that
> doing a display of a weak key hash table can seemingly prevent a key
> from being garbage collected.  For instance
>
> 	(define h (make-weak-key-hash-table 7))
>
> 	(define k "mykey")
> 	(hash-set! h (string-copy k) 12345)
>
> 	(display (hash-ref h k)) (newline)
> 	(display h) (newline)
> 	(gc)
> 	(display (hash-ref h k)) (newline)
>
> run with "guile -s foo.scm" produces
>
> 	12345
> 	#wh(() () () () () ((mykey . 12345)) ())
> 	12345
>
> whereas I might have expected the gc to have collected the entry just
> set, making the second hash-ref give #f rather than 12345.  This is
> what happens if the (display h) is not present.

And if you keep (display h) but add

  (display <some deep list-structure>),
  
hash-ref will give #f again.

The reason is that in order to avoid infinite loops due to circular
data-structures print functions need to keep track of printed
references.  These are stored in a "print-state" attached to the
port.  After displaying h, the print-state contains the reference to
the pair (mykey . 12345) which protects it from GC.

We should probably replace this vector with a weak vector, or (perhaps
more efficient) clear the references from the vector.

> I don't really know if this is a bug, or ignorance on my part, but it
> seemed more than a little strange.

Well, it's not really a bug.  The Guile GC doesn't make any guarantees
that objects will get GC:d.  For example, if the C stack happens to
contain an integer which happens to coincide with a reference on the
heap, that object won't get GC:d.  A conservative GC only behaves
nicely in a statisticial sense.

Thanks for your observation.

Best regards,
Mikael Djurfeldt


_______________________________________________
Bug-guile mailing list
Bug-guile@gnu.org
http://mail.gnu.org/mailman/listinfo/bug-guile


  reply	other threads:[~2003-01-30  9:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-29 23:21 weak key hash versus display Kevin Ryde
2003-01-30  9:27 ` Mikael Djurfeldt [this message]
2003-01-31 21:56   ` Kevin Ryde

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

  List information: https://www.gnu.org/software/guile/

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

  git send-email \
    --in-reply-to=xy7fzrbgf7g.fsf@nada.kth.se \
    --to=djurfeldt@nada.kth.se \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).