std.stringbuffer
Sean Kelly
sean at invisibleduck.org
Wed Apr 30 07:19:36 PDT 2008
== Quote from Me Here (p9e883002 at sneakemail.com)'s article
> My point was that /if/ Ds arrays have a similar capability, to be
> preallocated large and empty and grow into the space then
> when a mutation requires a reallocation of a mutable array because it has
> outgrown its original allocation,
> then a debug-enabled warning saying by how much, might allow the
> programmer to preallocate the initial mutable array larger and
> so avoid reallocation at runtime.
D arrays do have this feature, thanks to a suggestion by Derek Parnell. That is,
reducing the array's length property does not cause a reallocation, even when
length is set to zero. Thus it is possible to do:
void fn( inout char[] buf )
{
buf.length = 1024; // preallocate 1024 bytes of storage
buf.length = 0;
buf ~= "hello"; // will copy into preallocated buffer
}
Thus the proper way to discard a buffer is to do:
buf = null;
I think for specific buffers it's probably enough to print their length when you're
done filling them and then explicitly preallocate the next run based on this info.
Tango also offers a means of performing program-level preallocation via GC.reserve()
for people so inclined.
Sean
More information about the Digitalmars-d
mailing list