What's happening is that the static this() constructor builds the hash table once. The enum version builds it every time it is used, as the enum name is replaced with its initializer.
Comment #2 by 2korden — 2008-10-11T06:47:58Z
I believe enum should be smarter than that. If not, it is no different than C-style define macro.
Comment #3 by andrei — 2008-10-11T09:06:33Z
(In reply to comment #1)
> What's happening is that the static this() constructor builds the hash table
> once. The enum version builds it every time it is used, as the enum name is
> replaced with its initializer.
>
I hope this is an explanation of the bug's cause, not a justification that this is not a bug. :o)
Comment #4 by jarrett.billingsley — 2008-10-11T16:23:31Z
Comment #5 by mitch.hayenga — 2010-09-22T10:22:20Z
I recently hit this performance issue myself while trying to use a lookup table, rather than branching on logic for a function. It can be avoided by declaring the field as invariant, but I had originally used Enum as thats one of the ways suggested by TDPL for doing CTFE.
pseudocode:
bool[256] generate_lookup_table(); // function declaration
// Performance = terrible here
enum lookup_as_enum = generate_lookup_table();
// Performance = great here
invariant lookup_as_enum = generate_lookup_table();
Comment #6 by nfxjfg — 2010-09-22T10:51:38Z
(In reply to comment #1)
> What's happening is that the static this() constructor builds the hash table
> once. The enum version builds it every time it is used, as the enum name is
> replaced with its initializer.
That's quite hilarious. There are now half a dozen of bugs related to dmd being stupid with static data construction. E.g. see bug 4397, bug 2526, bug 2356, bug 4881. They possibly are all caused by the same underlying issue. Walter, don't you think your users are finally annoyed enough so that you could look into fixing it?
Comment #7 by bearophile_hugs — 2010-09-22T11:40:48Z
(In reply to comment #6)
> Walter, don't you think your users are finally annoyed enough
> so that you could look into fixing it?
Be more gentle, please.
Comment #8 by robert.schadek — 2024-12-13T17:48:50Z