Skip to content

Switching Enum.HasFlag calls with bitwise operations#953

Merged
TalZaccai merged 4 commits into
mainfrom
talzacc/enum_hasflag
Jan 25, 2025
Merged

Switching Enum.HasFlag calls with bitwise operations#953
TalZaccai merged 4 commits into
mainfrom
talzacc/enum_hasflag

Conversation

@TalZaccai
Copy link
Copy Markdown
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.

Comment thread libs/server/Objects/Types/GarnetObjectStoreOutput.cs Outdated
@TalZaccai TalZaccai requested a review from TedHartMS January 25, 2025 00:06
@TalZaccai TalZaccai merged commit 9a48a1c into main Jan 25, 2025
@TalZaccai TalZaccai deleted the talzacc/enum_hasflag branch January 25, 2025 00:59
@PaulusParssinen
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@github-actions github-actions Bot locked and limited conversation to collaborators Mar 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants