200-600x slower Dlang performance with nested foreach loop

Steven Schveighoffer schveiguy at gmail.com
Wed Jan 27 21:22:26 UTC 2021


On 1/27/21 10:14 AM, Paul Backus wrote:
> On Wednesday, 27 January 2021 at 15:12:32 UTC, Paul Backus wrote:
>>
>> Maybe it's to avoid invalidating the result of `key in aa` when adding 
>> or removing entries? The spec doesn't say anything about it either way 
>> [1], but allowing invalidation would make AAs much more difficult to 
>> use in @safe code.

Yes, that's the reason.

> 
> Correction: the GC would take care of the safety issue, of course. I 
> haven't had my morning tea yet, and clearly I'm not thinking straight. :)

No, it wouldn't. The problem is not a GC issue, but an issue with 
aliasing expectations (if you rehash, you completely rearrange the buckets).

so if you have:

auto x = key in aa;

aa[someOtherKey] = value; // rehashes

Code at this point currently can count on x pointing at the item being 
used in the AA.

If we change it now, lots of code will break.

This is not truly a horrible issue, you can use a custom implementation 
(see any number of container libraries on code.dlang.org).

What would be nice though, is to provide an actual template, that 
builtin AAs are an alias for (like string), and then if you want 
slightly different behavior, you provide different parameters.

But at the end of the day, the builtin AAs will ALWAYS do this. We can't 
change it now.

-Steve


More information about the Digitalmars-d-learn mailing list