-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[SRM] Refactor reading from streams. #111323
Conversation
This removes `StreamExtensions.CopyTo` from modern .NET.
Properly implement double-checked locking, and take advantage of the fact that some APIs cannot return null.
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.
Copilot reviewed 8 out of 23 changed files in this pull request and generated no comments.
Files not reviewed (15)
- src/libraries/System.Reflection.Metadata/src/System.Reflection.Metadata.csproj: Language not supported
- src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ImmutableMemoryStream.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/StreamConstraints.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/MemoryBlockProvider.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/StreamExtensions.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/ByteArrayMemoryProvider.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/ExternalMemoryBlockProvider.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEReader.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/SectionHeader.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/StreamExtensions.netcoreapp.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/DirectoryEntry.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEHeader.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEHeaders.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/CoffHeader.cs: Evaluated as low risk
- src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEBinaryReader.cs: Evaluated as low risk
src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobReader.cs
Outdated
Show resolved
Hide resolved
@@ -194,7 +195,7 @@ public unsafe PEReader(Stream peStream, PEStreamOptions options, int size) | |||
// if the caller asked for metadata initialize the PE headers (calculates metadata offset): | |||
if ((options & PEStreamOptions.PrefetchMetadata) != 0) | |||
{ | |||
InitializePEHeaders(); | |||
_lazyPEHeaders = new PEHeaders(imageBlock, IsLoadedImage); |
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 save an unnecessary Interlocked.CompareExchange
here.
src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEReader.cs
Outdated
Show resolved
Hide resolved
If the memory provider is backed by a stream, we use that, otherwise we get a memory block and wrap it in an `UnmanagedMemoryStream`.
129415f
to
918ba99
Compare
Turns out the unification of `PEBinaryReader` and `BlobReader` can be avoided for now, and refactorings can still be made without it.
918ba99
to
ade8410
Compare
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 kept these changes to make the API shape match BlobReader
. Let me know if I should revert them.
src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobReader.cs
Outdated
Show resolved
Hide resolved
Failures are unrelated, this is ready for review. |
…/Metadata/BlobReader.cs
...lection.Metadata/src/System/Reflection/Internal/Utilities/StreamExtensions.netstandard2.0.cs
Show resolved
Hide resolved
src/libraries/System.Reflection.Metadata/src/System.Reflection.Metadata.csproj
Outdated
Show resolved
Hide resolved
/azp run runtime-wasm |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run runtime |
Azure Pipelines successfully started running 1 pipeline(s). |
Attempting to restart some CI due to hang and failures. |
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.
LGTM thanks @teo-tsirpanis for cleaning up this. We'll keep an eye out for potential regressions here.
* main: (31 commits) Fix linux-x86 build (dotnet#111861) Add FrozenDictionary specialization for integers / enums (dotnet#111886) [SRM] Refactor reading from streams. (dotnet#111323) Sign the DAC and DBI during the build process instead of in separate steps (dotnet#111416) Removing Entry2MethodDesc as it is unnecessary (dotnet#111756) Cross Product for Vector2 and Vector4 (dotnet#111265) Handle unicode in absolute URI path for combine. (dotnet#111710) Drop RequiresProcessIsolation on mcc tests (dotnet#111887) [main] Update dependencies from dotnet/roslyn (dotnet#111691) new trimmer feature System.TimeZoneInfo.Invariant (dotnet#111215) [browser] reduce msbuild memory footprint (dotnet#111751) Add debugging checks for stack overflow tests failure (dotnet#111867) Localized file check-in by OneLocBuild Task: Build definition ID 679: Build ID 2629821 (dotnet#111884) Bump main to preview2 (dotnet#111882) Avoid generic virtual dispatch for frozen collections alternate lookup (dotnet#108732) Bump main versioning to preview1 (dotnet#111880) Switch OneLoc to main (dotnet#111872) Improve docs on building ILVerify (dotnet#111851) Update Debian version to 13 (dotnet#111768) win32: add fallback to environment vars for system folder (dotnet#109673) ...
This PR refactors and streamlines the code in
System.Reflection.Metadata
that reads from streams. I originally attempted to implement my suggestion in #17088 (comment), but realized that it is not necessary, and instead of unifyingPEBinaryReader
andBlobReader
, we always use the former, and wrappingAbstractMemoryBlock
s in anUnmanagedMemoryStream
still resulted in some simplifications. I also took advantage of some newer APIs in certain cases.Fixes #17088.