Improve range checking - #95
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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.
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
This pull request refactors the implementation of range checks in the
IsDefined(T value)method for enum types inUnderlyingOperation.csand 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.