-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Improve performance of UnmanagedMemoryStream #93766
Conversation
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsUnmanagedMemoryStream used Interlocked operations to update its state to prevent tearing of 64-bit values on 32-bit platforms. This pattern is expensive in general and it was found to be prohibitively expensive on recent hardware.. This change removes the expensive Interlocked operations and addresses the tearing issues in alternative way:
|
da08fb6
to
d75bdf8
Compare
UnmanagedMemoryStream used Interlocked operations to update its state to prevent tearing of 64-bit values on 32-bit platforms. This pattern is expensive in general and it was found to be prohibitively expensive on recent hardware.. This change removes the expensive Interlocked operations and addresses the tearing issues in alternative way: - The _length field is converted to nuint that is guaranteed to be updated atomically. - Writes to _length field are volatile to guaranteed the unininitialized memory cannot be read. - The _position field remains long and it has a risk of tearing. It is not a problem since tearing of this field cannot lead to buffer overruns. Fixes dotnet#93624
d75bdf8
to
557306e
Compare
src/libraries/System.Private.CoreLib/src/System/IO/UnmanagedMemoryStream.cs
Show resolved
Hide resolved
Thank you for the fix. Will the fix be part of .NET8? Will it also be ported to .NET Framework? |
I will do a bar-check about backporting this fix to .NET 8.
I do not expect this fix to be ported to .NET Framework. Performance fixes are ported to .NET Framework only in very exceptional situations. |
/backport to release/8.0-staging |
Started backporting to release/8.0-staging: https://github.com/dotnet/runtime/actions/runs/6596033333 |
The fix was approved for .NET 8. #93812 |
UnmanagedMemoryStream used Interlocked operations to update its state to prevent tearing of 64-bit values on 32-bit platforms. This pattern is expensive in general and it was found to be prohibitively expensive on recent hardware..
This change removes the expensive Interlocked operations and addresses the tearing issues in alternative way:
Fixes #93624