potential speed improvement for repeated string concatenation

BCS ao at pathlink.com
Tue Jul 24 13:08:34 PDT 2007


Reply to Downs,

> Here's something interesting I noticed while writing serialization
> code.
> If you're in a situation where you have to repeatedly append small to
> medium sized chunks of string
> to a buffer, and the computation of those chunks is relatively cheap,
> it might be faster (and use
> less memory) to do it in two passes: first, determining the size of
> the result string, then
> allocating it completely and filling it in.


For cases like this, it would be nice to have a VirtualArray type

class VArray(T)
{
  opSliceAssign(T[], size_t, size_t);
  opSliceAssign(T[]);
  T[] opSlice(size_t, size_t);
  T[] opSlice();
  offset(int);
  offsetByLast();
}

This would act as an array that is as big as it needs to be and where the 
beginning can be hidden.

Then this would work

auto a = new VArray!(char);
foreach(char[] s; arrayOfStrings)
{
  a[] = s; // copy in starting at "0";
  a.offsetByLast
}
return a[];

Inside this would do copies and reallocates while trying to not keep the 
number of the second down;





More information about the Digitalmars-d mailing list