Has AA a bad implementation?

Steven Schveighoffer schveiguy at gmail.com
Fri Feb 12 20:59:50 UTC 2021


On 2/12/21 3:35 PM, frame wrote:
> On Friday, 12 February 2021 at 12:48:30 UTC, welkam wrote:
> 
>> There can be slices that point to the memory owned by this array so 
>> you cant just free it. If you want manually manage the memory then you 
>> should look at dub for different containers.
> 
> The array is overwritten with 0 so any pointers are lost anyway. Yeah, I 
> can use any element I want. But my problem is that any thirdparty code 
> may not care about. In any static context it produces leaks. This 
> behavior is not documented - anyone thinks that clear() means clear, 
> not: fill with 0.
> 
> In some context it's needed to reduce memory. As a standard element it 
> should at least have an explicit ability to free memory when it's really 
> desired.

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: 
https://github.com/dlang/druntime/blob/a026ea46088b369f1182c397e7e046f5fc950358/src/rt/aaA.d#L173

When you use clear, it removes the pointers to all the existing 
key/value pairs. Those data items are separate GC blocks. If you no 
longer have any pointers to them, they will be garbage collected.

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.

I don't think there's any current way to explicitly free all the items 
in the AA. Even aa.remove(key) is not going to free the item.

-Steve


More information about the Digitalmars-d mailing list