Skip to content

Lazy initialization for Member<T>.NameUtf8 / EnumMemberAttribute / Labels - #128

Merged
xin9le merged 5 commits into
mainfrom
perf/lazy-initialization
Nov 20, 2026
Merged

xin9le merged 5 commits into
mainfrom
perf/lazy-initialization

Conversation

@xin9le

@xin9le xin9le commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Summary

Member<T> used to build the following three values in its constructor for every member. Most applications never use them, or only a few APIs read them, so they are now created on first access.

Property Before After
NameUtf8 Encoding.UTF8.GetBytes(name) on every construction Created on first access
EnumMemberAttribute GetCustomAttribute<EnumMemberAttribute>() on every construction Fetched on first access
Labels (internal) GetCustomAttributes<LabelAttribute>() + ToFrozenDictionary() on every construction Created on first access (used only by GetLabel())

Implementation notes

  • Thread safety: Interlocked.CompareExchange ensures that only one instance is ever published, even when several threads initialize at the same time.
    • ImmutableArray<T> compares its underlying arrays by reference, so NameUtf8 must always return the same array.
    • GetCustomAttribute() returns a new instance on every call, so EnumMemberAttribute also keeps a single reference.
  • "Not fetched" for EnumMemberAttribute: null is a valid result when the attribute is not defined. So "not fetched yet" is detected by reference comparison with a dedicated sentinel instance (JitOptimization.MemberHelper.s_notFetchedEnumMemberAttribute).
    • A bool flag would make Member<T> larger for enums with an 8-byte underlying type, so a sentinel is used instead.
    • The size of Member<T> is unchanged.

Performance

  • Environment:
    • BenchmarkDotNet v0.16.0-preview.2
    • .NET SDK 11.0.100
    • AMD EPYC 7763 2.44 GHz (4 cores, 8 threads, Hyper-V)
    • Windows 11
  • Compared builds: main vs. this branch.

1. First-use cost (static constructor)

Initialization happens only once per process. BenchmarkDotNet's default job repeats the same call many times and averages the results, so it cannot measure this cost. It was measured as follows instead:

  • RunStrategy.ColdStart launched 40 processes, each measuring a single call (Warmup 0, Iteration 1, Invocation 1).
  • That single call is the first use of FastEnum.GetValues<T>() for 100 enum types. The reported time is per enum type.
  • FastEnum.Core is loaded into the default AssemblyLoadContext and called directly, without reflection.
  • Each job switches the DLL under test via an MsBuildArgument.
  • Allocation was measured inside the benchmark with GC.GetAllocatedBytesForCurrentThread(). Under ColdStart, MemoryDiagnoser would measure a second, already-initialized call.
  • The run was repeated twice with the job order reversed, to rule out ordering effects.
Per enum type main branch Diff
Median time (run 1) 3.399 ms 3.265 ms −3.9 %
Median time (run 2) 3.415 ms 3.266 ms −4.4 %
Allocated 9,968 B 8,169 B −1,798 B (−18.0 %)

2. Accessors after initialization (default job)

Method main branch Allocated
GetLabel() 1.767 ns 2.037 ns -
GetEnumMemberValue() 1.629 ns 1.946 ns -
Member<T>.NameUtf8 1.333 ns 1.630 ns -
  • Each call is about 0.3 ns slower, because it now checks whether the value has been initialized.
  • Nothing extra is allocated.
  • These APIs are rarely used, so reducing the initialization cost that every user pays was prioritized.

Notes

  • The access modifier of JitOptimization.EnumKeyDictionary.t_pending changed from internal to public. The enclosing JitOptimization class is internal, so nothing new is exposed outside the assembly.
  • All 633 existing unit tests pass on .NET 10 and .NET 11.

🤖 Generated with Claude Code

@xin9le xin9le self-assigned this Nov 20, 2026
@xin9le xin9le added the enhancement New feature or request label Nov 20, 2026
@xin9le
xin9le merged commit 5e583a3 into main Nov 20, 2026
2 checks passed
@xin9le
xin9le deleted the perf/lazy-initialization branch November 20, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant