From: Florent Monnier <monnier.florent@gmail.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] caml_copy_string
Date: Mon, 23 Aug 2010 14:09:05 +0200 [thread overview]
Message-ID: <201008231409.06024.monnier.florent@gmail.com> (raw)
In-Reply-To: <AANLkTi=vo41hxuw_uwtCVD6YMHeUBfBR+YYNCxKrCjU0@mail.gmail.com>
Le dimanche 22 août 2010 01:30:28, Jeffrey Barber a écrit :
> Is there a way to get a string from C to OCaml without the caml_copy_string
> 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 this
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
========================
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/fail.h>
static const char *str = "the walking camel";
static const int len = 18;
CAMLprim value ml_mystr1(value unit) {
return caml_copy_string(str);
}
CAMLprim value ml_mystr2(value ml_buf) {
int buf_len = 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 */
}
========================
external mystr1: unit -> string = "ml_mystr1"
external mystr2: string -> int = "ml_mystr2" "noalloc"
let n = 1_000_000_000
let test1() =
for i = 1 to n do
let _ = mystr1 () in ()
done
let test2() =
let buf = String.create 100 in
for i = 1 to n do
let _ = mystr2 buf in ()
done
let () =
match Sys.argv.(1) with
| "1" -> test1()
| "2" -> test2()
| _ -> assert false
========================
--
Regards
Florent
next prev parent reply other threads:[~2010-08-23 12:09 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 [this message]
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
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=201008231409.06024.monnier.florent@gmail.com \
--to=monnier.florent@gmail.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