From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: roessler@rftp.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Runtime string allocation/resizing
Date: Wed, 22 Jun 2005 09:07:27 +0900 (JST) [thread overview]
Message-ID: <20050622.090727.59467363.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <42B87C09.3020008@rftp.com>
From: Robert Roessler <roessler@rftp.com>
> When dealing with OCaml-to-C-land interfacing, I sometimes am in the
> position of needing to allocate and fill a string - but only have an
> upper bound for the length... until after I have already copied the
> data [from a third-party library function].
>
> What is the recommended "OCaml" way to deal with this?
>
> Should I do two caml_alloc_string calls? One to initially "get" the
> data, and then alloc a second one and copy to it after I know the
> correct length?
>
> Or would it be considered better practice to use malloc in the C heap
> for the "temp" copy and only do the caml_alloc_string when I know the
> exact size?
>
> Or ?
If your goal is raw performance, then probably the fastest approach
would be to use alloca(). Unfortunately, this is not very portable.
Using malloc() is not a very good idea either, as some implementations
are really slow. So I would recommend using caml_alloc_string twice.
Allocation costs amost nothing, and the added GC cost is really
minimal. (But maybe GC specialists know better.)
I you have a global bound on the size of the string, a static buffer
is of course the best solution (but I suppose you would not have the
question then.)
Jacques
prev parent reply other threads:[~2005-06-22 0:07 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-06-21 20:43 Robert Roessler
2005-06-21 21:49 ` [Caml-list] " Stephane Glondu
2005-06-22 0:07 ` Jacques Garrigue [this message]
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=20050622.090727.59467363.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@inria.fr \
--cc=roessler@rftp.com \
/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