Repository navigation
⚠️ Breaking changes
- Target frameworks are now
net10.0andnet11.0. .NET 8 and .NET 9 are no longer supported.
🚀 Performance improvements
The Reflection API (FastEnum.Xxx<T>() and the extension methods) has been reworked internally. The public API is unchanged.
Faster initialization, less memory
The first call for each enum type is much cheaper, because:
- LINQ and
FrozenDictionaryare no longer used to build the lookup tables, so less code is JIT-compiled for each enum type. - The lookup table by value is no longer built when the values are continuous.
EnumMemberAttribute,LabelAttributeand the UTF-8 name are now read the first time they are used, not up front.
Measured on the first call of each API for an enum type (median of 30 runs, each in a new process):
| First API called | 16 fields (continuous) | 16 fields (discontinuous) | 128 fields (continuous) | 128 fields (discontinuous) |
|---|---|---|---|---|
GetName |
17.7 → 3.1 ms (5.6x) | 18.2 → 7.4 ms (2.5x) | 18.8 → 3.3 ms (5.7x) | 19.4 → 7.5 ms (2.6x) |
TryParse |
17.8 → 2.9 ms (6.1x) | 17.8 → 6.8 ms (2.6x) | 18.6 → 3.0 ms (6.2x) | 18.7 → 6.8 ms (2.7x) |
GetMembers |
17.3 → 2.7 ms (6.4x) | 17.6 → 6.6 ms (2.7x) | 18.3 → 2.9 ms (6.4x) | 18.6 → 6.7 ms (2.8x) |
For the very first enum type in a process, which includes loading FastEnum itself, GetName goes from 41 ms to 12–17 ms (2.5–3.4x faster).
| Memory per enum type | 16 fields (continuous) | 16 fields (discontinuous) | 128 fields (continuous) | 128 fields (discontinuous) |
|---|---|---|---|---|
| Allocated during initialization | 18.7 → 7.6 KB (−59%) | 21.2 → 11.0 KB (−48%) | 189 → 55 KB (−71%) | 209 → 64 KB (−69%) |
| Retained after GC | 9.4 → 6.5 KB (−31%) | 12.0 → 9.9 KB (−18%) | 59.5 → 47.9 KB (−20%) | 79.9 → 57.1 KB (−29%) |
Faster lookups
A new hash table built for enum values replaces FrozenDictionary. Comparing names while ignoring case is also faster.
| API | v2.0.8 | v2.1.0 | Change |
|---|---|---|---|
GetMember() / ToMember() (discontinuous values) |
2.04 ns | 1.03 ns | −50% |
ToString() / FastToString() (discontinuous values) |
1.68 ns | 1.14 ns | −32% |
GetLabel() (discontinuous values) |
3.00 ns | 2.10 ns | −30% |
GetName() / ToName() (discontinuous values) |
1.29 ns | 1.07 ns | −17% |
TryParse(ignoreCase: true) |
9.00 ns | 7.58 ns | −16% |
TryParse(ignoreCase: true) (constant input) |
7.56 ns | 4.36 ns | −42% |
The other APIs perform the same as before.
Note
Because attributes are now read the first time they are used, GetEnumMemberValue() and GetLabel() can be about 0.3 ns slower than before.
How it was measured
- AMD EPYC 7763, Windows 11, .NET 11.0.0, BenchmarkDotNet 0.16.0-preview.2
- v2.0.8 has no
net11.0build, so itsnet10.0build was used. This is what a .NET 11 app gets from the v2.0.8 package. - Initialization times include JIT compilation, and the absolute values depend on the machine.
- The Source Generator version (
FastEnum.Generators) is not included, because it has not changed.
🙏 Contributors
Thanks to @akeit0 for #131 and #132!
📝 What's Changed
- Fix
.GetName()/.GetMember()/.ToString()returning a wrong member for large 64-bit values by @xin9le in #110 - Speed up case-insensitive
.TryParse()with an ASCII fast path by @xin9le in #111 - Add non constant input benchmark by @xin9le in #112
- Hello, .NET 11 & Goodbye .NET 8 / .NET 9 by @xin9le in #113
- Minor refactorings by @xin9le in #114
- Replace
FrozenDictionarywithEnumKeyDictionaryfor value lookup of non-continuous enums by @xin9le in #115 - Use
nuintindices for array lookups to eliminate sign/zero-extension instructions by @xin9le in #116 - Benchmark under .NET 11 by @xin9le in #117
- Remove the table load and
nullcheck fromEnumKeyDictionarylookups by @xin9le in #118 - Skip building the value dictionary for continuous enums by @xin9le in #119
- Rename:
EnumKeyDictionary->EnumKeyDictionaryHelperby @xin9le in #120 - Take
EnumInfo<T>min / max from the sorted members instead of LINQ by @xin9le in #121 - Reduce
Member<T>initialization cost by @xin9le in #122 - Fix the bucket count of the case-insensitive name dictionary and build the name dictionaries without LINQ by @xin9le in #123
- Replace LINQ in building s_orderedMembers to cut per-enum initialization cost by half by @xin9le in #124
- Eliminate per-enum JIT cost in dictionary construction via
IMemberName/IMemberValue<T>by @xin9le in #125 - Consolidate per-enum JIT helpers into
JitOptimizationand keepToUInt64JIT-friendly by @xin9le in #126 MemberHelperis moved toJitOptimizationby @xin9le in #127- Lazy initialization for
Member<T>.NameUtf8/EnumMemberAttribute/Labelsby @xin9le in #128 - Remove unnecessary
AggressiveInliningby @xin9le in #129 - Refactor
FastEnumBoosterGenerator.Generateinto static local functions by @xin9le in #130 - Use generic Enum.GetValues for Native AOT by @akeit0 in #131
- Update description for source code generation by @xin9le in #133
- Share enum initialization by underlying width by @akeit0 in #132
- Refactor
EnumInfo<T>initialization and member ordering by @xin9le in #134 - Declare Native AOT compatibility for
FastEnum.Coreby @xin9le in #135 - Replace the T4-generated
UnderlyingOperationwith generic math by @xin9le in #136
Full Changelog: v2.0.8...v2.1.0