ensure MetadataReader fast path works with every FileStream on every OS#50367
Merged
adamsitnik merged 4 commits intodotnet:mainfrom Mar 31, 2021
Merged
ensure MetadataReader fast path works with every FileStream on every OS#50367adamsitnik merged 4 commits intodotnet:mainfrom
adamsitnik merged 4 commits intodotnet:mainfrom
Conversation
jkotas
reviewed
Mar 29, 2021
src/libraries/System.Reflection.Metadata/src/System.Reflection.Metadata.csproj
Show resolved
Hide resolved
krwq
reviewed
Mar 30, 2021
...Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/StreamMemoryBlockProvider.cs
Show resolved
Hide resolved
krwq
reviewed
Mar 30, 2021
...ection.Metadata/src/System/Reflection/Internal/Utilities/FileStreamReadLightUp.netcoreapp.cs
Outdated
Show resolved
Hide resolved
Member
|
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. |
stephentoub
approved these changes
Mar 30, 2021
stephentoub
approved these changes
Mar 30, 2021
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
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
ReadFilesyscall:runtime/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/FileStreamReadLightUp.cs
Line 58 in 49e5d17
syncFileStreams: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 != requestedSizetheblock.Pointerwas not incremented properly:runtime/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/StreamMemoryBlockProvider.cs
Lines 79 to 81 in 79ae74f
fixes #14276