Use pooled array builders in all generators
このコミットが含まれているのは:
1 親
d4b1e71c39
コミット
883d99ca9f
31ファイル変更
+320
-205
検索条件に一致するファイルはありません
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Immutable;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using ComputeSharp.SourceGeneration.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
@@ -17,7 +18,7 @@ internal static class DiagnosticsExtensions
|
||||
/// <param name="symbol">The source <see cref="ISymbol"/> to attach the diagnostics to.</param>
|
||||
/// <param name="args">The optional arguments for the formatted message to include.</param>
|
||||
public static void Add(
|
||||
this ImmutableArray<DiagnosticInfo>.Builder diagnostics,
|
||||
this ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
|
||||
DiagnosticDescriptor descriptor,
|
||||
ISymbol symbol,
|
||||
params object[] args)
|
||||
@@ -33,7 +34,7 @@ internal static class DiagnosticsExtensions
|
||||
/// <param name="node">The source <see cref="SyntaxNode"/> to attach the diagnostics to.</param>
|
||||
/// <param name="args">The optional arguments for the formatted message to include.</param>
|
||||
public static void Add(
|
||||
this ImmutableArray<DiagnosticInfo>.Builder diagnostics,
|
||||
this ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
|
||||
DiagnosticDescriptor descriptor,
|
||||
SyntaxNode node,
|
||||
params object[] args)
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace ComputeSharp.SourceGeneration.Helpers;
|
||||
/// A helper type to build sequences of values with pooled buffers.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to create sequences for.</typeparam>
|
||||
internal ref struct ImmutableArrayBuilder<T>
|
||||
internal struct ImmutableArrayBuilder<T> : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The shared <see cref="ObjectPool{T}"/> instance to share <see cref="Writer"/> objects.
|
||||
@@ -49,6 +49,24 @@ internal ref struct ImmutableArrayBuilder<T>
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data written to the underlying buffer so far, as a <see cref="ReadOnlySpan{T}"/>.
|
||||
/// </summary>
|
||||
public readonly ReadOnlySpan<T> WrittenSpan
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.writer!.WrittenSpan;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of elements currently written in the current instance.
|
||||
/// </summary>
|
||||
public readonly int Count
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => this.writer!.WrittenSpan.Length;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ImmutableArray{T}.Builder.Add(T)"/>
|
||||
public readonly void Add(T item)
|
||||
{
|
||||
@@ -64,6 +82,16 @@ internal ref struct ImmutableArrayBuilder<T>
|
||||
this.writer!.AddRange(items);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts an item to the builder at the specified index.
|
||||
/// </summary>
|
||||
/// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
|
||||
/// <param name="item">The object to insert into the <see cref="IList{T}"/>.</param>
|
||||
public readonly void Insert(int index, T item)
|
||||
{
|
||||
this.writer!.Insert(index, item);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToImmutable"/>
|
||||
public readonly ImmutableArray<T> ToImmutable()
|
||||
{
|
||||
@@ -84,7 +112,7 @@ internal ref struct ImmutableArrayBuilder<T>
|
||||
return this.writer!.WrittenSpan.ToString();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IDisposable.Dispose"/>
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
Writer? writer = this.writer;
|
||||
@@ -131,9 +159,7 @@ internal ref struct ImmutableArrayBuilder<T>
|
||||
this.index = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data written to the underlying buffer so far, as a <see cref="ReadOnlySpan{T}"/>.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="ImmutableArrayBuilder{T}.WrittenSpan"/>
|
||||
public ReadOnlySpan<T> WrittenSpan
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -158,6 +184,25 @@ internal ref struct ImmutableArrayBuilder<T>
|
||||
this.index += items.Length;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ImmutableArrayBuilder{T}.Insert"/>
|
||||
public void Insert(int index, T item)
|
||||
{
|
||||
if (index < 0 || index > this.index)
|
||||
{
|
||||
ImmutableArrayBuilder.ThrowArgumentOutOfRangeExceptionForIndex();
|
||||
}
|
||||
|
||||
EnsureCapacity(1);
|
||||
|
||||
if (index < this.index)
|
||||
{
|
||||
Array.Copy(this.array, index, this.array, index + 1, this.index - index);
|
||||
}
|
||||
|
||||
this.array[index] = item;
|
||||
this.index++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the items in the current writer.
|
||||
/// </summary>
|
||||
@@ -201,4 +246,18 @@ internal ref struct ImmutableArrayBuilder<T>
|
||||
this.array = newArray;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Private helpers for the <see cref="ImmutableArrayBuilder{T}"/> type.
|
||||
/// </summary>
|
||||
file static class ImmutableArrayBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws an <see cref="ArgumentOutOfRangeException"/> for <c>"index"</c>.
|
||||
/// </summary>
|
||||
public static void ThrowArgumentOutOfRangeExceptionForIndex()
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ internal sealed partial record HierarchyInfo(string FilenameHint, string FullyQu
|
||||
/// <returns>A <see cref="HierarchyInfo"/> instance describing <paramref name="typeSymbol"/>.</returns>
|
||||
public static HierarchyInfo From(INamedTypeSymbol typeSymbol)
|
||||
{
|
||||
ImmutableArray<TypeInfo>.Builder hierarchy = ImmutableArray.CreateBuilder<TypeInfo>();
|
||||
using ImmutableArrayBuilder<TypeInfo> hierarchy = ImmutableArrayBuilder<TypeInfo>.Rent();
|
||||
|
||||
for (INamedTypeSymbol? parent = typeSymbol;
|
||||
parent is not null;
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする