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 2B2B3BBAF for ; Mon, 23 Aug 2010 14:09:14 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmMBAEMBckxKfVK0kGdsb2JhbACTJY0DCBUBAQEBCQkMBxEDH54niRiCE4VyLohUAQEDBYUyBIl2 X-IronPort-AV: E=Sophos;i="4.56,257,1280700000"; d="scan'208";a="55912939" Received: from mail-wy0-f180.google.com ([74.125.82.180]) by mail3-smtp-sop.national.inria.fr with ESMTP; 23 Aug 2010 14:09:13 +0200 Received: by wya21 with SMTP id 21so7858910wya.39 for ; Mon, 23 Aug 2010 05:09:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:subject:date :user-agent:references:in-reply-to:mime-version:message-id :content-type:content-transfer-encoding; bh=gvEkiBJUn7YFim3+vfeHRfxAWPGtKnDrNCBPihLyaJI=; b=CPsRiGVrjUTK6kt+Prar7ghib+3SAnKQMpmu3U6Rwn/VSLehNE6ZFkI1dQdx60tQSA wDHOY/c/b5kXn4EqOT0luzDqbwUQ6SHbYi260yhjaNBNhzAug79xdMjielCJ26pmaNyZ HQBuIOA0aEclU4LBiCPMYqhzLiXxy+8wjTPPs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:subject:date:user-agent:references:in-reply-to:mime-version :message-id:content-type:content-transfer-encoding; b=Kx7A+K1r9wjOvaqQmJO6CZle+awb1UryvLKQdrNzp99QXLatqeMbuKMBP0hYVdOwMs 4J+mV7KP5uskmtnFyIwocZdinB3dQ/dJe/3Sq31H4EByLdJhLcCNt07+dijyXQ1ii0Mb X2GtoPDpLIAzD8LdaPrOzkPC+SdCkBXxlmtNg= Received: by 10.227.153.211 with SMTP id l19mr4042565wbw.224.1282565353678; Mon, 23 Aug 2010 05:09:13 -0700 (PDT) Received: from localhost.localnet (pat35-5-78-226-58-147.fbx.proxad.net [78.226.58.147]) by mx.google.com with ESMTPS id v44sm3589956weq.28.2010.08.23.05.09.12 (version=SSLv3 cipher=RC4-MD5); Mon, 23 Aug 2010 05:09:13 -0700 (PDT) From: Florent Monnier To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] caml_copy_string Date: Mon, 23 Aug 2010 14:09:05 +0200 User-Agent: KMail/1.13.3 (Linux/2.6.33.5-desktop586-2mnb; KDE/4.4.3; i686; ; ) References: In-Reply-To: MIME-Version: 1.0 Message-Id: <201008231409.06024.monnier.florent@gmail.com> Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; ocaml:01 ocaml:01 buffer:01 allocations:01 buffer:01 ocamlopt:01 stub:01 mlvalues:01 alloc:01 const:01 const:01 camlprim:01 mystr:01 camlprim:01 mystr:01 Le dimanche 22 ao=FBt 2010 01:30:28, Jeffrey Barber a =E9crit : > Is there a way to get a string from C to OCaml without the caml_copy_stri= ng > function, or is there a version that doesn't copy the string? an alternative method is to provide a string from ocaml to c then c fills t= his=20 buffer, then you can save allocations by reusing the same buffer, see: ocamlopt -o test.opt main.ml main_stub.c $ time ./test.opt 1 44 seconds elapsed $ time ./test.opt 2 21 seconds elapsed =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #include #include #include #include static const char *str =3D "the walking camel"; static const int len =3D 18; CAMLprim value ml_mystr1(value unit) { return caml_copy_string(str); } CAMLprim value ml_mystr2(value ml_buf) { int buf_len =3D caml_string_length(ml_buf); if (len > buf_len) caml_failwith("buffer overflow"); memcpy(String_val(ml_buf), str, len); return Val_int(len); /* len chars were written in the buffer */ } =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D external mystr1: unit -> string =3D "ml_mystr1" external mystr2: string -> int =3D "ml_mystr2" "noalloc" let n =3D 1_000_000_000 let test1() =3D for i =3D 1 to n do let _ =3D mystr1 () in () done let test2() =3D let buf =3D String.create 100 in for i =3D 1 to n do let _ =3D mystr2 buf in () done let () =3D match Sys.argv.(1) with | "1" -> test1() | "2" -> test2() | _ -> assert false =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =2D-=20 Regards =46lorent