caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Warren Harris <warrensomebody@gmail.com>
To: "CUOQ Pascal - Pascal.CUOQ@cea.fr"
	<+caml+warren+5dabfc3456.Pascal.CUOQ#cea.fr@spamgourmet.com>
Cc: <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] RE: understanding weak
Date: Thu, 30 Oct 2008 12:35:38 -0700	[thread overview]
Message-ID: <8C745AFA-4FC2-4574-AB97-98321ECC4964@gmail.com> (raw)
In-Reply-To: <5EFD4D7AC6265F4D9D3A849CEA9219191AB114@LAXA.intra.cea.fr>


On Oct 30, 2008, at 11:48 AM, CUOQ Pascal - Pascal.CUOQ@cea.fr wrote:

>
> Warren Harris <warren@metaweb.com> wrote:
>> I'd like to understand better how ocaml's weak pointers operate.
>
> You will be interested in the following important article:
> http://portal.acm.org/citation.cfm?id=1411308
> :)

Thank you for the reference. Looks like the paper's timing couldn't be  
better.

>
>
>> First, although it doesn't seem to be specified in the documentation,
>> I assume that weak pointers will *not* be reclaimed (e.g. from a weak
>> hash table) if the program retains some other reference to the  
>> object.
>
> Exactly.
>
>> My second question relates specifically to my application. I would
>> like to have a primary cache of objects, and a secondary index into
>> sub-objects referenced from the primary cache. I.e. CacheA references
>> objects of type A; objects of type A reference objects of type B;
>> CacheB references objects of type B. I would like to guarantee that
>> weak references in CacheB are not flushed unless the corresponding
>> reference from CacheA is first flushed. I assume will be the case  
>> if a
>> non-weak reference from A to B is maintained.
>
> The non-weak reference from A to B prevents B being unreachable
> without A being unreachable, so yes, a reference from CacheB can
> not disappear without the reference from CacheA disappearing
> earlier or simultaneously.
> This said, if what you want is really a cache, you would probably be
> better off fixing its size and renewal strategy yourself than letting
> the GC do it for you (by using weak pointers). What it will do has  
> almost
> no chance of being the best compromise between memory use and
> speed for your application, and it may change without notice from
> one version to the other. In short: don't use weak pointers to make  
> caches.

Thanks for the advice -- but I thought this was exactly what weak hash  
tables were intended for. (The paper seems to indicate that too.) I  
realize that an explicit replacement policy can be more application- 
friendly, but if what you really want is just "unless  anyone is using  
this" then using weak hash tables would seem appropriate.

>
>
>> Can anyone verify?
>
> If you want to experiment to confirm your impressions, the function
> Gc.full_major is your friend.

I'd hate to do an empirical analysis of this only to find I hadn't  
uncovered the full truth once the app was in production... so asking  
seemed like the way to go. Seems like the manual should say more here.

Warren



  parent reply	other threads:[~2008-10-30 19:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20081030182019.EEBC5BBB7@yquem.inria.fr>
2008-10-30 18:48 ` CUOQ Pascal
2008-10-30 19:12   ` [Caml-list] " Daniel Bünzli
2008-10-30 19:35   ` Warren Harris [this message]
2008-10-30 23:06     ` Alain Frisch
2008-10-31  8:33       ` Rémi Vanicat
2008-11-01  1:08   ` [Caml-list] " Jon Harrop
2008-11-01 10:37     ` Stefano Zacchiroli
2008-11-01 15:18       ` kirillkh

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=8C745AFA-4FC2-4574-AB97-98321ECC4964@gmail.com \
    --to=warrensomebody@gmail.com \
    --cc=+caml+warren+5dabfc3456.Pascal.CUOQ#cea.fr@spamgourmet.com \
    --cc=caml-list@yquem.inria.fr \
    /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).