diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.cs index 23b9e15c..a38c5f47 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectIdProperty.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Immutable; using System.Runtime.CompilerServices; using System.Security.Cryptography; @@ -77,46 +78,55 @@ partial class ID2D1ShaderGenerator /// The resulting effect id. private static ImmutableArray CreateDefaultEffectId(INamedTypeSymbol typeSymbol) { - // Initialize an instance using the MD5 algorithm. We use this for several reasons: - // - We don't really need security, this is just to uniquely identify types - // - The hash size is 128 bits, which is exactly the size of a GUID. - IncrementalHash incrementalHash = EffectId.incrementalHash ??= IncrementalHash.CreateHash(HashAlgorithmName.MD5); + try + { + // Initialize an instance using the MD5 algorithm. We use this for several reasons: + // - We don't really need security, this is just to uniquely identify types + // - The hash size is 128 bits, which is exactly the size of a GUID. + IncrementalHash incrementalHash = EffectId.incrementalHash ??= IncrementalHash.CreateHash(HashAlgorithmName.MD5); - string assemblyName = typeSymbol.ContainingAssembly?.Name ?? string.Empty; + string assemblyName = typeSymbol.ContainingAssembly?.Name ?? string.Empty; - using ImmutableArrayBuilder byteBuffer = ImmutableArrayBuilder.Rent(); - using ImmutableArrayBuilder charBuffer = ImmutableArrayBuilder.Rent(); + using ImmutableArrayBuilder charBuffer = ImmutableArrayBuilder.Rent(); - // Format the fully qualified name into a pooled builder to avoid the string allocation - typeSymbol.AppendFullyQualifiedMetadataName(in charBuffer); + // Format the fully qualified name into a pooled builder to avoid the string allocation + typeSymbol.AppendFullyQualifiedMetadataName(in charBuffer); - int maxTypeNameCharsLength = Encoding.UTF8.GetMaxByteCount(charBuffer.Count); - int maxAssemblyNameCharsLength = Encoding.UTF8.GetMaxByteCount(assemblyName.Length); - int maxEncodedCharsLength = Math.Max(maxTypeNameCharsLength, maxAssemblyNameCharsLength); + int maxTypeNameByteCount = Encoding.UTF8.GetMaxByteCount(charBuffer.Count); + int maxAssemblyNameByteCount = Encoding.UTF8.GetMaxByteCount(assemblyName.Length); + int maxEncodedByteCount = Math.Max(maxTypeNameByteCount, maxAssemblyNameByteCount); - byteBuffer.EnsureCapacity(maxEncodedCharsLength); + byte[] bufferUtf8 = ArrayPool.Shared.Rent(maxEncodedByteCount); - // UTF8 encode the fully qualified name first - int typeNameCharsLength = Encoding.UTF8.GetBytes(charBuffer.WrittenSpan, byteBuffer.DangerousGetArray().AsSpan()); + // UTF8 encode the fully qualified name first + int typeNameBytesWritten = Encoding.UTF8.GetBytes(charBuffer.WrittenSpan, bufferUtf8.AsSpan()); - byteBuffer.Advance(typeNameCharsLength); + // Append the UTF8 fully qualified name to the MD5 hash + incrementalHash.AppendData(bufferUtf8, 0, typeNameBytesWritten); - // Append the UTF8 fully qualified name to the MD5 hash - incrementalHash.AppendData(byteBuffer.DangerousGetArray(), 0, byteBuffer.Count); + // UTF8 encode the assembly name as well + int assemblyNameBytesWritten = Encoding.UTF8.GetBytes(assemblyName.AsSpan(), bufferUtf8.AsSpan()); - // UTF8 encode the assembly name as well - int assemblyNameCharsLength = Encoding.UTF8.GetBytes(assemblyName.AsSpan(), byteBuffer.DangerousGetArray().AsSpan()); + // Append the UTF8 assembly name to the MD5 hash + incrementalHash.AppendData(bufferUtf8, 0, assemblyNameBytesWritten); - byteBuffer.Advance(assemblyNameCharsLength); + // The state is not fully in the incremental hash, we can return the array + ArrayPool.Shared.Return(bufferUtf8); - // Append the UTF8 assembly name to the MD5 hash - incrementalHash.AppendData(byteBuffer.DangerousGetArray(), 0, byteBuffer.Count); + // Get the resulting MD5 hash (128 bits) + byte[] hash = incrementalHash.GetHashAndReset(); - // Get the resulting MD5 hash (128 bits) - byte[] hash = incrementalHash.GetHashAndReset(); + // We own this buffer, so we can just reinterpret to an immutable array + return Unsafe.As>(ref hash); + } + catch + { + // Something went wrong, throw away the current incremental hash. + // Realistically speaking, this should just never happen. + incrementalHash = null; - // We own this buffer, so we can just reinterpret to an immutable array - return Unsafe.As>(ref hash); + throw; + } } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Helpers/ImmutableArrayBuilder{T}.cs b/src/ComputeSharp.SourceGeneration/Helpers/ImmutableArrayBuilder{T}.cs index fca0f6f0..3aa5b62b 100644 --- a/src/ComputeSharp.SourceGeneration/Helpers/ImmutableArrayBuilder{T}.cs +++ b/src/ComputeSharp.SourceGeneration/Helpers/ImmutableArrayBuilder{T}.cs @@ -75,33 +75,6 @@ internal struct ImmutableArrayBuilder : IDisposable this.writer!.AddRange(items); } - /// - /// Advances the current writer by the specified amount. - /// - /// The amoutn to advance by. - public readonly void Advance(int count) - { - this.writer!.Advance(count); - } - - /// - /// Gets the array currently in use (rented from the pool). - /// - /// The array currently in use. - public readonly T[] DangerousGetArray() - { - return this.writer!.DangerousGetArray(); - } - - /// - /// Ensures that the current instance has enough free space to contain a given number of new items. - /// - /// The minimum number of items to ensure space for. - public readonly void EnsureCapacity(int requestedSize) - { - this.writer!.EnsureCapacity(requestedSize); - } - /// /// Inserts an item to the builder at the specified index. /// @@ -204,23 +177,6 @@ internal struct ImmutableArrayBuilder : IDisposable this.index += items.Length; } - /// - public void Advance(int count) - { - if (count > this.array.Length - this.index) - { - ImmutableArrayBuilder.ThrowArgumentOutOfRangeExceptionForCount(); - } - - this.index += count; - } - - /// - public T[] DangerousGetArray() - { - return this.array; - } - /// public void Insert(int index, T item) { @@ -253,9 +209,12 @@ internal struct ImmutableArrayBuilder : IDisposable 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)] - public void EnsureCapacity(int requestedSize) + private void EnsureCapacity(int requestedSize) { if (requestedSize > this.array.Length - this.index) { @@ -294,12 +253,4 @@ file static class ImmutableArrayBuilder { throw new ArgumentOutOfRangeException("index"); } - - /// - /// Throws an for "count". - /// - public static void ThrowArgumentOutOfRangeExceptionForCount() - { - throw new ArgumentOutOfRangeException("count"); - } } \ No newline at end of file