Asking about performance of std concatenation vs. Appender vs. custom class

ludo fakeaddress at gmail.com
Fri Apr 9 13:07:39 UTC 2021


> reserving cuts down on the reallocations, but that only takes 
> some of the time. Appending a 1000-element int array is going 
> to go from a 16-byte block, to a 32-byte block, etc. up to a 
> 4096 byte block. This involves roughly 8 reallocations per test.
>
> But every append requires an opaque function call into the 
> runtime to check if the allocated length is big enough, and 
> reallocate if necessary.
>
> So if the reallocations aren't too expensive, then appending 
> performance would not be too much different.

For ArrayBuilder, the function call to check length is there too, 
it is the grow() function. Sure reallocations take time. But the 
comment of the class talks about an increased performance thanks 
to *the base pointer being 4 system words instead of two*

What did the dev mean by that?

> Yes, because Appender has direct access to the allocated 
> length, rather than having to go through an opaque call.
>
Alright, ArrayBuilder definitely benefits from the same.

>
> This I have no idea on. There are probably a lot of 
> explanations that could be true. Have you tried profiling the 
> code?

I just tried to run dub test --build=profile on dterrent, but I 
get some obscure error message. Not working. Also tried to dmd 
-unittest -profile arraybuilder.d with an empty
main(), not working, I don't get a trace.log. Am I supposed to 
create a main() test program to have access to profiling, that is 
to say there is no possible profiling of unittest?

>
> Are you compiling with -inline -O?
>
> -Steve

-inline does not work (segfault) but optimize does not change 
anything except for stdLength (10ms) and ... ArrayBuilder, which 
gain even more performance (90ms).

Conclusion, ArrayBuilder is still much more efficient, 15 years 
after this code was written :) It behaves closer to a regular 
array, maybe because of space reallocation strategy?

Any comment appreciated



More information about the Digitalmars-d-learn mailing list