Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 13, 2016 Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
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? |
July 14, 2016 Re: Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
Posted in reply to Miguel L | On 13/07/2016 11:59 PM, 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?
All of those "options" do the same thing, remove all references to that data.
There is a couple of options. What I will recommend instead is to start using buffers and only expand when you append past the length. You will probably want a struct to wrap this up (and disable postblit).
Otherwise if you're lazy just disable GC and reenable after the code segment.
|
July 13, 2016 Re: Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
Posted in reply to Miguel L | 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.
|
July 13, 2016 Re: Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
Posted in reply to Miguel L | On Wednesday, 13 July 2016 at 11:59:18 UTC, Miguel L wrote:
> The options would be:
>
> a=[];
> a.length=0;
> a=null;
> ...
> any other?
it really depends of your other code. if you don't have any slices of the array, for example, you can use `a.length = 0; a.assumeSafeAppend;` -- this will reuse the allocated memory. but you should be REALLY sure that you have no array slices are floating around! 'cause you effectively promised the runtime that.
otherwise, `a = [];` and `a = null;` is the same, as `[]` is a "null array".
most of the time it is ok to use `a = null;` and let GC do it's work. it is safe, and you'd better stick to that.
|
July 13, 2016 Re: Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
Posted in reply to Miguel L | 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?
The best option would be a.clear(). From the language specs:
“Removes all remaining keys and values from an associative array. The array is not rehashed after removal, to allow for the existing storage to be reused. This will affect all references to the same instance and is not equivalent to destroy(aa) which only sets the current reference to null.”
|
July 13, 2016 Re: Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | On Wednesday, 13 July 2016 at 12:20:07 UTC, cym13 wrote:
> The best option would be a.clear(). From the language specs:
>
> “Removes all remaining keys and values from an associative array. The array is not rehashed after removal, to allow for the existing storage to be reused. This will affect all references to the same instance and is not equivalent to destroy(aa) which only sets the current reference to null.”
I don't think OP is using associative arrays, but dynamic arrays (if I understood correctly).
|
July 13, 2016 Re: Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lodovico Giaretta | On Wednesday, 13 July 2016 at 12:22:55 UTC, Lodovico Giaretta wrote:
> On Wednesday, 13 July 2016 at 12:20:07 UTC, cym13 wrote:
>> The best option would be a.clear(). From the language specs:
>>
>> “Removes all remaining keys and values from an associative array. The array is not rehashed after removal, to allow for the existing storage to be reused. This will affect all references to the same instance and is not equivalent to destroy(aa) which only sets the current reference to null.”
>
> I don't think OP is using associative arrays, but dynamic arrays (if I understood correctly).
You're right, my bad, I read too fast :/
|
July 13, 2016 Re: Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On Wednesday, 13 July 2016 at 12:05:12 UTC, rikki cattermole wrote: > On 13/07/2016 11:59 PM, Miguel L wrote: >> The options would be: >> >> a=[]; >> a.length=0; >> a=null; >> ... >> any other? >> >> Can you help me please? > > All of those "options" do the same thing, remove all references to that data. No they don't. The first and the third change the pointer, so one cannot reuse the array. @Miguel: You want to use http://dlang.org/phobos/object.html#.assumeSafeAppend See the example in the doc. To reset your buffer you can use`buff.length = 0` instead of taking a slice as the example does. |
July 13, 2016 Re: Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lodovico Giaretta | 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
}
|
July 13, 2016 Re: Best way to clear dynamic array for reuse | ||||
---|---|---|---|---|
| ||||
Posted in reply to Miguel L | 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.
|
Copyright © 1999-2021 by the D Language Foundation