Can assumeSafeAppend() grab more and more capacity?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 6 23:15:34 PDT 2017


On Wednesday, June 07, 2017 07:43:06 ag0aep6g via Digitalmars-d-learn wrote:
> You understand the spec to say that because `foo.capacity` is 15 at one
> point, you should then be able to put 15 elements into `foo` without
> relocation. And what `bar` does in the meantime shouldn't matter.
>
> I don't think the spec is supposed to make that strong a guarantee, but
> I see how it can be interpreted that way. Maybe it should be
> reworded/amended to describe the actual behavior more precisely.

Given the nature of dynamic arrays in D, it doesn't actually make sense to
guarantee the capacity when another dynamic array referring to the same
memory does something which could affect that capacity. As far as I can
tell, it would actually be impossible to do so, because the runtime doesn't
actually have any idea how many dynamic arrays refer to the same memory
without doing the work that it would do with a collection of the GC to find
everything that points to that block of memory. For it to work otherwise
would basically require that a dynamic array manage its own memory rather
than having the GC do it. The fact that a dynamic array in D is just a
struct with a pointer and a length pretty much forces the semantics that we
currently have.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list