potential speed improvement for repeated string concatenation

janderson askme at me.com
Fri Jul 27 09:18:51 PDT 2007


downs wrote:
> 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.
> I noticed this while trying to figure out why my serialization code for 
> YAWR was so atrociously slow. I'm passing the ser method a void 
> delegate(char[]) that it's supposed to call with the strings it 
> serializes, in order. So normally, serialization would look like this:
> 
> char[] buffer; ser(data, (char[] f) { buffer~=f; });
> 
> When I figured out that it was the primarily bottleneck that caused the 
> delays while saving, I replaced it with the following code
> 
> size_t len=0;
> ser(data, (char[] f) { len+=f.length; });
> auto buffer=new char[len]; len=0;
> ser(data, (char[] f) { buffer[len..len+f.length]=f; len+=f.length; }
> 
> To my surprise, this more than doubled the speed of that code. So if you 
> have some block of code that does lots of repeated string concats, you 
> might want to give this a try.
>  --downs

You can do this as well:

void reserve(T)(T[] array, int reserve)
{
	array.length += reserve;
	array.length = 0;
}

That way you can use char[] like normal.



More information about the Digitalmars-d mailing list