Best way to clear dynamic array for reuse
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 13 10:19:09 PDT 2016
On 7/13/16 8:41 AM, Lodovico Giaretta wrote:
> On Wednesday, 13 July 2016 at 12:37:26 UTC, Miguel L wrote:
>> I tried Appender, but for some reason garbage collector still seems to
>> be running every few iterations.
>> I will try to expand a little on my code because maybe there is
>> something i am missing:
>>
>> Appender!(A[]) a;
>>
>> void foo( out Appender!(A[]) bar)
>> {
>> ...
>> bar~= lot of elements
>> }
>>
>> for(....)
>> {
>> //a=[]; //discard array contents
>> a.clear();
>> foo(a) appends thousand of elements to a
>> ... use a for some calculations
>> }
>
> Well, I think foo's parameter should be `ref Appender!(A[]) bar` instead
> of `out Appender!(A[]) bar`.
Yes, this is why you still have issues. An out parameter is set to its
init value upon function entry, so you have lost all your allocation at
that point.
> Also, if you know you will append lots of
> elements, doing a.reserve(s), with s being an estimate of the number of
> appends you expect, might be a good idea.
This is true for builtin arrays as well.
-Steve
More information about the Digitalmars-d-learn
mailing list