caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Till Varoquaux" <till.varoquaux@gmail.com>
To: "Richard Jones" <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Weak hash table for attaching extra data to an object
Date: Tue, 14 Aug 2007 13:48:17 +0200	[thread overview]
Message-ID: <9d3ec8300708140448u4e5664cs4aa0bdc149a9c048@mail.gmail.com> (raw)
In-Reply-To: <46C18D1B.2070303@functionality.de>

I have a small library that could do the trick. It is built a a
hashtable whose keys  are weak, you need to recompact it from time to
time in order to free associated values. One strategy would be to
recompact when n values are freed.
I tried to limit the performance cost by storing collision values in a
separate table.
The code can be browsed online at:
http://till.varoquaux.free.fr/projects/mini_js/WeakHt.ml.html
There's a very small dependency on another file, i use Option.get
(defined as in extlib:
  let get = function
   | Some v -> v
   | None -> raise No_value
)
It is licensed under lgpl+special linking clause. If you need a more
permissive license let me know.
This can be downloaded via darcs:
darcs get http://till.varoquaux.free.fr/projects/mini_js/WeakHt.ml.html
or directly at:
http://till.varoquaux.free.fr/projects/mini_js/WeakHt.ml
There no mli file (I'm lazy).
Constructive criticism is welcome.
If you need automatic collection of values and have a hard time
implementing I can work on it.
Cheers,
Till
P.S.: I wasn't aware of HWeak when I did this library, I don't know
how it compares performance wise. Having never had to profile this
library I actually don't know how bad it really is. This can certainly
also be worked on.
On 8/14/07, Thomas Fischbacher <tf@functionality.de> wrote:
>
> Richard Jones wrote:
>
> > I have a module which I can't edit[1].  This module, let's call it
> > Document, stores a representation of an XML tree in internal node
> > objects:
> >
> >   type intnode
> >
> > What I want to do is -- completely outside Document -- attach my own
> > extra information to these nodes.
>
> Ah, so the situation really is somewhat common, and not something
> just I am running into occasionally. (The problem in particular
> also arises occasionally when writing symbolic algebra code when one
> wants to attach meta-data (e.g. on typesetting) to terms without
> touching the underlying term representations in any way.)
>
> > The key point is that if the
> > garbage collector collects an intnode, then I want my extra
> > information to be garbage collected too.
> >
> > I wonder if anyone has done this, or can suggest a better way to solve
> > this problem?  The issue of attaching extra data to some existing
> > object would seem to be a fairly common one ...
>
> You might be able to "fake" a weak hash table by using a trick: for a
> key-value entry k1 -> v1, you take k1 and v1 and rebuild the outermost
> data structure to get k2 with k2 = k1 and k2 != k1 (oh hey, now that
> looks confusing, right?), as well as v2 = v1 and v2 != v1. Now, you
> enter k2 -> v2 into an ordinary hash table and register finalizers for
> k1 and v1 which remove this k2 -> v2 entry from your hash. That might
> do.
>
> --
> best regards,
> Thomas Fischbacher
> tf@functionality.de
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


-- 
http://till-varoquaux.blogspot.com/


  reply	other threads:[~2007-08-14 11:48 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-14 10:15 Richard Jones
2007-08-14 11:08 ` [Caml-list] " Thomas Fischbacher
2007-08-14 11:48   ` Till Varoquaux [this message]
2007-08-14 20:44   ` Jon Harrop
2007-08-14 23:09     ` Stefano Zacchiroli
2007-08-15  0:33       ` Jon Harrop
2007-08-15 12:33         ` Daniel Bünzli
2007-08-16 14:54         ` Markus Mottl
2007-08-15  1:28     ` skaller
2007-08-15 19:04     ` Richard Jones
2007-08-16 16:17       ` Some observations and measurements with using Weak Hashtable to annotate a tree (was: Re: [Caml-list] Weak hash table for attaching extra data to an object) Richard Jones
2007-08-14 16:22 ` [Caml-list] Weak hash table for attaching extra data to an object Richard Jones
2007-08-14 23:24   ` Till Varoquaux
2007-08-14 23:35   ` Fernando Alegre
2007-08-15  7:59     ` Richard Jones

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=9d3ec8300708140448u4e5664cs4aa0bdc149a9c048@mail.gmail.com \
    --to=till.varoquaux@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.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.
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 NNTP newsgroup(s).