Skip to content

Consolidate per-enum JIT helpers into JitOptimization and keep ToUInt64 JIT-friendly - #126

Merged
xin9le merged 10 commits into
mainfrom
refactor
Nov 19, 2026
Merged

xin9le merged 10 commits into
mainfrom
refactor

Conversation

@xin9le

@xin9le xin9le commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Summary

Refactoring of the internal helpers that keep per-enum JIT cost low. No public API changes and no behavior changes.

  • Consolidate non-generic helpers into JitOptimization
    • CaseStringDictionaryHelper, EnumInfoHelper and EnumKeyDictionaryHelper are merged into one internal class, JitOptimization, as the nested classes CaseStringDictionary, EnumInfo and EnumKeyDictionary.
    • They hold the parts that do not depend on the enum type, so they are JIT-compiled only once.
  • Simplify EnumInfo<T>.toOrderedMembers()
    • It no longer takes the separate values argument; it reads members[i].Value instead.
  • Constrain JitOptimization.EnumKeyDictionary.ToUInt64<T>() to struct, Enum.
  • Keep ToUInt64<T>() as an if chain with Unsafe.BitCast
    • It is on the hot path of every value lookup and is inlined for each enum type.

Why ToUInt64 is not a switch expression and does not use Unsafe.As

While working on this PR, rewriting ToUInt64 as Unsafe.SizeOf<T>() switch { ... } with Unsafe.As<T, X>(ref value) made value lookups up to ~17% slower on 1/2/4-byte enums. It also made initialization ~1% slower. The JIT disassembly showed why:

  • The JIT folds a constant switch only after importing every arm. An if on a JIT-time constant is folded while importing, so the unused branches never reach the IR.
  • An unused arm can still force value onto the stack. With Unsafe.As(ref value), an imported but unused arm reads value wider than it is (e.g. ulong from a 4-byte enum). This forces value onto the stack, and it stays there after the arm is removed. Every lookup then pays a store and a reload.
  • Importing every arm costs init time. Importing all four arms also adds JIT time for every enum type, which showed up in initialization.

Unsafe.BitCast takes the value directly instead of its address, so it stays in a register. The if chain keeps the unused branches from ever being imported. Together they generate the same code as main.

Performance

Measured main against this branch with BenchmarkDotNet. Each job loads its own FastEnum.Core.dll, and the logs confirm which one each job loaded. No regression was found.

BenchmarkDotNet v0.16.0-preview.2, Windows 11 (Hyper-V)
AMD EPYC 7763 2.44GHz, 1 CPU, 8 logical and 4 physical cores
.NET 11.0.0, X64 RyuJIT x86-64-v3

Initialization (EnumInfo<T> static constructor for 999 enum types, ColdStart, 15 launches)

Enums main this PR Ratio
Non-contiguous values 3.990 s 4.040 s 1.01
Contiguous values 2.975 s 2.998 s 1.01

The ~1% difference is within the measurement error (±0.06 s).

Value lookup (ratio to main, 1,024 keys per invocation, no allocation)

Enum GetName IsDefined (defined) IsDefined (undefined)
Direct (5 values) 1.00 1.01 1.00
byte (10 values) 1.00 0.99 1.00
Flags (15 values) 1.00 1.00 1.00
HttpStatusCode 1.00 1.00 1.00
long (20 values) 1.00 1.00 1.00
Sparse (100 values) 1.00 0.99 1.00

Name lookup TryParse (ratio to main, 1,024 names per invocation)

Enum Case-sensitive Ignore case Ignore case (undefined)
Direct (5 values) 1.00 1.00 1.00
Flags (15 values) 1.03 1.00 1.00
HttpStatusCode 0.86 0.94 0.98
Sparse (100 values) 1.00 1.03 1.00
300 names 0.99 1.00 1.01

An A/A run, with the same DLL in both jobs, varied by up to ±8% on these sub-10 ns benchmarks. Differences of a few percent are therefore noise, and this PR is not claiming any speedup.

Tests

  • All 633 unit tests pass on .NET 10 and .NET 11.

🤖 Generated with Claude Code

@xin9le xin9le self-assigned this Nov 19, 2026
@xin9le xin9le changed the title Consolidate per-enum JIT helpers into JitOptimization and keep ToUInt64 JIT-friendly Consolidate per-enum JIT helpers into JitOptimization and keep ToUInt64 JIT-friendly Nov 19, 2026
@xin9le
xin9le merged commit 5e6d565 into main Nov 19, 2026
2 checks passed
@xin9le
xin9le deleted the refactor branch November 19, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant