-
Notifications
You must be signed in to change notification settings - Fork 857
[SDK] Volatile reads + MetricPoint improvements #3458
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
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.
#2951 showed good perf improvement when using own mechanism, as opposed to plain locks. If we modify the SnapShot path to also use the Interlock check for IsCriticalSectionOccupied, - would that not work??
cc : @utpilla
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.
@cijothomas Possibly, but there be dragons. I'll paraphrase what @noahfalk told me.
Consider code like this...
The compiler is free/able (by spec) to rewrite that as:
That is why it is risky to try and make our own mechanism. If we just use
lockthere, order is guaranteed to not change.Uh oh!
There was an error while loading. Please reload this page.
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 think we can use
Interlocked.Exchange(or maybeVolatile.Write) instead of simply assigning that to zero to avoid that risk.There is an example on the docs for
Interlockedthat shows this this approach: https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked?view=net-6.0#examples. This example does not even useVolatile.Write. It just uses anInterlocked.Exchangeto update that value.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.
@utpilla Let's say we did it like this...
(Or different version with a mix of
InterlockedvsVolatile.)That would work (I think) but would also be a lot slower for the happy-path?
I thought maybe Thread.MemoryBarrier would help us. But the docs do recommend a
lockover that 🤷Let's say we did it like this...
I don't think that works. Because the stuff in unchecked could be cached. Needs some kind of a memory fence. But I could be wrong this stuff is confusing.
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.
When I suggested using
Interlocked, I didn't mean that we should switch every statement to useInterlockedlike shown here:I was indeed referring to this:
How does using a
lockinstead help with this? ^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.
@utpilla
Good question. It is magic 😄 @noahfalk Can you provide a little detail here? My understanding is that the compiler(?) treats the whole
lockbody as fenced/non-reordering?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 think it should be okay even if the instructions inside the
uncheckedblock are re-ordered.But does memory barrier/fence even help with caching/freshness?