Skip to content

Improve range checking - #95

Merged
xin9le merged 3 commits into
mainfrom
perf/range-check
Jul 7, 2026
Merged

Improve range checking#95
xin9le merged 3 commits into
mainfrom
perf/range-check

Conversation

@xin9le

@xin9le xin9le commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Summary

This pull request refactors the implementation of range checks in the IsDefined(T value) method for enum types in UnderlyingOperation.cs and its code generator template. The main improvement is replacing the previous two-comparison check for continuous enums with a more efficient single unsigned comparison that leverages wraparound semantics. This change is applied consistently across all relevant numeric types.

Benchmark results

Approximately x1.05 faster.

BenchmarkDotNet v0.15.8, Windows 11 (10.0.26200.8737/25H2/2025Update/HudsonValley2)
Intel Core Ultra 7 155H 3.00GHz, 1 CPU, 22 logical and 16 physical cores
.NET SDK 10.0.301
  [Host]     : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3
  DefaultJob : .NET 10.0.9 (10.0.9, 10.0.926.27113), X64 RyuJIT x86-64-v3


| Method            | Mean     | Error    | StdDev   | Median   | Ratio | RatioSD | Allocated | Alloc Ratio |
|------------------ |---------:|---------:|---------:|---------:|------:|--------:|----------:|------------:|
| RangeCheck_Simple | 31.21 ns | 1.060 ns | 3.077 ns | 30.02 ns |  1.01 |    0.14 |         - |          NA |
| RangeCheck_Cast   | 29.82 ns | 0.779 ns | 2.296 ns | 29.59 ns |  0.96 |    0.12 |         - |          NA |

@xin9le
xin9le requested a review from Copilot July 7, 2026 14:17
@xin9le xin9le self-assigned this Jul 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request refactors UnderlyingOperation<T>.IsDefined(T value) for continuous enums to use a single unsigned wraparound comparison instead of two ordered comparisons, aiming to improve performance (notably for hot-path IsDefined calls).

Changes:

  • Updated the T4 template (UnderlyingOperation.tt) to generate a single unsigned range check based on (val - min) <= (max - min) wraparound semantics.
  • Updated the generated implementation (UnderlyingOperation.cs) across all underlying numeric types to use the new range-check pattern.
  • Added explanatory inline comments describing the wraparound-based approach.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/libs/FastEnum.Core/Internals/UnderlyingOperation.tt Updates the generator template to emit the new single unsigned wraparound range-check logic.
src/libs/FastEnum.Core/Internals/UnderlyingOperation.cs Applies the generated wraparound range-check logic to each underlying-type specialization of IsDefined.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/libs/FastEnum.Core/Internals/UnderlyingOperation.tt Outdated
Comment thread src/libs/FastEnum.Core/Internals/UnderlyingOperation.cs Outdated
@xin9le
xin9le merged commit 23f5a91 into main Jul 7, 2026
3 checks passed
@xin9le
xin9le deleted the perf/range-check branch July 7, 2026 14:54
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.

2 participants