July 24, 2014 Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Is this a bug? Or am I misunderstanding something? import std.stdio, std.string; import std.array; unittest { int[] a; a.reserve(10); a.length++; int *p = &(a[0]); a.length++; // Increase size. Shouldn't reallocate. int *p2 = &(a[0]); assert(p == p2); // And indeed it didn't. // So sometime later, we want to reuse the allocation. We 'clear' it: a.length = 0; // ... and later start using it... a.length++; int *q = &a[0]; assert(p == q, format("p: %s; q: %s", p, q)); // FAILS! (on dmd 2.065.0) Why? Apparently, setting length to 0 caused it to free the // reserved allocation? } |
July 24, 2014 Re: Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Godfrey | On Thursday, 24 July 2014 at 23:30:13 UTC, Andrew Godfrey wrote:
> Is this a bug? Or am I misunderstanding
> something?
>
You need to use assumeSafeAppend before increasing the length again to avoid allocating new storage.
The runtime doesn't know that `a` is the only reference to that array, so increasing the length causes an allocation to avoid stomping on data.
|
July 24, 2014 Re: Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Godfrey | See this post: http://forum.dlang.org/thread/shpcpmwvphbyaumxghsd@forum.dlang.org My response in that thread: ---- The reason is simple, observe: auto arr = [1,2,3] It is safe if you append 4 to that. auto arr = [1,2,3,4] auto other = arr[]; arr.length = 2; It is *not* safe to append 5 to arr here, because doing so would change other to [1,2,5,4] which is (probably) not what you want (or, at least, you haven't made it explicit that you're okay with this behavior). Since slices are unaware of whether other views on memory exist, they must always reallocate if they've been shrunk to avoid the possibility of overwriting existing elements in other views of the array. When you use "assumeSafeAppend", you are guaranteeing that either no other views exist, or if they do exist that you don't care if they are overwritten. Take note that the second case is *really bad* if your element type is immutable, for obvious reasons. ---- |
July 25, 2014 Re: Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Godfrey | On Thu, 24 Jul 2014 23:30:11 +0000 Andrew Godfrey via Digitalmars-d <digitalmars-d@puremagic.com> wrote: > Is this a bug? Or am I misunderstanding > something? It's not a bug. If it did it any other way, you'd have problems with arrays stomping on each others memory. The underlying data structure keeps track of the farthest point in the block which an array has been expanded to, but it doesn't keep track of what arrays exist that point to it, so it can't know which ones point where. So, it has no way of knowing that the slice whose length you're setting to 0 is the only slice. assumeSafeAppend is used to fix that problem, but you have to be sure that it _is_ safe to append to that array without stomping on other arrays, or you're going to have problems. You really should read this article: http://dlang.org/d-array-article.html - Jonathan M Davis |
July 25, 2014 Re: Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Friday, 25 July 2014 at 04:38:40 UTC, Jonathan M Davis via Digitalmars-d wrote: > You really should read this article: > > http://dlang.org/d-array-article.html Thank you, that was educational. What I needed to read was that "A slice does not own the array, it references the array." - which I knew, but I hadn't internalized that even when a dynamic array is a private member, it is accessed via a slice and the runtime is not aware when there's a 1-to-1 correspondence between the two. The documentation could help a bit more. I'm game to try to make a pull request for this, but I'm wondering if the library docs can actually point to the article? More feasibly, I'm thinking: Both the documentation for length() (http://dlang.org/arrays) and reserve() (http://dlang.org/phobos/std_array.html) should at least mention assumeSafeAppend. An example similar to what I posted may also be worthwhile. (As I understand it from the article, the problem is that reducing 'length' also reduces 'used', whereas in this case I really want that slice to continue to own the entire reserved amount.) |
July 25, 2014 Re: Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Godfrey | On Fri, Jul 25, 2014 at 03:07:53PM +0000, Andrew Godfrey via Digitalmars-d wrote: > On Friday, 25 July 2014 at 04:38:40 UTC, Jonathan M Davis via Digitalmars-d wrote: > >You really should read this article: > > > >http://dlang.org/d-array-article.html > > Thank you, that was educational. > What I needed to read was that "A slice does not own the array, it > references the array." - which I knew, but I hadn't internalized that > even when a dynamic array is a private member, it is accessed via a > slice and the runtime is not aware when there's a 1-to-1 > correspondence between the two. > > The documentation could help a bit more. I'm game to > try to make a pull request for this, but I'm wondering > if the library docs can actually point to the article? That's a very good idea. I think that would dispel a lot of misconceptions that newcomers might have. > More feasibly, I'm thinking: > Both the documentation for length() (http://dlang.org/arrays) > and reserve() (http://dlang.org/phobos/std_array.html) > should at least mention assumeSafeAppend. > An example similar to what I posted may also be worthwhile. [...] Yes, please submit a PR for this. T -- I see that you JS got Bach. |
July 25, 2014 Re: Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Godfrey | On Friday, 25 July 2014 at 15:07:55 UTC, Andrew Godfrey wrote: > The documentation could help a bit more. I'm game to > try to make a pull request for this, but I'm wondering > if the library docs can actually point to the article? > > More feasibly, I'm thinking: > Both the documentation for length() (http://dlang.org/arrays) > and reserve() (http://dlang.org/phobos/std_array.html) > should at least mention assumeSafeAppend. > An example similar to what I posted may also be worthwhile. Instead of using the old library link (http://dlang.org/phobos/) use http://dlang.org/library/ instead. This way you can go to http://dlang.org/library/std/array.html and in the discuss section at the bottom make comments. The D community is hoping those discussion sections start being used in the same way the PHP documentation is used. |
July 25, 2014 Re: Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Friday, 25 July 2014 at 15:59:18 UTC, Gary Willoughby wrote:
>
> The D community is hoping those discussion sections start being
> used in the same way the PHP documentation is used.
We are? Please, no. Holding the PHP doc comments up as an example of a positive thing is alternately alternately hilarious and terrifying.
-Wyatt
|
July 25, 2014 Re: Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyatt | "Wyatt" <wyatt.epp@gmail.com> wrote:
> On Friday, 25 July 2014 at 15:59:18 UTC, Gary Willoughby wrote:
>>
>> The D community is hoping those discussion sections start being used in the same way the PHP documentation is used.
>
> We are? Please, no. Holding the PHP doc comments up as an example of a positive thing is alternately alternately hilarious and terrifying.
>
> -Wyatt
Why? I don't have much context but a few people I talked to were positive about the community contribution to php docs. -- Andrei
|
July 25, 2014 Re: Setting array length to 0 discards reserved allocation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Friday, 25 July 2014 at 17:32:38 UTC, Andrei Alexandrescu wrote:
> "Wyatt" <wyatt.epp@gmail.com> wrote:
>> On Friday, 25 July 2014 at 15:59:18 UTC, Gary Willoughby wrote:
>>>
>>> The D community is hoping those discussion sections start being
>>> used in the same way the PHP documentation is used.
>>
>> We are? Please, no. Holding the PHP doc comments up as an example of a
>> positive thing is alternately alternately hilarious and terrifying.
>>
>> -Wyatt
>
> Why? I don't have much context but a few people I talked to were positive
> about the community contribution to php docs. -- Andrei
They are hit or miss. Sometimes you'll find exactly what you need and sometimes you'll find terrible misinformation. I suspect this our community will fair better than PHP has here but there is still that risk.
I think it's worth a shot and if it becomes a problem the user comments can just be removed.
|
Copyright © 1999-2021 by the D Language Foundation