diff --git a/src/ComputeSharp.SourceGeneration/Helpers/ImmutableArrayBuilder{T}.cs b/src/ComputeSharp.SourceGeneration/Helpers/ImmutableArrayBuilder{T}.cs index d682f0cf..a87e467b 100644 --- a/src/ComputeSharp.SourceGeneration/Helpers/ImmutableArrayBuilder{T}.cs +++ b/src/ComputeSharp.SourceGeneration/Helpers/ImmutableArrayBuilder{T}.cs @@ -5,35 +5,34 @@ using System; using System.Collections.Immutable; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; namespace ComputeSharp.SourceGeneration.Helpers; /// -/// A helper type to build instances with pooled buffers. +/// A helper type to build sequences of values with pooled buffers. /// -/// The type of items to create arrays for. +/// The type of items to create sequences for. internal ref struct ImmutableArrayBuilder { /// - /// The shared instance to share objects. + /// The shared instance to share objects. /// - private static readonly ObjectPool.Builder> sharedObjectPool = new(ImmutableArray.CreateBuilder); + private static readonly ObjectPool sharedObjectPool = new(static () => new Writer()); /// /// The owner instance. /// - private readonly ObjectPool.Builder> objectPool; + private readonly ObjectPool objectPool; /// - /// The rented instance to use. + /// The rented instance to use. /// - private ImmutableArray.Builder? builder; + private Writer? writer; /// - /// Rents a new pooled instance through a new value. + /// Creates a value with a pooled underlying data writer. /// - /// A to interact with the underlying instance. + /// A instance to write data to. public static ImmutableArrayBuilder Rent() { return new(sharedObjectPool, sharedObjectPool.Allocate()); @@ -42,107 +41,156 @@ internal ref struct ImmutableArrayBuilder /// /// Creates a new object with the specified parameters. /// - /// - /// - private ImmutableArrayBuilder(ObjectPool.Builder> objectPool, ImmutableArray.Builder builder) + /// The target to return to. + /// The target data writer to use. + private ImmutableArrayBuilder(ObjectPool objectPool, Writer writer) { this.objectPool = objectPool; - this.builder = builder; - } - - /// - public readonly int Count - { - get => this.builder!.Count; + this.writer = writer; } /// public readonly void Add(T item) { - this.builder!.Add(item); + this.writer!.Add(item); } /// /// Adds the specified items to the end of the array. /// /// The items to add at the end of the array. - public readonly unsafe void AddRange(ReadOnlySpan items) + public readonly void AddRange(ReadOnlySpan items) { - if (items.IsEmpty) - { - return; - } - - int offset = this.builder!.Count; - - this.builder!.Count += items.Length; - - ref T firstItem = ref Unsafe.AsRef(in this.builder!.ItemRef(offset)); - - if (typeof(T) == typeof(char)) - { - int sizeInBytes = checked(items.Length * Unsafe.SizeOf()); - - fixed (void* source = &Unsafe.As(ref MemoryMarshal.GetReference(items))) - fixed (void* destination = &Unsafe.As(ref firstItem)) - { - new ReadOnlySpan(source, sizeInBytes).CopyTo(new Span(destination, sizeInBytes)); - } - } - else - { - ref T lastItem = ref Unsafe.Add(ref firstItem, items.Length); - ref T sourceItem = ref MemoryMarshal.GetReference(items); - - while (Unsafe.IsAddressLessThan(ref firstItem, ref lastItem)) - { - firstItem = sourceItem; - - firstItem = ref Unsafe.Add(ref firstItem, 1); - sourceItem = ref Unsafe.Add(ref sourceItem, 1); - } - } + this.writer!.AddRange(items); } /// public readonly ImmutableArray ToImmutable() { - return this.builder!.ToImmutable(); + T[] array = this.writer!.WrittenSpan.ToArray(); + + return Unsafe.As>(ref array); } /// public readonly T[] ToArray() { - return this.builder!.ToArray(); + return this.writer!.WrittenSpan.ToArray(); } /// - public override readonly unsafe string ToString() + public override readonly string ToString() { - if (typeof(T) == typeof(char) && - this.builder!.Count > 0) - { - fixed (char* p = &Unsafe.As(ref Unsafe.AsRef(in this.builder!.ItemRef(0)))) - { - return new ReadOnlySpan(p, this.builder!.Count).ToString(); - } - } - - return this.builder!.ToString(); + return this.writer!.WrittenSpan.ToString(); } /// public void Dispose() { - ImmutableArray.Builder? builder = this.builder; + Writer? writer = this.writer; - this.builder = null; + this.writer = null; - if (builder is not null) + if (writer is not null) { - builder.Clear(); + writer.Clear(); - this.objectPool.Free(builder); + this.objectPool.Free(writer); + } + } + + /// + /// A class handling the actual buffer writing. + /// + private sealed class Writer + { + /// + /// The underlying array. + /// + private T[] array; + + /// + /// The starting offset within . + /// + private int index; + + /// + /// Creates a new instance with the specified parameters. + /// + public Writer() + { + this.array = new T[8]; + this.index = 0; + } + + /// + /// Gets the data written to the underlying buffer so far, as a . + /// + public ReadOnlySpan WrittenSpan + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => new(this.array, 0, this.index); + } + + /// + public void Add(T value) + { + EnsureCapacity(1); + + this.array[this.index++] = value; + } + + /// + public void AddRange(ReadOnlySpan items) + { + EnsureCapacity(items.Length); + + items.CopyTo(this.array.AsSpan(this.index)); + + this.index += items.Length; + } + + /// + /// Clears the items in the current writer. + /// + public void Clear() + { + if (typeof(T) != typeof(char)) + { + this.array.AsSpan(0, this.index).Clear(); + } + + this.index = 0; + } + + /// + /// Ensures that has enough free space to contain a given number of new items. + /// + /// The minimum number of items to ensure space for in . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void EnsureCapacity(int requestedSize) + { + if (requestedSize > this.array.Length - this.index) + { + ResizeBuffer(requestedSize); + } + } + + /// + /// Resizes to ensure it can fit the specified number of new items. + /// + /// The minimum number of items to ensure space for in . + [MethodImpl(MethodImplOptions.NoInlining)] + private void ResizeBuffer(int sizeHint) + { + int minimumSize = this.index + sizeHint; + int requestedSize = Math.Max(this.array.Length * 2, minimumSize); + + T[] newArray = new T[requestedSize]; + + Array.Copy(this.array, newArray, this.index); + + this.array = newArray; } } } \ No newline at end of file