On Wednesday, 13 July 2016 at 12:05:18 UTC, Lodovico Giaretta wrote:
On Wednesday, 13 July 2016 at 11:59:18 UTC, Miguel L wrote:
I am using a temporary dynamic array inside a loop this way:
A[] a;
for(....)
{
a=[]; //discard array contents
... appends thousand of elements to a
... use a for some calculations
}

I would like to know which would be the best way to clear a contents avoiding reallocations, as there seems to be lots of garbage collection cycles taking place.

The options would be:

a=[];
a.length=0;
a=null;
...
any other?

Can you help me please?

Use std.array.Appender. It allows faster appends, and has a handy .clear method that zeroes the length of the managed array, without de-allocating it, so the same buffer is reused.

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
 }



Reply via email to