`clear`ing a dynamic array

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 25 17:33:19 PDT 2015


On Sunday, October 25, 2015 17:15:50 Shriramana Sharma via Digitalmars-d-learn wrote:
> Jonathan M Davis via Digitalmars-d-learn wrote:
>
> > Appender really isn't intended to be used as a
> > container - just as a way to make appending more efficient or to have an
> > output range which is an array
>
> I get the part about Appender helping to make an output range of a regular
> array, but I'm not sure how it is supposed to make appending "more
> efficient". I just had a look at the std.array code for appender and
> couldn't figure what was so special – obviously it's my limited knowledge.

There is bookkeeping in the GC involved with appending to a dynamic array
(e.g. it has to look up whether there is room to grow into the buffer that
backs the dynamic array or even whether the dynamic array is even backed by
a GC-allocated buffer at all). Appender takes advantage of the fact that
it's designed specifically for appending and keeps track of certain things
on its own, bypassing a lot of what ~= normally does in an effort to make
the specific use case of appending a bunch of times in a row efficient. So,
it bypasses a lot of the checks that ~= is normally forced to do, but the
result is that it's really just for constructing an array up front, whereas
~= can be used whenever, and I'm not sure that Appender even works correctly
if you do something like get the array from it and start operating on it
separately from the Appender and then continue to use the Appender. Really,
you're supposed to use it to fill the array with its initial values, get the
array out of the Appender, and then stop using the Appender.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list