Builtin array and AA efficiency questions

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 16 08:46:50 PDT 2015


On 10/16/15 1:05 AM, Mike Parker wrote:
> On Thursday, 15 October 2015 at 21:48:29 UTC, Random D user wrote:
>
>>> An array uses a block marked for appending, assumeSafeAppend simply
>>> sets how much data is assumed to be valid. Calling assumeSafeAppend
>>> on a block not marked for appending will do nothing except burn CPU
>>> cycles.
>>>
>>> So yours is not an accurate description.
>>
>> Related to my question above.
>> How do you get a block not marked for appending? a view slice?
>>
>> Perhaps I should re-read the slice article. I believe it had something
>> like capacity == 0 --> always allocates. Is it this?
>
> There are a handful of attributes that can be set on memory allocated by
> the GC. See the BlkAttr enumeration in core.memory [1]. Under the hood,
> memory for dynamic arrays (slices) is marked with BlkAttr.APPENDABLE. If
> an array pointing to memory not marked as such, either manually
> allocated through the GC, through malloc, or another source, then
> assumeSafeAppend can't help you.

Yes, this flag is ONLY set when new'ing an array, or otherwise 
allocating via an array operation (appending, extending length, etc). It 
should NOT be set any other way, because the runtime is the only place 
where this flag is understood.

> capacity tells you how many more elements can be appended to a dynamic
> array (slice) before an allocation will be triggered.

Mostly correct :) capacity includes current elements, so it doesn't 
change as you append.

> So if you get a 0,
> that means the next append will trigger one.

Correct.

-Steve


More information about the Digitalmars-d-learn mailing list