-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
ensure MetadataReader fast path works with every FileStream on every OS #50367
Conversation
src/libraries/System.Reflection.Metadata/src/System.Reflection.Metadata.csproj
Show resolved
Hide resolved
...Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/StreamMemoryBlockProvider.cs
Show resolved
Hide resolved
...ection.Metadata/src/System/Reflection/Internal/Utilities/FileStreamReadLightUp.netcoreapp.cs
Outdated
Show resolved
Hide resolved
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.
It looks good to me as is.
Consider changing the signature of TryReadFile from: bool TryReadFile(Stream stream, byte* buffer, int size, out int bytesRead) to instead be: int ReadFile(Stream stream, byte* buffer, int size) The sole caller of TryReadFile always proceeds to use the stream and the out bytesRead if TryReadFile returns false, so it seems like it'd be more understandable if instead of: int bytesRead = 0;
if (!isFileStream || !FileStreamReadLightUp.TryReadFile(stream, block.Pointer, size, out bytesRead))
{
stream.CopyTo(block.Pointer + bytesRead, size - bytesRead); the caller was: int bytesRead = 0;
if (!isFileStream ||
(bytesRead = FileStreamReadLightUp.ReadFile(stream, block.Pointer, size)) != size)
{
stream.CopyTo(block.Pointer + bytesRead, size - bytesRead); That way it's clear to the caller that the stream may have still advanced even if TryReadFile returned false, that bytesRead is still meaningful, etc., and it's clear why bytesRead is being factored into the CopyTo. |
This code predates me, but according to my understanding:
runtime/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/FileStreamReadLightUp.cs
Lines 12 to 17 in 49e5d17
ReadFile
syscall:runtime/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/FileStreamReadLightUp.cs
Line 58 in 49e5d17
sync
FileStream
s:runtime/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/FileStreamReadLightUp.cs
Lines 62 to 66 in 49e5d17
Based on the comment from #14276
It seems that this code was created before we have added a
Stream.Read(Span)
overload that allows for reading from a file to an unmanaged memory buffer.Moreover, I think that partial reads were not handled properly, as in cases when
bytesRead != requestedSize
theblock.Pointer
was not incremented properly:runtime/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/StreamMemoryBlockProvider.cs
Lines 79 to 81 in 79ae74f
fixes #14276