Use more spans in `System.Reflection.Metadata` et. al. (#76574)
authorTheodore Tsirpanis <teo@tsirpanis.gr>
Thu, 3 Nov 2022 15:29:21 +0000 (17:29 +0200)
committerGitHub <noreply@github.com>
Thu, 3 Nov 2022 15:29:21 +0000 (11:29 -0400)
commitb8a0906683ad3371d68b2ce73da6e8cc0081c43b
tree108c417a7be32c983eee4f75686a7c23be54a57c
parenta20b31ab0c8b9b0aad8fbf72d1c5e91a2095adf0
Use more spans in `System.Reflection.Metadata` et. al. (#76574)

* Use Unsafe.As in ImmutableByteArrayInterop.

Code quality increases.

* Optimize methods in the BlobUtilities class.

* Include more information in exceptions thrown from catch blocks.

* Use spans in BlobContentId, avoiding ImmutableByteArrayInterop.

* Use spans in BlobWriter, reducing pinning and ImmutableArrayInterop.

* Use spans in BlobBuilder, reducing pinning and ImmutableArrayInterop.

* Remove NoInlining from the throw helpers.

It prevents the JIT from looking into the method's body, realizing it's a throw helper, and doing what's best (such as considering it cold).

* Inline `BlobUtilities.Read(Immutable)?Bytes`.

* Avoid a length check introduced by the use of `BinaryPrimitives`.

* Remove `DisallowNull` from `ImmutableArrayInterop.DangerousCreateFromUnderlyingArray`.

* Remove an early bail-out if the buffer was empty.

* Stop using value tuples.

* Fix tests and expose writing a span to BlobWriter.

* Fix stack overflows.

* Shorten BlobUtilities methods that write integers.

* Optimize some methods in `MemoryBlock`.
13 files changed:
src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/AbstractMemoryBlock.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/MemoryBlocks/StreamMemoryBlockProvider.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/BlobUtilities.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ImmutableByteArrayInterop.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/MemoryBlock.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobBuilder.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobContentId.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobWriter.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/MetadataReader.netstandard.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEReader.EmbeddedPortablePdb.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/PortableExecutable/PEReader.cs
src/libraries/System.Reflection.Metadata/src/System/Reflection/Throw.cs
src/libraries/System.Reflection.Metadata/tests/Utilities/StreamExtensionsTests.cs