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
reallocations (just like it doesn't result in a lot of realloctions
for std::vector or ArrayList). When reallocations become a problem is
when you start slicing a dynamic array so that you have other dynamic
arrays which refer to the same memory, and you append to those dynamic
arrays, or when you reduce the length of an array and then append to
it, because in both of those cases, you're appending to dynamic arrays
which do not refer to the last element in their underlying memory block.

Hopefully, that makes things at least somewhat clearer.


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
any other slice, you can append or change its length any time and it is
never going to be reallocated unless it's out of free space"? or it is
more like "adjust capacity after last operation" so I should be calling
it whenever I am adjusting length or before appending?

No, it's not a permanent adjustment. It simply tells the array runtime that the extra elements are no longer used and can be reclaimed.

If you append again, then shrink, you have to call assumeSafeAppend again.

What Jonathan is trying to explain is that the array slice (the int[] type) does not store any of this information. It's all stored in the runtime. So there's nothing adjusted on the slice itself, just on the array runtime "type". This means the slice isn't considered "special" in any way.

-Steve

Reply via email to