Skip to content

Commit

Permalink
Remove Lease type, move logic to declaring type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Oct 5, 2022
1 parent 1bef046 commit 66dee36
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers
// If requested, only include the event and the basic methods to raise it, but not the additional helpers
if (!info.IncludeAdditionalHelperMethods)
{
using ImmutableArrayBuilder<MemberDeclarationSyntax>.Lease selectedMembers = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();
using ImmutableArrayBuilder<MemberDeclarationSyntax> selectedMembers = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();

foreach (MemberDeclarationSyntax memberDeclaration in memberDeclarations)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public static AttributeInfo From(AttributeData attributeData)
{
string typeName = attributeData.AttributeClass!.GetFullyQualifiedName();

using ImmutableArrayBuilder<TypedConstantInfo>.Lease constructorArguments = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
using ImmutableArrayBuilder<(string, TypedConstantInfo)>.Lease namedArguments = ImmutableArrayBuilder<(string, TypedConstantInfo)>.Rent();
using ImmutableArrayBuilder<TypedConstantInfo> constructorArguments = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
using ImmutableArrayBuilder<(string, TypedConstantInfo)> namedArguments = ImmutableArrayBuilder<(string, TypedConstantInfo)>.Rent();

// Get the constructor arguments
foreach (TypedConstant typedConstant in attributeData.ConstructorArguments)
Expand Down Expand Up @@ -65,8 +65,8 @@ public static AttributeInfo From(INamedTypeSymbol typeSymbol, SemanticModel sema
{
string typeName = typeSymbol.GetFullyQualifiedName();

using ImmutableArrayBuilder<TypedConstantInfo>.Lease constructorArguments = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
using ImmutableArrayBuilder<(string, TypedConstantInfo)>.Lease namedArguments = ImmutableArrayBuilder<(string, TypedConstantInfo)>.Rent();
using ImmutableArrayBuilder<TypedConstantInfo> constructorArguments = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
using ImmutableArrayBuilder<(string, TypedConstantInfo)> namedArguments = ImmutableArrayBuilder<(string, TypedConstantInfo)>.Rent();

foreach (AttributeArgumentSyntax argument in arguments)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static TypedConstantInfo From(
return new Array(elementTypeName, ImmutableArray<TypedConstantInfo>.Empty);
}

using ImmutableArrayBuilder<TypedConstantInfo>.Lease items = ImmutableArrayBuilder<TypedConstantInfo>.Rent();
using ImmutableArrayBuilder<TypedConstantInfo> items = ImmutableArrayBuilder<TypedConstantInfo>.Rent();

// Enumerate all array elements and extract serialized info for them
foreach (ExpressionSyntax initializationExpression in initializerExpression.Expressions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static bool TryGetInfo(
[NotNullWhen(true)] out PropertyInfo? propertyInfo,
out ImmutableArray<DiagnosticInfo> diagnostics)
{
using ImmutableArrayBuilder<DiagnosticInfo>.Lease builder = ImmutableArrayBuilder<DiagnosticInfo>.Rent();
using ImmutableArrayBuilder<DiagnosticInfo> builder = ImmutableArrayBuilder<DiagnosticInfo>.Rent();

// Validate the target type
if (!IsTargetTypeValid(fieldSymbol, out bool shouldInvokeOnPropertyChanging))
Expand Down Expand Up @@ -101,10 +101,10 @@ public static bool TryGetInfo(
return false;
}

using ImmutableArrayBuilder<string>.Lease propertyChangedNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<string>.Lease propertyChangingNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<string>.Lease notifiedCommandNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<AttributeInfo>.Lease forwardedAttributes = ImmutableArrayBuilder<AttributeInfo>.Rent();
using ImmutableArrayBuilder<string> propertyChangedNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<string> propertyChangingNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<string> notifiedCommandNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<AttributeInfo> forwardedAttributes = ImmutableArrayBuilder<AttributeInfo>.Rent();

bool notifyRecipients = false;
bool notifyDataErrorInfo = false;
Expand Down Expand Up @@ -318,8 +318,8 @@ private static bool IsGeneratedPropertyInvalid(string propertyName, ITypeSymbol
private static bool TryGatherDependentPropertyChangedNames(
IFieldSymbol fieldSymbol,
AttributeData attributeData,
in ImmutableArrayBuilder<string>.Lease propertyChangedNames,
in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics)
in ImmutableArrayBuilder<string> propertyChangedNames,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics)
{
// Validates a property name using existing properties
bool IsPropertyNameValid(string propertyName)
Expand Down Expand Up @@ -383,8 +383,8 @@ bool IsPropertyNameValidWithGeneratedMembers(string propertyName)
private static bool TryGatherDependentCommandNames(
IFieldSymbol fieldSymbol,
AttributeData attributeData,
in ImmutableArrayBuilder<string>.Lease notifiedCommandNames,
in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics)
in ImmutableArrayBuilder<string> notifiedCommandNames,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics)
{
// Validates a command name using existing properties
bool IsCommandNameValid(string commandName, out bool shouldLookForGeneratedMembersToo)
Expand Down Expand Up @@ -505,7 +505,7 @@ private static bool TryGetIsNotifyingRecipients(IFieldSymbol fieldSymbol, out bo
private static bool TryGetIsNotifyingRecipients(
IFieldSymbol fieldSymbol,
AttributeData attributeData,
in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
bool hasOrInheritsClassLevelNotifyPropertyChangedRecipients,
out bool isBroadcastTargetValid)
{
Expand Down Expand Up @@ -608,7 +608,7 @@ private static bool TryGetNotifyDataErrorInfo(IFieldSymbol fieldSymbol, out bool
private static bool TryGetNotifyDataErrorInfo(
IFieldSymbol fieldSymbol,
AttributeData attributeData,
in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics,
in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
bool hasOrInheritsClassLevelNotifyDataErrorInfo,
out bool isValidationTargetValid)
{
Expand Down Expand Up @@ -701,7 +701,7 @@ private static bool TryGetNotifyDataErrorInfo(
/// <returns>The generated <see cref="MemberDeclarationSyntax"/> instance for <paramref name="propertyInfo"/>.</returns>
public static MemberDeclarationSyntax GetPropertySyntax(PropertyInfo propertyInfo)
{
using ImmutableArrayBuilder<StatementSyntax>.Lease setterStatements = ImmutableArrayBuilder<StatementSyntax>.Rent();
using ImmutableArrayBuilder<StatementSyntax> setterStatements = ImmutableArrayBuilder<StatementSyntax>.Rent();

// Get the property type syntax
TypeSyntax propertyType = IdentifierName(propertyInfo.TypeNameWithNullabilityAnnotations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ObservableRecipientGenerator()
/// <inheritdoc/>
protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers(ObservableRecipientInfo info, ImmutableArray<MemberDeclarationSyntax> memberDeclarations)
{
using ImmutableArrayBuilder<MemberDeclarationSyntax>.Lease builder = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();
using ImmutableArrayBuilder<MemberDeclarationSyntax> builder = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();

// If the target type has no constructors, generate constructors as well
if (!info.HasExplicitConstructors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static bool IsObservableValidator(INamedTypeSymbol typeSymbol)
/// <returns>The resulting <see cref="ValidationInfo"/> instance for <paramref name="typeSymbol"/>.</returns>
public static ValidationInfo GetInfo(INamedTypeSymbol typeSymbol)
{
using ImmutableArrayBuilder<string>.Lease propertyNames = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<string> propertyNames = ImmutableArrayBuilder<string>.Rent();

foreach (ISymbol memberSymbol in typeSymbol.GetMembers())
{
Expand Down Expand Up @@ -93,7 +93,7 @@ public static ValidationInfo GetInfo(INamedTypeSymbol typeSymbol)
/// <returns>A <see cref="RecipientInfo"/> instance for the current type being inspected.</returns>
public static RecipientInfo GetInfo(INamedTypeSymbol typeSymbol, ImmutableArray<INamedTypeSymbol> interfaceSymbols)
{
using ImmutableArrayBuilder<string>.Lease names = ImmutableArrayBuilder<string>.Rent();
using ImmutableArrayBuilder<string> names = ImmutableArrayBuilder<string>.Rent();

foreach (INamedTypeSymbol interfaceSymbol in interfaceSymbols)
{
Expand All @@ -113,7 +113,7 @@ public static RecipientInfo GetInfo(INamedTypeSymbol typeSymbol, ImmutableArray<
/// <returns>The head <see cref="CompilationUnitSyntax"/> instance with the type attributes.</returns>
public static CompilationUnitSyntax GetSyntax(bool isDynamicallyAccessedMembersAttributeAvailable)
{
using ImmutableArrayBuilder<AttributeListSyntax>.Lease attributes = ImmutableArrayBuilder<AttributeListSyntax>.Rent();
using ImmutableArrayBuilder<AttributeListSyntax> attributes = ImmutableArrayBuilder<AttributeListSyntax>.Rent();

// Prepare the base attributes with are always present:
//
Expand Down Expand Up @@ -261,7 +261,7 @@ public static CompilationUnitSyntax GetSyntax(ValidationInfo validationInfo)
/// <returns>The sequence of <see cref="StatementSyntax"/> instances to validate declared properties.</returns>
private static ImmutableArray<StatementSyntax> EnumerateValidationStatements(ValidationInfo validationInfo)
{
using ImmutableArrayBuilder<StatementSyntax>.Lease statements = ImmutableArrayBuilder<StatementSyntax>.Rent();
using ImmutableArrayBuilder<StatementSyntax> statements = ImmutableArrayBuilder<StatementSyntax>.Rent();

// This loop produces a sequence of statements as follows:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics,
this in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
DiagnosticDescriptor descriptor,
ISymbol symbol,
params object[] args)
Expand All @@ -41,7 +41,7 @@ public static void Add(
/// <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 in ImmutableArrayBuilder<DiagnosticInfo>.Lease diagnostics,
this in ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
DiagnosticDescriptor descriptor,
SyntaxNode node,
params object[] args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,85 @@
using System;
using System.Collections.Immutable;

#pragma warning disable CS0618

namespace CommunityToolkit.Mvvm.SourceGenerators.Helpers;

/// <summary>
/// A helper class to build <see cref="ImmutableArray{T}"/> instances with pooled buffers.
/// A helper type to build <see cref="ImmutableArray{T}"/> instances with pooled buffers.
/// </summary>
/// <typeparam name="T">The type of items to create arrays for.</typeparam>
internal static class ImmutableArrayBuilder<T>
internal ref struct ImmutableArrayBuilder<T>
{
/// <summary>
/// The shared <see cref="ObjectPool{T}"/> instance to share <see cref="ImmutableArray{T}.Builder"/> objects.
/// </summary>
private static readonly ObjectPool<ImmutableArray<T>.Builder> objectPool = new(ImmutableArray.CreateBuilder<T>);
private static readonly ObjectPool<ImmutableArray<T>.Builder> sharedObjectPool = new(ImmutableArray.CreateBuilder<T>);

/// <summary>
/// The owner <see cref="ObjectPool{T}"/> instance.
/// </summary>
private readonly ObjectPool<ImmutableArray<T>.Builder> objectPool;

/// <summary>
/// Rents a new pooled <see cref="ImmutableArray{T}.Builder"/> instance through a <see cref="Lease"/> value.
/// The rented <see cref="ImmutableArray{T}.Builder"/> instance to use.
/// </summary>
/// <returns>A <see cref="Lease"/> to interact with the underlying <see cref="ImmutableArray{T}.Builder"/> instance.</returns>
public static Lease Rent()
private ImmutableArray<T>.Builder? builder;

/// <summary>
/// Rents a new pooled <see cref="ImmutableArray{T}.Builder"/> instance through a new <see cref="ImmutableArrayBuilder{T}"/> value.
/// </summary>
/// <returns>A <see cref="ImmutableArrayBuilder{T}"/> to interact with the underlying <see cref="ImmutableArray{T}.Builder"/> instance.</returns>
public static ImmutableArrayBuilder<T> Rent()
{
return new(objectPool, objectPool.Allocate());
return new(sharedObjectPool, sharedObjectPool.Allocate());
}

/// <summary>
/// A wrapper for a pooled <see cref="ImmutableArray{T}.Builder"/> instance.
/// Creates a new <see cref="ImmutableArrayBuilder{T}"/> object with the specified parameters.
/// </summary>
public ref struct Lease
/// <param name="objectPool"></param>
/// <param name="builder"></param>
private ImmutableArrayBuilder(ObjectPool<ImmutableArray<T>.Builder> objectPool, ImmutableArray<T>.Builder builder)
{
/// <summary>
/// The owner <see cref="ObjectPool{T}"/> instance.
/// </summary>
private readonly ObjectPool<ImmutableArray<T>.Builder> objectPool;
this.objectPool = objectPool;
this.builder = builder;
}

/// <summary>
/// The rented <see cref="ImmutableArray{T}.Builder"/> instance to use.
/// </summary>
private ImmutableArray<T>.Builder? builder;
/// <inheritdoc cref="ImmutableArray{T}.Builder.Count"/>
public readonly int Count
{
get => this.builder!.Count;
}

/// <summary>
/// Creates a new <see cref="Lease"/> object with the specified parameters.
/// </summary>
/// <param name="objectPool"></param>
/// <param name="builder"></param>
[Obsolete("Don't create instances of this type manually, use ImmutableArrayBuilder<T>.Rent() instead.")]
public Lease(ObjectPool<ImmutableArray<T>.Builder> objectPool, ImmutableArray<T>.Builder builder)
{
this.objectPool = objectPool;
this.builder = builder;
}
/// <inheritdoc cref="ImmutableArray{T}.Builder.Add(T)"/>
public readonly void Add(T item)
{
this.builder!.Add(item);
}

/// <inheritdoc cref="ImmutableArray{T}.Builder.Count"/>
public readonly int Count
{
get => this.builder!.Count;
}
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToImmutable"/>
public readonly ImmutableArray<T> ToImmutable()
{
return this.builder!.ToImmutable();
}

/// <inheritdoc cref="ImmutableArray{T}.Builder.Add(T)"/>
public readonly void Add(T item)
{
this.builder!.Add(item);
}
/// <inheritdoc cref="ImmutableArray{T}.Builder.ToArray"/>
public readonly T[] ToArray()
{
return this.builder!.ToArray();
}

/// <inheritdoc cref="ImmutableArray{T}.Builder.ToImmutable"/>
public readonly ImmutableArray<T> ToImmutable()
{
return this.builder!.ToImmutable();
}
/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
{
ImmutableArray<T>.Builder? builder = this.builder;

/// <inheritdoc cref="ImmutableArray{T}.Builder.ToArray"/>
public readonly T[] ToArray()
{
return this.builder!.ToArray();
}
this.builder = null;

/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
if (builder is not null)
{
ImmutableArray<T>.Builder? builder = this.builder;

this.builder = null;

if (builder is not null)
{
builder.Clear();
builder.Clear();

this.objectPool.Free(builder);
}
this.objectPool.Free(builder);
}
}
}
Loading

0 comments on commit 66dee36

Please sign in to comment.