Array concatenation vs. Appender

Jonathan M Davis jmdavisProg at gmx.com
Sat Feb 2 17:40:19 PST 2013


On Sunday, February 03, 2013 02:27:16 Era Scarecrow wrote:
> On Saturday, 2 February 2013 at 15:47:37 UTC, FG wrote:
> > Yeah but let us move reallocation out of the equation.
> > Reserving space limits the amount of RAM used and can avoid
> > reallocations all together but in a little test it came out
> > that still appender is 2.5-4 times faster than tab ~= str,
> > where tab is char[] (same when it is ubyte[]). Why is that?
> 
>   Reserving doesn't mean it has to allocate any memory at all;
> That's up to the implementation of the runtime and OS, it may
> just say 'any allocations not related to this object start after
> address xxx', then as the reserved space needs more it requests
> the larger space from the OS. The limitations of how much you can
> use memory-wise doesn't change as that's determined by the
> infrastructure. (VM and harddrive space doesn't count).

reserve should guarantee that you have at least the requested amount of memory 
already allocated, or it's broken. Its whole purpose is to guarantee that 
capacity is at least as large as the amount being reserved so that you can do 
a single allocation up front rather than having to worry about reallocations 
occuring as you append.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list