caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Anil Madhavapeddy <anil@recoil.org>
To: Till Varoquaux <till@pps.jussieu.fr>
Cc: Florent Monnier <monnier.florent@gmail.com>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] caml_copy_string
Date: Tue, 24 Aug 2010 16:22:48 +0100	[thread overview]
Message-ID: <D122405E-9F3E-4488-BDC7-B495D020AF86@recoil.org> (raw)
In-Reply-To: <AANLkTi=X5_dgmjtyH8A=rOfHELigTkdZ_W3iw4PfkzHL@mail.gmail.com>

On 24 Aug 2010, at 15:52, Till Varoquaux wrote:

> On Tue, Aug 24, 2010 at 10:21 AM, Florent Monnier
> <monnier.florent@gmail.com> wrote:
>> Le lundi 23 août 2010 22:24:48, Romain Beauxis a écrit :
>>> Le lundi 23 août 2010 07:09:05, Florent Monnier a écrit :
>>>> an alternative method is to provide a string from ocaml to c then c fills
>>>> this  buffer, then you can save allocations by reusing the same buffer,
>>> 
>>>> see:
>>> This is a good idea but I would be a little bit suspicious about using
>>> "noalloc". Even if it works in your tests, this options is very delicate to
>>> use with the Gc, and may segfault in some cases.
>> 
>> could you develop? which cases?
> 
> IIRC noalloc calls release the ocaml lock. This means that the runtime
> can run at the same time as the c call and the GC migh collect your
> string or move it along. Quick rule if thumb is: you are allocating
> your string so you should not use noalloc.

That's not quite right; "noalloc" calls do not have the OCaml runtime in a functioning state at all since the instructions to set it up are not emitted by ocamlopt.

See [1] for Xavier Leroy's explanation on the matter, which I've quoted below:

> Yes.  Actually, it is forbidden to call any function of the OCaml
> runtime system from a noalloc function.
> 
> Explanation: ocamlopt-generated code caches in registers some global
> variables of importance to the OCaml runtime system, such as the
> current allocation pointer.
> 
> When calling a regular (no-"noalloc") C function from OCaml, these
> global variables are updated with the cached values so that everything
> goes well if the C function allocates, triggers a GC, or releases the
> global lock (enabling a context switch).
> 
> This updating is skipped when the C function has been declared
> "noalloc" -- this is why calls to "noalloc" functions are slightly
> faster.  The downside is that the runtime system is not in a
> functioning state while within a "noalloc" C function, and must
> therefore not be invoked.
> 
> The cost of updating global variables is small, so "noalloc" makes
> sense only for short-running C functions (say, < 100 instructions) like
> those from the math library (sin, cos, etc).  If the C function makes
> significant work (1000 instructions or more), just play it safe and
> don't declare it "noalloc".
> 

[1] http://caml.inria.fr/pub/ml-archives/caml-list/2010/03/2e386c384c04f4a424acc44a1fae3abd.en.html

-anil


  reply	other threads:[~2010-08-24 15:22 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-21 23:30 caml_copy_string Jeffrey Barber
2010-08-21 23:42 ` [Caml-list] caml_copy_string Romain Beauxis
2010-08-21 23:46 ` Mathias Kende
2010-08-22 17:16   ` Till Varoquaux
2010-08-23  0:42     ` Till Varoquaux
2010-08-23  1:02       ` Jeffrey Barber
2010-08-23 12:09 ` Florent Monnier
2010-08-23 12:59   ` Stéphane Glondu
2010-08-23 13:46     ` Florent Monnier
2010-08-23 20:24   ` Romain Beauxis
2010-08-24 14:21     ` Florent Monnier
2010-08-24 14:52       ` Till Varoquaux
2010-08-24 15:22         ` Anil Madhavapeddy [this message]
2010-08-24 15:35           ` Romain Beauxis
2010-08-25 19:16             ` Florent Monnier
2010-08-25 19:33               ` Romain Beauxis
2010-08-25 15:21   ` Goswin von Brederlow
  -- strict thread matches above, loose matches on Subject: below --
2005-10-29  0:24 Jonathan Roewen
2005-10-29  0:32 ` Robert Roessler

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=D122405E-9F3E-4488-BDC7-B495D020AF86@recoil.org \
    --to=anil@recoil.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=monnier.florent@gmail.com \
    --cc=till@pps.jussieu.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).