Best way to clear dynamic array for reuse

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 13 05:41:17 PDT 2016


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`. 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.


More information about the Digitalmars-d-learn mailing list