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

Steven Schveighoffer schveiguy at gmail.com
Thu Apr 1 15:52:18 UTC 2021


On 4/1/21 10:53 AM, ludo wrote:

> The results are below (you can also git clone the repo + dub test):
> 
> | Concatenation method | benchmark in ms|
> |---------------------|---------------------|
> |with std:            | 385 ms|
> |with stdReserve:     | 327 ms|
> |with stdLength:      | 29 ms|
> |with AppenderReserve:| 256 ms|
> |with Arraybuilder:   | 118 ms|
> 
> - The use of reserve does not seem to improve much the standard 
> concatenation performance. **Would you know why?**

My guess:

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.

> - AppenderReserve outperforms a basic concatenation, but the gain is not 
> completely obvious, even with a call to reserve. **Is it expected 
> behavior?**

Yes, because Appender has direct access to the allocated length, rather 
than having to go through an opaque call.

> 
> - ArrayBuilder is 3 times faster than standard concatenation, and twice 
> as fast as AppenderReserve! **Which explanation would you give?** 

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


> There are some functions calls... Maybe the drag comes from there. I am 
> really out of my league here, any help appreciated. I am not even sure 
> which part of the Appender code is activated.

Are you compiling with -inline -O?

-Steve


More information about the Digitalmars-d-learn mailing list