October 25, 2015
Jonathan M Davis via Digitalmars-d-learn wrote:

> Appender really isn't intended to be used as a
> container - just as a way to make appending more efficient or to have an
> output range which is an array

I get the part about Appender helping to make an output range of a regular array, but I'm not sure how it is supposed to make appending "more efficient". I just had a look at the std.array code for appender and couldn't figure what was so special – obviously it's my limited knowledge.

http://dlang.org/arrays.html#resize says: """Also, you may wish to utilize the phobos reserve function to pre-allocate array data to use with the append operator."""

I presume this means http://dlang.org/phobos/std_array.html#.Appender.reserve but how `append` is considered an operator is beyond me.

Anyhow, is `reserve` the only thing that makes this more efficient? How is this more efficient than setting the .length of the dynamic array directly? I see there's a lot of logic going into ensureAddable() but doesn't this logic happen within druntime for the regular dynamic arrays itself?

-- 
Shriramana Sharma, Penguin #395953
October 25, 2015
On Sunday, 25 October 2015 at 11:45:53 UTC, Shriramana Sharma wrote:
> http://dlang.org/arrays.html#resize says: """Also, you may wish to utilize the phobos reserve function to pre-allocate array data to use with the append operator."""
>
> I presume this means http://dlang.org/phobos/std_array.html#.Appender.reserve but how `append` is considered an operator is beyond me.

That sentence doesn't refer to std.array.Appender. `reserve` means <http://dlang.org/phobos/object.html#.reserve>. The append operator is `~=`.

October 25, 2015
anonymous wrote:

>> I presume this means http://dlang.org/phobos/std_array.html#.Appender.reserve but how `append` is considered an operator is beyond me.
> 
> That sentence doesn't refer to std.array.Appender. `reserve` means <http://dlang.org/phobos/object.html#.reserve>. The append operator is `~=`.

Thanks for that clarification. Now submitted a pull request.

-- 
Shriramana Sharma, Penguin #395953
October 26, 2015
On Sunday, October 25, 2015 16:23:14 Shriramana Sharma via Digitalmars-d-learn wrote:
> Thanks all, for your replies.
>
> Jonathan M Davis via Digitalmars-d-learn wrote:
>
> > If you want a container rather than a dynamic array - especially if you're looking for full reference semantics - then use std.container.array.Array.
>
> Hmmm, pardon me but while I'm sure I don't specifically require reference semantics, I'm not sure how you mean to contrast a "container" vs a "dynamic array". Both are aware of their content count and both are iterable, no? Is it that by "container" you mean that something that owns its contents and is responsible for deleting them all when it itself is deleted?

Dynamic arrays are really pseudo-containers. They do not own or manage their own memory, and their memory and elements are potentially shared across multiple dynamic arrays. And mutating one dynamic array does not normally affect another one - even if they refer to the same memory - unless you're mutating its elements, and if one of them ends up having to be reallocated (e.g. because there wasn't enough room to append another element when an append operation was attempted), then two dynamic arrays which referred to the same memory would then refer to completely different memory.

You started out this thread talking about how you wanted to be able to "clear" a dynamic array and have that affect other dynamic arrays which referred to the same memory, and that doesn't make any sense with a dynamic array, because dynamic arrays are not full reference types. They share the memory that they point to, but mutating the length of one (either directly or by adding or removing elements) does not affect any other dynamic array (except insofar as it can affect when an array would have to have its memory reallocated). That's why I talked about reference semantics.

If you want a container that you pass around where removing an element from it or adding an element to it affects all of the other variables referring to that same data, you need an actual container type, not a dynamic array.

If you haven't read this article yet

http://dlang.org/d-array-article.html

I'd suggest that you do. The terminology that it uses does not match the offial terminology (e.g. per the spec, T[] is a dynamic array regardless of what memory backs it, whereas that article refers to the GC-allocated buffer that backs most dynamic arrays as being the dynamic array), but it should make the semantics of D's dynamic arrays much clearer.

- Jonathan M Davis

October 26, 2015
On Sunday, October 25, 2015 17:15:50 Shriramana Sharma via Digitalmars-d-learn wrote:
> Jonathan M Davis via Digitalmars-d-learn wrote:
>
> > Appender really isn't intended to be used as a
> > container - just as a way to make appending more efficient or to have an
> > output range which is an array
>
> I get the part about Appender helping to make an output range of a regular array, but I'm not sure how it is supposed to make appending "more efficient". I just had a look at the std.array code for appender and couldn't figure what was so special – obviously it's my limited knowledge.

There is bookkeeping in the GC involved with appending to a dynamic array (e.g. it has to look up whether there is room to grow into the buffer that backs the dynamic array or even whether the dynamic array is even backed by a GC-allocated buffer at all). Appender takes advantage of the fact that it's designed specifically for appending and keeps track of certain things on its own, bypassing a lot of what ~= normally does in an effort to make the specific use case of appending a bunch of times in a row efficient. So, it bypasses a lot of the checks that ~= is normally forced to do, but the result is that it's really just for constructing an array up front, whereas ~= can be used whenever, and I'm not sure that Appender even works correctly if you do something like get the array from it and start operating on it separately from the Appender and then continue to use the Appender. Really, you're supposed to use it to fill the array with its initial values, get the array out of the Appender, and then stop using the Appender.

- Jonathan M Davis


1 2
Next ›   Last »