Repository navigation
Lazy initialization for Member<T>.NameUtf8 / EnumMemberAttribute / Labels - #128
Merged
Merged
Conversation
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
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.NameUtf8Encoding.UTF8.GetBytes(name)on every constructionEnumMemberAttributeGetCustomAttribute<EnumMemberAttribute>()on every constructionLabels(internal)GetCustomAttributes<LabelAttribute>()+ToFrozenDictionary()on every constructionGetLabel())Implementation notes
Interlocked.CompareExchangeensures that only one instance is ever published, even when several threads initialize at the same time.ImmutableArray<T>compares its underlying arrays by reference, soNameUtf8must always return the same array.GetCustomAttribute()returns a new instance on every call, soEnumMemberAttributealso keeps a single reference.EnumMemberAttribute:nullis 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).boolflag would makeMember<T>larger for enums with an 8-byte underlying type, so a sentinel is used instead.Member<T>is unchanged.Performance
mainvs. 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.ColdStartlaunched 40 processes, each measuring a single call (Warmup 0, Iteration 1, Invocation 1).FastEnum.GetValues<T>()for 100 enum types. The reported time is per enum type.FastEnum.Coreis loaded into the defaultAssemblyLoadContextand called directly, without reflection.MsBuildArgument.GC.GetAllocatedBytesForCurrentThread(). Under ColdStart,MemoryDiagnoserwould measure a second, already-initialized call.2. Accessors after initialization (default job)
GetLabel()GetEnumMemberValue()Member<T>.NameUtf8Notes
JitOptimization.EnumKeyDictionary.t_pendingchanged frominternaltopublic. The enclosingJitOptimizationclass isinternal, so nothing new is exposed outside the assembly.🤖 Generated with Claude Code