AA vs __gshared

Steven Schveighoffer schveiguy at gmail.com
Tue Aug 15 17:36:13 UTC 2023


On 8/12/23 5:55 AM, IchorDev wrote:
> On Thursday, 10 August 2023 at 15:20:28 UTC, Steven Schveighoffer wrote:
>> That shouldn't matter.
> 
> Well, it does here. The AA is mutated during the loop, so perhaps this 
> is an optimisation quirk where it works with `for` but segfaults in 
> `foreach`? I've pretty thoroughly abused the `for` version and I haven't 
> gotten it to segfault yet.

oh yeah. That is not allowed. Any mutation of the AA during iteration 
can invalidate existing foreach or ranges over the AA.

Basically, a rehash can cause the buckets to jumble up, and in that 
case, the "current index" can be changed to point at a null bucket.

More here: https://dlang.org/spec/statement.html#foreach_restrictions

In fact, that statement is way too broad. Invalidation of iteration 
should be based on the type's requirements.

We really should put a note in the AA spec page.

>> I also highly recommend using `emplace` to handle all the sticky 
>> issues with lifetime/construction.
> 
> Have not run into the aforementioned sticky issues yet, but I can't even 
> find `emplace`'s docs anywhere now.

https://dlang.org/phobos/core_lifetime.html#.emplace

> I recall it being incompatible with 
> classes that have @nogc/nothrow constructors though, which made it 
> pretty useless to me, and it wouldn't work with BetterC, which was a 
> requirement for the allocation wrapper I was writing at the time.

It probably won't work with betterC, but that's probably just because of 
linker errors.

Any attribute requirements would be inferred based on the attributes of 
your constructor, because emplace is a template.

-Steve


More information about the Digitalmars-d-learn mailing list