str.to!Enum performance
realhet
real_het at hotmail.com
Mon Sep 7 16:59:25 UTC 2026
On Monday, 7 September 2026 at 16:20:42 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> On 08/09/2026 4:01 AM, realhet wrote:
>> On Monday, 7 September 2026 at 15:24:52 UTC, Richard (Rikki)
>> Andrew Cattermole wrote:
> It probably needs optimizing.
>
> Have at it.
Yepp, and I just found my tools for it. I almost forgot about
those.
```d
enum EnumMemberNames(T) = is(T==enum) ? [__traits(allMembers, T)]
: [];
enum EnumAssocArray(alias T) = /+keys = enumMember.text, values =
enumMember+/
((){ T[string] o; foreach(i, name; EnumMemberNames!T) o[name] =
[EnumMembers!T][i]; return o; })(); ```
```
I only create the AA if it is needed in runtime.
This way the .exe file only contains the enum member names as
separate zero terminated string literals. All strings are aligned
to 16 bytes.
Anyways, the most important is to know what's going under the
hood. Thx!
More information about the Digitalmars-d-learn
mailing list