Skip to content
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

Switching Enum.HasFlag calls with bitwise operations #953

Merged
merged 4 commits into from
Jan 25, 2025

Conversation

TalZaccai
Copy link
Contributor

Switching a few areas in the code from using Enum.HasFlag (which apparently uses reflection and thus incur overhead) to using more efficient bitwise operations.

@TalZaccai TalZaccai requested a review from TedHartMS January 25, 2025 00:06
@TalZaccai TalZaccai merged commit 9a48a1c into main Jan 25, 2025
18 checks passed
@TalZaccai TalZaccai deleted the talzacc/enum_hasflag branch January 25, 2025 00:59
@PaulusParssinen
Copy link
Contributor

PaulusParssinen commented Jan 25, 2025

Enum.HasFlag has been intrinsic since 2017. The codegen should be same as bitwise check. It doesn't have to do reflection if the call HasFlag call targets the generic overload. I suggest reverting this PR.

@TalZaccai
Copy link
Contributor Author

Enum.HasFlag has been intrinsic since 2017. The codegen should be same as bitwise check. It doesn't have to do reflection if the call HasFlag call targets the generic overload. I suggest reverting this PR.

Interesting... I've definitely seen unnecessary allocations when running a profiler, which I don't see any longer, so I'm not sure why that would be the case.

@PaulusParssinen
Copy link
Contributor

PaulusParssinen commented Jan 25, 2025

Interesting... I've definitely seen unnecessary allocations when running a profiler, which I don't see any longer, so I'm not sure why that would be the case.

It shouldn't be boxing in even in debug mode or Tier-0 codegen anymore (I could be wrong here). But if there clearly is difference with profiler runs I think the changes should then stay. It definitely gets eliminated in Tier-1 (i.e. fully optimized hot code) 😅

update: checked few places, the change that optimized HasFlag for tier-0 (i.e. unoptimized code) was reverted shortly after due to some concerns. I think this change is good after all. A static HasFlag helper to do this bitwise could be useful in future.

@TalZaccai
Copy link
Contributor Author

That's good to know! Thanks for looking into it!

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.

3 participants