Has AA a bad implementation?

Steven Schveighoffer schveiguy at gmail.com
Fri Feb 12 22:23:45 UTC 2021

On 2/12/21 4:56 PM, frame wrote:
> On Friday, 12 February 2021 at 20:59:50 UTC, Steven Schveighoffer wrote:
>> There's a misunderstanding here. The bucket array is not the data 
>> itself, its just basically a pointer and the hash. The Bucket struct 
>> is here:
> No, I'm talking about those Buckets.
>> If you want the bucket memory itself to be freed on the next GC cycle, 
>> you can just set the AA to null. But the bucket memory shouldn't be 
>> that cumbersome.
> Maybe not, but it comes in mind for daemon applications. 10000 items are 
> consuming 600 ~ 800K each array.

Your numbers are a bit high, but I don't know what else you would 
expect. If you have an AA with 10000 elements, it's going to consume a 
multiple of that number in memory.

> Yes, nulling helps to get rid of the Bucket list. Also destroy(). But 
> that's not so intentional than using clear() - this is my point.

The intent of clear is to reset the array so I DON'T have to reallocate 
the bucket array.

Before clear existed, you had to remove each element one at a time, and 
then it would resize down throwing away all that space. If your use case 
is to clear an aa to fill it up again, then clear is perfect for that. 
It also has implications if you have multiple references to the same AA.

Note, I thought rehash would shrink the bucket array down, but it looks 
like it ignores that when the AA is empty. That seems like an omission.

Other types in Phobos treat clear the same way (e.g. appender).

I think you are just misunderstanding what clear is for. It means, reset 
but don't deallocate.


More information about the Digitalmars-d mailing list