Repository navigation
Conversation
ToUInt64 JIT-friendlyJitOptimization and keep ToUInt64 JIT-friendly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactoring of the internal helpers that keep per-enum JIT cost low. No public API changes and no behavior changes.
JitOptimizationCaseStringDictionaryHelper,EnumInfoHelperandEnumKeyDictionaryHelperare merged into one internal class,JitOptimization, as the nested classesCaseStringDictionary,EnumInfoandEnumKeyDictionary.EnumInfo<T>.toOrderedMembers()valuesargument; it readsmembers[i].Valueinstead.JitOptimization.EnumKeyDictionary.ToUInt64<T>()tostruct, Enum.ToUInt64<T>()as anifchain withUnsafe.BitCastWhy
ToUInt64is not a switch expression and does not useUnsafe.AsWhile working on this PR, rewriting
ToUInt64asUnsafe.SizeOf<T>() switch { ... }withUnsafe.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:switchonly after importing every arm. Anifon a JIT-time constant is folded while importing, so the unused branches never reach the IR.valueonto the stack. WithUnsafe.As(ref value), an imported but unused arm readsvaluewider than it is (e.g.ulongfrom a 4-byte enum). This forcesvalueonto the stack, and it stays there after the arm is removed. Every lookup then pays a store and a reload.Unsafe.BitCasttakes the value directly instead of its address, so it stays in a register. Theifchain keeps the unused branches from ever being imported. Together they generate the same code asmain.Performance
Measured
mainagainst this branch with BenchmarkDotNet. Each job loads its ownFastEnum.Core.dll, and the logs confirm which one each job loaded. No regression was found.Initialization (
EnumInfo<T>static constructor for 999 enum types, ColdStart, 15 launches)The ~1% difference is within the measurement error (±0.06 s).
Value lookup (ratio to
main, 1,024 keys per invocation, no allocation)GetNameIsDefined(defined)IsDefined(undefined)byte(10 values)HttpStatusCodelong(20 values)Name lookup
TryParse(ratio tomain, 1,024 names per invocation)HttpStatusCodeAn 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
🤖 Generated with Claude Code