Re: Avoid deallocate empty arrays?

2020-12-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/17/20 5:57 PM, H. S. Teoh wrote: On a side note, though, I find this idiosyncratic behaviour annoying when all I really want is to use an array as, e.g., a backing for a stack. For those cases, I ignore array capacity and keep a slice over the entire allocated storage, including elements

Re: Avoid deallocate empty arrays?

2020-12-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On 12/17/20 1:10 PM, IGotD- wrote: On Thursday, 17 December 2020 at 17:46:59 UTC, Steven Schveighoffer wrote: This isn’t correct. Can you post the code that led you to believe this? -Steve Sure. import std.algorithm; import std.typecons; import std.stdio; struct Buffer {

Re: Avoid deallocate empty arrays?

2020-12-17 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Dec 17, 2020 at 06:57:08PM +, IGotD- via Digitalmars-d-learn wrote: [...] > How does this connect to the array with zero elements? According to > your explanation if I have understood it correctly, capacity is also > indicating if the pointer has been "borrowed" from another array >

Re: Avoid deallocate empty arrays?

2020-12-17 Thread Ali Çehreli via Digitalmars-d-learn
On 12/17/20 8:48 AM, Ali Çehreli wrote: > There is also assumeUnique() I meant assumeSafeAppend(). Ali

Re: Avoid deallocate empty arrays?

2020-12-17 Thread IGotD- via Digitalmars-d-learn
On Thursday, 17 December 2020 at 18:42:54 UTC, H. S. Teoh wrote: Are you sure? My understanding is that capacity is always set to 0 when you shrink an array, in order to force reallocation when you append a new element. The reason is this: int[] data = [ 1, 2, 3, 4, 5 ];

Re: Avoid deallocate empty arrays?

2020-12-17 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Dec 17, 2020 at 06:10:25PM +, IGotD- via Digitalmars-d-learn wrote: [...] > b.m_buffer.length 2, b.m_buffer.capacity 31 > b.m_buffer.length 0, b.m_buffer.capacity 0 > > capacity 0, suggests that the array has been deallocated. Are you sure? My understanding is that capacity is

Re: Avoid deallocate empty arrays?

2020-12-17 Thread IGotD- via Digitalmars-d-learn
On Thursday, 17 December 2020 at 17:46:59 UTC, Steven Schveighoffer wrote: This isn’t correct. Can you post the code that led you to believe this? -Steve Sure. import std.algorithm; import std.typecons; import std.stdio; struct Buffer { this(size_t size) {

Re: Avoid deallocate empty arrays?

2020-12-17 Thread Steven Schveighoffer via Digitalmars-d-learn
On Thursday, 17 December 2020 at 16:11:37 UTC, IGotD- wrote: It's common using arrays for buffering, that means constantly adding elements and empty the elements. I have seen that when the number of elements is zero, the array implementation deallocates the array which is shown with capacity

Re: Avoid deallocate empty arrays?

2020-12-17 Thread IGotD- via Digitalmars-d-learn
On Thursday, 17 December 2020 at 16:46:47 UTC, Q. Schroll wrote: On Thursday, 17 December 2020 at 16:11:37 UTC, IGotD- wrote: It's common using arrays for buffering Outside of CTFE, use an Appender.¹ Unless you're having a const/immutable element type, Appender can shrink and reuse space.²

Re: Avoid deallocate empty arrays?

2020-12-17 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 17 December 2020 at 16:11:37 UTC, IGotD- wrote: It's common using arrays for buffering Outside of CTFE, use an Appender.¹ Unless you're having a const/immutable element type, Appender can shrink and reuse space.² If you have, reallocation is necessary anyway not to break

Re: Avoid deallocate empty arrays?

2020-12-17 Thread Ali Çehreli via Digitalmars-d-learn
On 12/17/20 8:11 AM, IGotD- wrote: > It's common using arrays for buffering, that means constantly adding > elements and empty the elements. I show an example of this at the following point in a DConf presentation: https://youtu.be/dRORNQIB2wA?t=791 The following code: int[] outer;

Avoid deallocate empty arrays?

2020-12-17 Thread IGotD- via Digitalmars-d-learn
It's common using arrays for buffering, that means constantly adding elements and empty the elements. I have seen that when the number of elements is zero, the array implementation deallocates the array which is shown with capacity is zero. This of course leads to constant allocation and