caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Thomas Crimi <tcrimi@gmail.com>
To: Sean Seefried <sean.seefried@nicta.com.au>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Heaps size problems with "caml_alloc_small" in foreign function interfaces
Date: Sun, 13 Jul 2008 10:03:04 -0400	[thread overview]
Message-ID: <3898DD39-12CD-4957-9165-CF437685E3F2@gmail.com> (raw)
In-Reply-To: <50710496-7007-4A9C-BFA4-DB98EEAD360E@nicta.com.au>

On Jul 13, 2008, at 9:25 AM, Sean Seefried wrote:

>
> So now I have a question. Is there any way that I can find out what  
> address the garbage collector moves these values to? I need to  
> update the table when this happens.

  You have to register each of your pointers as global roots, but  
beware that doing so for a large number of objects can dramatically  
slow down the GC since all the roots must be scanned on each minor  
collection.  The main issue here is that the OCaml run-time can't tell  
if you're modifying root pointers behind its back;  pointing into the  
young generation, so each scan of the young generation needs to first  
check all root pointers.  It should be possible to better optimize  
this by having the C code promise not to do this without telling the  
GC, but such functions currently don't exist.

  A solution I've used in the past (surely obtained from this list) is  
to keep the Hashtable in OCaml, and register some functions that the C  
code can use to access it.  The C code can refer to objects long-term  
by integer ID which can be used for fast lookups.

  When you need the object, look it up by integer id to get the  
pointer.  Then, of course, register the pointer as a global root.   
Once your done with the object pointer you release the global root  
again (or just keep a single always-registered global root around and  
re-point it to each object you need).

   This keeps the number of global roots to a minimum but still allows  
fast access to the pointers.

   Generally in my code I've created C++ wrapper classes to wrap a  
'val' and provide C++-object-like access.  It's very easy to have a  
base class that properly manages root allocation / release and then  
never have to think about it again.  GC errors like this are insidious  
since your program can run for weeks without issue then one particular  
data-set causes a GC at the inopportune time and you have a mysterious  
crash.

   I'm not familiar with CamlIDL, so I'm not sure how amenable these  
tricks are.

  Regards,
   Tom


  parent reply	other threads:[~2008-07-13 14:03 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-12  6:07 Sean Seefried
2008-07-12  6:09 ` Sean Seefried
2008-07-12  8:50 ` Richard Jones
2008-07-13 13:25   ` Sean Seefried
2008-07-13 13:37     ` Gordon Henriksen
2008-07-13 14:03     ` Thomas Crimi [this message]
  -- strict thread matches above, loose matches on Subject: below --
2008-07-11  8:21 Sean Seefried
2008-07-11  9:40 ` [Caml-list] " Richard Jones
2008-07-11  9:51   ` Richard Jones
2008-07-11 10:21   ` Sean Seefried
2008-07-11 15:56     ` Richard Jones
2008-07-11 14:11 ` Xavier Leroy
2008-07-12  3:20   ` Sean Seefried
2008-07-12  6:00   ` Sean Seefried

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=3898DD39-12CD-4957-9165-CF437685E3F2@gmail.com \
    --to=tcrimi@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=sean.seefried@nicta.com.au \
    /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).