From 90c32d4fd35c926f443f33fed4ece428d56cbeb1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 17 Aug 2022 15:35:51 +0200 Subject: [PATCH] Make ArrayPoolBinaryWriter generic, add extensions --- .../ArrayPoolBinaryWriterExtensions.cs | 85 ++++++++++++ ...yWriter.cs => ArrayPoolBinaryWriter{T}.cs} | 124 +++++------------- .../Shaders/Interop/D2D1PixelShaderEffect.cs | 2 +- 3 files changed, 119 insertions(+), 92 deletions(-) create mode 100644 src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs rename src/ComputeSharp.D2D1/Shaders/Interop/Buffers/{ArrayPoolBinaryWriter.cs => ArrayPoolBinaryWriter{T}.cs} (62%) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs new file mode 100644 index 00000000..a187cc4a --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs @@ -0,0 +1,85 @@ +using System; +#if NET6_0_OR_GREATER +using System.Buffers.Text; +#endif +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; +#if !NET6_0_OR_GREATER +using ComputeSharp.D2D1.NetStandard.System.Text; +#endif + +namespace ComputeSharp.D2D1.Shaders.Interop.Buffers; + +/// +/// Helpers for the type. +/// +internal static class ArrayPoolBinaryWriterExtensions +{ + /// + /// Writes the raw value of type into the buffer. + /// + /// The type of value to write. + /// 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, scoped in T value) + where T : unmanaged + { + Span span = writer.GetSpan(sizeof(T)); + + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(span), value); + + writer.Advance(sizeof(T)); + } + + /// + /// Writes the raw data from the input into the buffer. + /// + /// The target instance to write data to. + /// The data to write. + public static void WriteRaw(this in ArrayPoolBufferWriter writer, scoped ReadOnlySpan data) + { + Span span = writer.GetSpan(data.Length); + + data.CopyTo(span); + + writer.Advance(data.Length); + } + + /// + /// Writes an value as UTF8 bytes into the buffer. + /// + /// The target instance to write data to. + /// The value to write. + public static void WriteAsUtf8(this in 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) + Span span = writer.GetSpan(10); + + _ = Utf8Formatter.TryFormat(value, span, out int bytesWritten); + + writer.Advance(bytesWritten); +#else + // Just accept the allocation on .NET Standard 2.0 + writer.WriteAsUtf8(value.ToString()); +#endif + } + + /// + /// Writes text as UTF8 bytes into the buffer. + /// + /// The target instance to write data to. + /// The text to write. + public static void WriteAsUtf8(this in ArrayPoolBufferWriter writer, string text) + { + int maximumByteSize = Encoding.UTF8.GetMaxByteCount(text.Length); + + Span span = writer.GetSpan(maximumByteSize); + + int writtenBytes = Encoding.UTF8.GetBytes(text.AsSpan(), span); + + writer.Advance(writtenBytes); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs similarity index 62% rename from src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs rename to src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs index 002285fd..c59ef460 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs @@ -2,33 +2,35 @@ using System.Buffers; using System.Diagnostics.CodeAnalysis; #if NET6_0_OR_GREATER -using System.Buffers.Text; using System.Numerics; #endif using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Text; #if !NET6_0_OR_GREATER -using ComputeSharp.D2D1.NetStandard.System.Text; using BitOperations = ComputeSharp.D2D1.NetStandard.System.Buffers.BitOperations; #endif namespace ComputeSharp.D2D1.Shaders.Interop.Buffers; -/// -/// Represents a heap-based, array-backed output sink into which binary data can be written. -/// -internal ref struct ArrayPoolBinaryWriter +/// +internal static class ArrayPoolBinaryWriter { /// /// The default buffer size to use to expand empty arrays. /// public const int DefaultInitialBufferSize = 256; +} +/// +/// Represents a heap-based, array-backed output sink into which data can be written. +/// +/// The type of values to write. +internal ref struct ArrayPoolBufferWriter + where T : unmanaged +{ /// - /// The underlying array. + /// The underlying array. /// - private byte[]? array; + private T[]? array; /// /// The starting offset within . @@ -36,11 +38,11 @@ internal ref struct ArrayPoolBinaryWriter private int index; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - public ArrayPoolBinaryWriter(int capacity) + public ArrayPoolBufferWriter(int capacity) { - this.array = ArrayPool.Shared.Rent(capacity); + this.array = ArrayPool.Shared.Rent(capacity); this.index = 0; } @@ -48,12 +50,12 @@ internal ref struct ArrayPoolBinaryWriter /// Gets the data written to the underlying buffer so far, as a . /// [UnscopedRef] - public ReadOnlySpan WrittenSpan + public readonly ReadOnlySpan WrittenSpan { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - byte[]? array = this.array; + T[]? array = this.array; if (array is null) { @@ -64,77 +66,17 @@ internal ref struct ArrayPoolBinaryWriter } } - /// - /// Writes the raw value of type into the buffer. - /// - /// The type of value to write. - /// The value to write. - /// This method will just blit the data of into the target buffer. - public unsafe void WriteRaw(scoped in T value) - where T : unmanaged - { - Span span = GetSpan(sizeof(T)); - - Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(span), value); - - Advance(sizeof(T)); - } - - /// - /// Writes the raw data from the input into the buffer. - /// - /// The data to write. - public unsafe void WriteRaw(scoped ReadOnlySpan data) - { - Span span = GetSpan(data.Length); - - data.CopyTo(span); - - Advance(data.Length); - } - - /// - /// Writes an value as UTF8 bytes into the buffer. - /// - /// The value to write. - public void WriteAsUtf8(int value) - { -#if NET6_0_OR_GREATER - // Get a span of at least 10 elements (10 is the length of int.MaxValue as text) - Span span = GetSpan(10); - - _ = Utf8Formatter.TryFormat(value, span, out int bytesWritten); - - Advance(bytesWritten); -#else - // Just accept the allocation on .NET Standard 2.0 - WriteAsUtf8(value.ToString()); -#endif - } - - /// - /// Writes text as UTF8 bytes into the buffer. - /// - /// The text to write. - public unsafe void WriteAsUtf8(string text) - { - int maximumByteSize = Encoding.UTF8.GetMaxByteCount(text.Length); - - Span span = GetSpan(maximumByteSize); - - int writtenBytes = Encoding.UTF8.GetBytes(text.AsSpan(), span); - - Advance(writtenBytes); - } - /// /// Advances the current writer. /// /// The amount to advance. - /// Must be called after . - private void Advance(int count) + /// + /// Must be called after . + /// This and the methods below are to enable mutating extensions. + /// + internal readonly void Advance(int count) { - byte[]? array = this.array; + T[]? array = this.array; if (array is null) { @@ -151,7 +93,7 @@ internal ref struct ArrayPoolBinaryWriter ThrowArgumentExceptionForAdvancedTooFar(); } - this.index += count; + Unsafe.AsRef(in this.index) += count; } /// @@ -159,7 +101,7 @@ internal ref struct ArrayPoolBinaryWriter /// /// The capacity to request. /// A to write data to. - private Span GetSpan(int sizeHint = 0) + internal readonly Span GetSpan(int sizeHint = 0) { CheckBufferAndEnsureCapacity(sizeHint); @@ -171,9 +113,9 @@ internal ref struct ArrayPoolBinaryWriter /// /// The minimum number of items to ensure space for in . [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void CheckBufferAndEnsureCapacity(int sizeHint) + private readonly void CheckBufferAndEnsureCapacity(int sizeHint) { - byte[]? array = this.array; + T[]? array = this.array; if (array is null) { @@ -201,7 +143,7 @@ internal ref struct ArrayPoolBinaryWriter /// /// The minimum number of items to ensure space for in . [MethodImpl(MethodImplOptions.NoInlining)] - private void ResizeBuffer(int sizeHint) + private readonly void ResizeBuffer(int sizeHint) { uint minimumSize = (uint)this.index + (uint)sizeHint; @@ -215,19 +157,19 @@ internal ref struct ArrayPoolBinaryWriter minimumSize = BitOperations.RoundUpToPowerOf2(minimumSize); } - byte[] newArray = ArrayPool.Shared.Rent((int)minimumSize); + T[] newArray = ArrayPool.Shared.Rent((int)minimumSize); Buffer.BlockCopy(this.array!, 0, newArray, 0, this.index); - ArrayPool.Shared.Return(this.array!); + ArrayPool.Shared.Return(this.array!); - this.array = newArray; + Unsafe.AsRef(in this.array) = newArray; } /// public void Dispose() { - byte[]? array = this.array; + T[]? array = this.array; if (array is null) { @@ -236,7 +178,7 @@ internal ref struct ArrayPoolBinaryWriter this.array = null; - ArrayPool.Shared.Return(array); + ArrayPool.Shared.Return(array); } /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 61ec3468..cd92376b 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -328,7 +328,7 @@ public static unsafe class D2D1PixelShaderEffect PixelShaderEffect.For.Initialize(mapperFactory); - using ArrayPoolBinaryWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); + using ArrayPoolBufferWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); // Blob id writer.WriteRaw(D2D1EffectRegistrationData.V1.BlobId);