New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid boxing while unpacking flags #8882
Conversation
Enum.HasFlag boxes its argument. Replace it with a method that does not.
Codecov Report
@@ Coverage Diff @@
## master #8882 +/- ##
==========================================
- Coverage 56.03% 55.78% -0.25%
==========================================
Files 922 916 -6
Lines 65944 65447 -497
Branches 12070 11988 -82
==========================================
- Hits 36951 36509 -442
- Misses 25985 25986 +1
+ Partials 3008 2952 -56
Flags with carried forward coverage won't be shown. Click here to find out more. |
private bool HasFlag(Flags flags) | ||
{ | ||
// NOTE Enum.HasFlag boxes its argument | ||
return (flags & _flags) == flags; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW: The name of the enum Flags
is misleading. It should be in singular.
I can read this better:
private bool HasFlag(Flags flags) | |
{ | |
// NOTE Enum.HasFlag boxes its argument | |
return (flags & _flags) == flags; | |
} | |
private bool HasFlag(Flags flag) | |
{ | |
// NOTE Enum.HasFlag boxes its argument | |
return (_flags & flag) == flag; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic works equally well with multiple flags. The type is a flags enum, and has a plural name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic works equally well with multiple flags.
This was clear to me. But Enum.HasFlag
uses only singular. And nothing else is used here.
Better readability (as in other recent PRs):
private bool HasFlag(Flags flags) | |
{ | |
// NOTE Enum.HasFlag boxes its argument | |
return (flags & _flags) == flags; | |
} | |
private bool HasFlag(Flags flags) | |
{ | |
// NOTE Enum.HasFlag boxes its argument | |
return (_flags & flags) == flags; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Agree with mstv
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC .NET Core has resolved these allocations, hasn't it? #8522 makes this change obsolete.
@RussKie what change are you looking for here? |
I believe it was a comment that the change is not necessary as .NET5 should be coming soon |
Proposed changes
Eliminate boxing when in
GitItemStatus
bool property getters.Enum.HasFlag
boxes its argument. Replace it with a method that does not.Test methodology
✒️ I contribute this code under The Developer Certificate of Origin.