Has AA a bad implementation?
Steven Schveighoffer
schveiguy at gmail.com
Sat Feb 13 16:18:02 UTC 2021
On 2/12/21 10:43 PM, frame wrote:
> On Friday, 12 February 2021 at 22:23:45 UTC, Steven Schveighoffer wrote:
>> On 2/12/21 4:56 PM, frame wrote:
>
>
>> Other types in Phobos treat clear the same way (e.g. appender).
>
> Is this really the same? This looks more like clearing in Appender:
>
>> void clear() @trusted pure nothrow
>> {
>> if (_data)
>> {
>> _data.arr = _data.arr.ptr[0 .. 0];
>> }
>> }
Yes, it's the same.
Appender uses knowledge of the array allocated length, the _data.arr
slice is the current "valid" portion.
Your request is the the bucket list is deallocated. Appender does not
deallocate the array, as it still deliberately retains a pointer to the
GC block.
>> I think you are just misunderstanding what clear is for. It means,
>> reset but don't deallocate.
>
> So it should be called reset() or blank() then.
You are not the first to interpret the meaning of words differently.
These are not winnable arguments. Just learn what it does now, not what
you think it should do.
And if you want a feature to do what you want, propose it under a
different name. The name "clear" is not changing.
>> I'm not really sure if it's a problem, just a weird inconsistency.
>> Technically nobody should care about the bucket array, it's an
>> implementation detail.
>
> I just don't like a widely used but leaky standard element that imply it
> does not. Better correct than error. But ok, it's not a big deal. Should
> be maybe documented that null can be useful.
>
Setting to null is useful, but only if you have one reference to the AA.
It won't deallocate the buckets if you have more than one reference.
e.g.:
int[int] aa = [1 : 2, 3 : 4];
auto bb = aa;
aa = null;
assert(bb.length == 2); // still there
aa = bb;
aa.clear;
assert(bb.length == 0); // all references are now empty, but bucket
array is still fully there
There is probably room to have a mechanism to destroy the buckets for
all references. Looking at destroy, it looks like it's equivalent to
setting equal to null. Which is consistent, but I'm surprised there's no
"recursive destroy" type function, even for normal arrays.
-Steve
More information about the Digitalmars-d
mailing list