caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Runtime string allocation/resizing
@ 2005-06-21 20:43 Robert Roessler
  2005-06-21 21:49 ` [Caml-list] " Stephane Glondu
  2005-06-22  0:07 ` Jacques Garrigue
  0 siblings, 2 replies; 3+ messages in thread
From: Robert Roessler @ 2005-06-21 20:43 UTC (permalink / raw)
  To: Caml-list

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 ?

BTW, I am more concerned with longer-term issues like memory 
fragmentation and "extra" [minor] collections than with raw speed (as 
in copying the data twice being "evil").

Robert Roessler
roessler@rftp.com
http://www.rftp.com


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Runtime string allocation/resizing
  2005-06-21 20:43 Runtime string allocation/resizing Robert Roessler
@ 2005-06-21 21:49 ` Stephane Glondu
  2005-06-22  0:07 ` Jacques Garrigue
  1 sibling, 0 replies; 3+ messages in thread
From: Stephane Glondu @ 2005-06-21 21:49 UTC (permalink / raw)
  To: caml-list

On Tuesday 21 June 2005 13:43, Robert Roessler wrote:
> [...]
>
> 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?

The second one is clearly better since the temp copy won't be used by the 
OCaml part. However, maybe there is a better alternative (for example, 
directly allocating in OCaml heap, then setting the string length so that the 
"extra" part is ignored).

Stephane Glondu.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Runtime string allocation/resizing
  2005-06-21 20:43 Runtime string allocation/resizing Robert Roessler
  2005-06-21 21:49 ` [Caml-list] " Stephane Glondu
@ 2005-06-22  0:07 ` Jacques Garrigue
  1 sibling, 0 replies; 3+ messages in thread
From: Jacques Garrigue @ 2005-06-22  0:07 UTC (permalink / raw)
  To: roessler; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-06-22  0:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-21 20:43 Runtime string allocation/resizing Robert Roessler
2005-06-21 21:49 ` [Caml-list] " Stephane Glondu
2005-06-22  0:07 ` Jacques Garrigue

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).