[plt-scheme] Weak hash tables

From: ifconfig nslookup (configurator at gmail.com)
Date: Thu Sep 9 16:31:53 EDT 2004

[First sent to wrong adress]

I wonder how much memory the buckets reserve?
Is there a way to set the weak-hash-tables (and normal hash-tables on
delete) to actually remove the buckets?
I have seen code that operates like this, at work:
A main hash-table is created to contain all keys, which are every
first name and every last name of every client in a database
(approximately 40,000 keys in an average customer's database).
Then, that hash-table is added the keys "A".."Z", and each is
populated with - guess what - a hash-table, that contains every name
without its first character.
You can pretty much guess the rest.
In the end, every table is left with up to 26 keys. But for some
reason, these tables are all first populated with their entire list,
and only then broken down into subtables, contained in the same
hash-table (I would use a new one).
In this hash-table or weak-hash-table, this would probably take up
very much space. As the average customer name is 6 characters, we can
calculate a total of 240,000 empty buckets.
So is there a way to cause non-reservance of empty buckets?


On Thu, 9 Sep 2004 11:24:48 -0600, Matthew Flatt <mflatt at cs.utah.edu> wrote:
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> At Thu, 9 Sep 2004 10:06:24 -0700, Don Felgar wrote:
> > I presume this is an artifact of conservative gc.
> More or less. Try
> (define wht (make-hash-table 'weak))
> (define strong "bar")
> (hash-table-put! wht 'strong strong)
> (let loop ()
>  (hash-table-put! wht (string->symbol (number->string (random 5000000)))
>                   "value")
>  (collect-garbage)
>  (printf "~a~n" (hash-table-count wht))
>  (loop)))
> > I wrote the above test to determine if a weak hash table shrinks
> > during gc, as opposed to accumulating k/v pairs with a #f value.
> It shrinks.
> (Actually, internally, it merely avoids growing. If you put 5000 things
> into a weak hash table and they all go away, then you're left with 5000
> buckets internally, while `hash-table-count' claims 0. If you put 200
> things back in, 200 buckets get recycled, but you're still stuck with
> 4800 empty buckets.)
> Matthew

Posted on the users mailing list.