On Wednesday, 13 July 2016 at 17:19:09 UTC, Steven Schveighoffer
wrote:
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
Ok, i have read about Appender and assumeSafeAppend(), but i am
still a bit confused.
What i have understood is: dynamic arrays are (almost) always
reallocating when appending to them except assumeSafeAppend() is
used, or when wrapping them with Appender. So, if i'm sure there
are no slices referencing my array i can use assumeSafeAppend().
But is this permanent? I mean if I declare:
array A[] x;
x.assumeSafeAppend();
Does that last forever so I can append without reallocating after
emptying array x, or should I call assumeSafeAppend() every time
I adjust x.length or append to x?
Maybe I should give up trying to use dynamic arrays and use fixed
length arrays instead.