[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2005-06-22 (00:07) |
From: | Jacques Garrigue <garrigue@m...> |
Subject: | Re: [Caml-list] Runtime string allocation/resizing |
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