-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Implement RuntimeHelpers.GetHashCode() happy path in C# #55273
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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.
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.
This is a GC hole. This byref will points to previous object. so it won't move together with the object that you are computing the hashcode for.
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.
Ouch 😅
We were just about wondering whether the GC could track refs pointing to the object header, I wasn't completely sure but figured it might work given that the data was still part of the same object - guess I know now ahah
I've tried using
fixed
there to fix that but as expected that's pretty slow and loses virtually all performance improvements than the current solution, so not really worth it anymore. Will close the PR for now then.While on the topic - @SingleAccretion found a
GT_START_NONGC
node in the emitter, and together with @EgorBo we were wondering whether it might make sense and/or be doable at all to introduce a new JIT intrinsic to be able to leverage that? Might make things like this possible without having to pin stuff and lose all performance gains? At the very least it sounds like a good learning opportunity so I thought I'd ask! Thanks! 😄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.
I do not think intrinsics for
GT_START_NONGC
make sense. It is so subtle and hard to get these things right. I have no problems with giving up the bit of performance that we would get.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.
Right, yeah that makes perfect sense and it'd also be extremely niche anyway. Thanks! 🙂
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.
Can the JIT do more optimizations around
fixed
to make it faster?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 spill to stack can be optimized. Nothing fundamental says that the pinned slot has to be on stack. The pinned value can be in register.
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 tailcall optimization can be done as well, at least in theory. I guess it may be hard to do today since we do not know whether there is anything that matters pinned when we are deciding whether to tailcall. Maybe it can be helped by moving the pinning into a separate (inlineable) method to make it easier for the JIT to see that there is nothing actually pinned to block the tailcall optimization?
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.
We have a similar issue for
RuntimeHelpers.GetMethodTable
.RuntimeHelpers.GetMethodTable
cuts corners and I believe it does not compile into as efficient code as possible. I think it would be ok to introducestatic T ReadAtByteOffset<T>(object o, int offset)
intrinsic that would readT
at given offset, without materializingo + offset
as byref, as efficiently as possible. We can then use that intrinsic for both syncblock reading andGetMethodTable
.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.
Ah, I didn't realize 👍
Yep, currently it's rejected with
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.
NOTE: @Sergio0694 can't comment here since the thread is locked, but judging by Discord #lowlevel channel he is excited where it goes 🙂