Re: Best way to clear dynamic array for reuse

2016-12-28 Thread Nemanja Boric via Digitalmars-d-learn
On Wednesday, 28 December 2016 at 12:30:33 UTC, Michael Rynn wrote: It takes a bit of work to get around the mutable buffers problem in D language arrays. I found it made a performance difference in xml parsing. https://github.com/betrixed/dlang-xml/blob/master/xml/util/buffer.d /**

Best way to clear dynamic array for reuse

2016-12-28 Thread Michael Rynn via Digitalmars-d-learn
It takes a bit of work to get around the mutable buffers problem in D language arrays. I found it made a performance difference in xml parsing. https://github.com/betrixed/dlang-xml/blob/master/xml/util/buffer.d /** Buffer(T) - Versatile appendable D array for buffer reuse, append, re

Re: Best way to clear dynamic array for reuse

2016-07-14 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/14/16 5:56 AM, Miguel L wrote: On Thursday, 14 July 2016 at 09:12:50 UTC, Jonathan M Davis wrote: So, whether you should be using Appender or assumeSafeAppend or neither depends entirely on what you're doing. However, in general, simply appending to dynamic arrays does not result in many re

Re: Best way to clear dynamic array for reuse

2016-07-14 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, July 14, 2016 09:56:02 Miguel L via Digitalmars-d-learn wrote: > Thank you Jonathan, that really cleared up a lot of things, I > read the article. But I still have this doubt: is > assumeSafeAppend() changing a property of the array as "this > array is never going to be referenced by a

Re: Best way to clear dynamic array for reuse

2016-07-14 Thread Miguel L via Digitalmars-d-learn
On Thursday, 14 July 2016 at 09:12:50 UTC, Jonathan M Davis wrote: So, whether you should be using Appender or assumeSafeAppend or neither depends entirely on what you're doing. However, in general, simply appending to dynamic arrays does not result in many reallocations (just like it doesn't r

Re: Best way to clear dynamic array for reuse

2016-07-14 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, July 14, 2016 07:07:52 Miguel L via Digitalmars-d-learn wrote: > 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 >

Re: Best way to clear dynamic array for reuse

2016-07-14 Thread Miguel L via Digitalmars-d-learn
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

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread Steven Schveighoffer via Digitalmars-d-learn
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:

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, July 13, 2016 11:59:18 Miguel L via Digitalmars-d-learn 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 kno

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread Lodovico Giaretta via Digitalmars-d-learn
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 Appende

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread Miguel L via Digitalmars-d-learn
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

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread Mathias Lang via Digitalmars-d-learn
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. T

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread cym13 via Digitalmars-d-learn
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,

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread Lodovico Giaretta via Digitalmars-d-learn
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

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread cym13 via Digitalmars-d-learn
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 c

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread ketmar via Digitalmars-d-learn
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 allo

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread Lodovico Giaretta via Digitalmars-d-learn
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 c

Re: Best way to clear dynamic array for reuse

2016-07-13 Thread rikki cattermole via Digitalmars-d-learn
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 avoi

Best way to clear dynamic array for reuse

2016-07-13 Thread Miguel L via Digitalmars-d-learn
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