From: Eric Erhardt Date: Tue, 17 May 2022 10:55:34 +0000 (-0500) Subject: Optimize ManifestResourceStream.Read(Span) (#69412) X-Git-Tag: accepted/tizen/unified/riscv/20231226.055536~9119 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f63fdeefa535a1d2fd1855249f673f12e021774d;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Optimize ManifestResourceStream.Read(Span) (#69412) ManifestResourceStream is derived from UnmanagedMemoryStream. UnmanagedMemoryStream optimizes Read(Span), but only if the instance is concretely an UnmanagedMemoryStream. This is to protect in case the derived Stream overriddes Read(byte[], int, int) prior to the Read(Span) overload being introduced. Optimize ManifestResourceStream by overriding Read(Span) and call ReadCore, so the Stream.Read(Span) logic of using a pooled byte[] buffer isn't used. --- diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs index 8fe1d3b..c6c2000 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs @@ -36,12 +36,18 @@ namespace System.Reflection private sealed class ManifestResourceStream : UnmanagedMemoryStream { + // ensures the RuntimeAssembly is kept alive for as long as the stream lives private RuntimeAssembly _manifestAssembly; internal unsafe ManifestResourceStream(RuntimeAssembly manifestAssembly, byte* pointer, long length, long capacity, FileAccess access) : base(pointer, length, capacity, access) { _manifestAssembly = manifestAssembly; } + + // override Read(Span) because the base UnmanagedMemoryStream doesn't optimize it for derived types + public override int Read(Span buffer) => ReadCore(buffer); + + // NOTE: no reason to override Write(Span), since a ManifestResourceStream is read-only. } internal object SyncRoot