From f7c68ef28732df9123ea25a9c1e70bab21f11e79 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 1 Oct 2022 16:25:59 +0200 Subject: [PATCH] Minor code tweaks, remove readonly tricks --- .../Buffers/ArrayPoolBinaryWriterExtensions.cs | 14 +++++++------- .../Interop/Buffers/ArrayPoolBinaryWriter{T}.cs | 14 +++++++------- .../Shaders/Interop/D2D1PixelShaderEffect.cs | 12 +++++++++--- .../__Internals/ArrayPoolStringBuilder.cs | 2 +- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs index 71ba9ed1..0ca1d077 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs @@ -23,7 +23,7 @@ internal static class ArrayPoolBinaryWriterExtensions /// The target instance to write data to. /// The value to write. /// This method will just blit the data of into the target buffer. - public static unsafe void WriteRaw(this in ArrayPoolBufferWriter writer, in T value) + public static unsafe void WriteRaw(this ref ArrayPoolBufferWriter writer, scoped in T value) where T : unmanaged { Span span = writer.GetSpan(sizeof(T)); @@ -39,7 +39,7 @@ internal static class ArrayPoolBinaryWriterExtensions /// The type of value to write. /// The target instance to write data to. /// The value to write. - public static unsafe void WriteRaw(this in ArrayPoolBufferWriter writer, in T value) + public static unsafe void WriteRaw(this ref ArrayPoolBufferWriter writer, scoped in T value) where T : unmanaged { Span span = writer.GetSpan(1); @@ -55,7 +55,7 @@ internal static class ArrayPoolBinaryWriterExtensions /// The type of values in the target writer. /// The target instance to write data to. /// The data to write. - public static void WriteRaw(this in ArrayPoolBufferWriter writer, ReadOnlySpan data) + public static void WriteRaw(this ref ArrayPoolBufferWriter writer, scoped ReadOnlySpan data) where T : unmanaged { Span span = writer.GetSpan(data.Length); @@ -71,7 +71,7 @@ internal static class ArrayPoolBinaryWriterExtensions /// /// The target instance to write data to. /// The data to write. - public static void WriteRaw(this in ArrayPoolBufferWriter writer, string data) + public static void WriteRaw(this ref ArrayPoolBufferWriter writer, string data) { Span span = writer.GetSpan(data.Length); @@ -86,7 +86,7 @@ internal static class ArrayPoolBinaryWriterExtensions /// /// The target instance to write data to. /// The value to write. - public static void WriteAsUtf8(this in ArrayPoolBufferWriter writer, int value) + public static void WriteAsUtf8(this ref ArrayPoolBufferWriter writer, int value) { #if NET6_0_OR_GREATER // Get a span of at least 10 elements (10 is the length of int.MaxValue as text) @@ -106,7 +106,7 @@ internal static class ArrayPoolBinaryWriterExtensions /// /// The target instance to write data to. /// The text to write. - public static void WriteAsUtf8(this in ArrayPoolBufferWriter writer, string text) + public static void WriteAsUtf8(this ref ArrayPoolBufferWriter writer, string text) { int maximumByteSize = Encoding.UTF8.GetMaxByteCount(text.Length); @@ -122,7 +122,7 @@ internal static class ArrayPoolBinaryWriterExtensions /// /// The target instance to write data to. /// The value to write. - public static void WriteAsUnicode(this in ArrayPoolBufferWriter writer, int value) + public static void WriteAsUnicode(this ref ArrayPoolBufferWriter writer, int value) { #if NET6_0_OR_GREATER // Get a span of at least 10 elements (10 is the length of int.MaxValue as text) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs index 3d56598d..db8ab67c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs @@ -35,7 +35,7 @@ internal ref struct ArrayPoolBufferWriter /// /// The starting offset within . /// - private readonly int index; + private int index; /// /// Initializes a new instance of the class. @@ -74,7 +74,7 @@ internal ref struct ArrayPoolBufferWriter /// Must be called after . /// This and the methods below are to enable mutating extensions. /// - internal readonly void Advance(int count) + internal void Advance(int count) { T[]? array = this.array; @@ -93,7 +93,7 @@ internal ref struct ArrayPoolBufferWriter ThrowArgumentExceptionForAdvancedTooFar(); } - Unsafe.AsRef(in this.index) += count; + this.index += count; } /// @@ -102,7 +102,7 @@ internal ref struct ArrayPoolBufferWriter /// The capacity to request. /// A to write data to. [UnscopedRef] - internal readonly Span GetSpan(int sizeHint = 0) + internal Span GetSpan(int sizeHint = 0) { CheckBufferAndEnsureCapacity(sizeHint); @@ -114,7 +114,7 @@ internal ref struct ArrayPoolBufferWriter /// /// The minimum number of items to ensure space for in . [MethodImpl(MethodImplOptions.AggressiveInlining)] - private readonly void CheckBufferAndEnsureCapacity(int sizeHint) + private void CheckBufferAndEnsureCapacity(int sizeHint) { T[]? array = this.array; @@ -144,7 +144,7 @@ internal ref struct ArrayPoolBufferWriter /// /// The minimum number of items to ensure space for in . [MethodImpl(MethodImplOptions.NoInlining)] - private readonly unsafe void ResizeBuffer(int sizeHint) + private unsafe void ResizeBuffer(int sizeHint) { uint minimumSize = (uint)this.index + (uint)sizeHint; @@ -164,7 +164,7 @@ internal ref struct ArrayPoolBufferWriter ArrayPool.Shared.Return(this.array!); - Unsafe.AsRef(in this.array) = newArray; + this.array = newArray; } /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 8e2704c8..52f4044e 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -82,7 +82,7 @@ public static unsafe class D2D1PixelShaderEffect PixelShaderEffect.For.Initialize(mapperFactory); - using ArrayPoolBufferWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); + ArrayPoolBufferWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); // Build the XML text writer.WriteRaw(""" @@ -263,6 +263,8 @@ public static unsafe class D2D1PixelShaderEffect effectId = PixelShaderEffect.For.Id; } + + writer.Dispose(); } /// @@ -342,7 +344,7 @@ public static unsafe class D2D1PixelShaderEffect PixelShaderEffect.For.Initialize(mapperFactory); - using ArrayPoolBufferWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); + ArrayPoolBufferWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); // Blob id writer.WriteRaw(D2D1EffectRegistrationData.V1.BlobId); @@ -514,7 +516,11 @@ public static unsafe class D2D1PixelShaderEffect effectId = PixelShaderEffect.For.Id; - return writer.WrittenSpan.ToArray(); + byte[] registrationBlob = writer.WrittenSpan.ToArray(); + + writer.Dispose(); + + return registrationBlob; } /// diff --git a/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs b/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs index 2e9ef06e..7f2bc9f2 100644 --- a/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs +++ b/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs @@ -44,7 +44,7 @@ public ref struct ArrayPoolStringBuilder /// Gets the data written to the underlying buffer so far, as a . /// [UnscopedRef] - public ReadOnlySpan WrittenSpan + public readonly ReadOnlySpan WrittenSpan { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => new(this.array, 0, this.index);