Skip to content

Replace the T4-generated UnderlyingOperation with generic math - #136

Merged
xin9le merged 7 commits into
mainfrom
refactor/goodbye-t4-template
Nov 25, 2026
Merged

xin9le merged 7 commits into
mainfrom
refactor/goodbye-t4-template

Conversation

@xin9le

@xin9le xin9le commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Summary

  • Remove the T4 template (UnderlyingOperation.tt) and its generated code, and implement the same per-underlying-type operations with generic math.
  • UnderlyingOperation is now a non-generic static class with generic methods (GetName<T>, IsDefined<T>, ToNumberString<T>, TryParseValue<T>, TryGetMember<T>). They dispatch on EnumInfo<T>.s_typeCode to GenericOperation<TUnderlying, TUnsigned, TCompute>, and each instantiation has a using alias (ByteOperation, Int32Operation, and so on).
  • TryGetMember does not use generic-math conversions such as nuint.CreateTruncating(). Their large IL used up the JIT inlining budget in deep call chains such as GetEnumMemberValue(). Instead, the index is widened with Unsafe.SizeOf() / Unsafe.As(), which the JIT folds at compile time.

Performance

Compared against main with all 61 benchmarks in FastEnum.Benchmarks: .NET 11, default job, with DisassemblyDiagnoser.

  • No regression found.
  • 50 benchmarks produce identical machine code.
  • The other 11 differ only in:
    • register allocation, with the same code size;
    • constant or data addresses;
    • code that does not run through FastEnum's UnderlyingOperation: the .NET built-in and Enums.NET baselines, string lookups, and discontinuous enums.
  • GetEnumMemberValue() produces machine code identical to main (1.923 ns → 1.943 ns, within noise).

Test

  • FastEnum.UnitTests: 635 passed on both net10.0 and net11.0.

🤖 Generated with Claude Code

@xin9le xin9le self-assigned this Nov 25, 2026
@xin9le
xin9le merged commit 1b58d04 into main Nov 25, 2026
2 checks passed
@xin9le
xin9le deleted the refactor/goodbye-t4-template branch November 25, 2026 15:12
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