Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary temporary array allocation #719

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInf
.AddArgumentListArguments(
Argument(fieldExpression),
Argument(IdentifierName("value")))),
Block(setterStatements.ToArray()));
Block(setterStatements.AsEnumerable()));
Sergio0694 marked this conversation as resolved.
Show resolved Hide resolved

// Prepare the forwarded attributes, if any
ImmutableArray<AttributeListSyntax> forwardedAttributes =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -88,6 +90,18 @@ public readonly T[] ToArray()
return this.writer!.WrittenSpan.ToArray();
}

/// <summary>
/// Gets an <see cref="IEnumerable{T}"/> instance for the current builder.
/// </summary>
/// <returns>An <see cref="IEnumerable{T}"/> instance for the current builder.</returns>
/// <remarks>
/// The builder should not be mutated while an enumerator is in use.
/// </remarks>
public readonly IEnumerable<T> AsEnumerable()
{
return this.writer!;
}

/// <inheritdoc/>
public override readonly string ToString()
{
Expand All @@ -107,7 +121,7 @@ public void Dispose()
/// <summary>
/// A class handling the actual buffer writing.
/// </summary>
private sealed class Writer : IDisposable
private sealed class Writer : ICollection<T>, IDisposable
{
/// <summary>
/// The underlying <typeparamref name="T"/> array.
Expand Down Expand Up @@ -142,6 +156,9 @@ public ReadOnlySpan<T> WrittenSpan
get => new(this.array!, 0, this.index);
}

/// <inheritdoc/>
bool ICollection<T>.IsReadOnly => true;

/// <inheritdoc cref="ImmutableArrayBuilder{T}.Add"/>
public void Add(T value)
{
Expand Down Expand Up @@ -173,6 +190,48 @@ public void Dispose()
}
}

/// <inheritdoc/>
void ICollection<T>.Clear()
{
throw new NotSupportedException();
}

/// <inheritdoc/>
bool ICollection<T>.Contains(T item)
{
throw new NotSupportedException();
}

/// <inheritdoc/>
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
Array.Copy(this.array!, 0, array, arrayIndex, this.index);
}

/// <inheritdoc/>
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
T?[] array = this.array!;
int length = this.index;

for (int i = 0; i < length; i++)
{
yield return array[i]!;
Sergio0694 marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}

/// <inheritdoc/>
bool ICollection<T>.Remove(T item)
{
throw new NotSupportedException();
}

/// <summary>
/// Ensures that <see cref="array"/> has enough free space to contain a given number of new items.
/// </summary>
Expand Down