From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 23728BBAF for ; Tue, 24 Aug 2010 17:22:52 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApsEAFp+c0xZELGa/2dsb2JhbAChMbsUgnWCQgSJeA X-IronPort-AV: E=Sophos;i="4.56,263,1280700000"; d="scan'208";a="55970678" Received: from recoil.dh.bytemark.co.uk (HELO dark.recoil.org) ([89.16.177.154]) by mail3-smtp-sop.national.inria.fr with SMTP; 24 Aug 2010 17:22:51 +0200 Received: (qmail 1603 invoked by uid 634); 24 Aug 2010 15:22:51 -0000 X-Spam-Level: * X-Spam-Check-By: dark.recoil.org Received: from no-dns-yet.demon.co.uk (HELO [192.168.14.21]) (62.49.66.12) (smtp-auth username remote@recoil.org, mechanism cram-md5) by dark.recoil.org (qpsmtpd/0.83) with ESMTPA; Tue, 24 Aug 2010 16:22:50 +0100 Subject: Re: [Caml-list] caml_copy_string Mime-Version: 1.0 (Apple Message framework v1081) Content-Type: text/plain; charset=iso-8859-1 From: Anil Madhavapeddy In-Reply-To: Date: Tue, 24 Aug 2010 16:22:48 +0100 Cc: Florent Monnier , caml-list@yquem.inria.fr Content-Transfer-Encoding: quoted-printable Message-Id: References: <201008231409.06024.monnier.florent@gmail.com> <201008231524.49124.toots@rastageeks.org> <201008241621.02363.monnier.florent@gmail.com> To: Till Varoquaux X-Mailer: Apple Mail (2.1081) X-Virus-Checked: Checked by ClamAV on dark.recoil.org X-Spam: no; 0.00; anil:01 anil:01 ocaml:01 buffer:01 allocations:01 buffer:01 segfault:01 iirc:01 ocaml:01 runtime:01 allocating:01 runtime:01 ocamlopt:01 pointer:01 allocates:01 On 24 Aug 2010, at 15:52, Till Varoquaux wrote: > On Tue, Aug 24, 2010 at 10:21 AM, Florent Monnier > wrote: >> Le lundi 23 ao=FBt 2010 22:24:48, Romain Beauxis a =E9crit : >>> Le lundi 23 ao=FBt 2010 07:09:05, Florent Monnier a =E9crit : >>>> 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, >>>=20 >>>> 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. >>=20 >> could you develop? which cases? >=20 > 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. >=20 > Explanation: ocamlopt-generated code caches in registers some global > variables of importance to the OCaml runtime system, such as the > current allocation pointer. >=20 > 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). >=20 > 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. >=20 > 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". >=20 [1] = http://caml.inria.fr/pub/ml-archives/caml-list/2010/03/2e386c384c04f4a424a= cc44a1fae3abd.en.html -anil