relative benefit of .reserve and .length

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 28 06:09:51 PDT 2016


On Thursday, 28 April 2016 at 12:56:24 UTC, Jay Norwood wrote:
> I timed some code recently and found that .reserve made almost 
> no improvement when appending.  It appears that the actual 
> change to the length by the append had a very high overhead of 
> something over 200 instructions executed, regardless if the 
> .reserve was done.  This was a simple append to an integer 
> array.
>
> The only way I found to avoid this was to set the length 
> outside the loop and update the array values by index.  That 
> was on the order of 10x faster.

Have you looked at the way .reserve is used in Appender ? In this 
struct reserving has a true impact. Exactly the opposite of what 
you've observed: if nothing is reserved in an appender then the 
Appender is not worth (unfortunately I have a benchmark for this 
but on another machine :/).

Out of an appender I believe .reserve can be used to force page 
creation if you know that several pages will be allocated.
For example for an ubyte[] when .length goes from 16 to 17 the 
memory block *really* allocated by realloc goes from 16 to 4096.


More information about the Digitalmars-d-learn mailing list