Skip to content
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 @@ -5,11 +5,12 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language.Components;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.PooledObjects;

namespace Microsoft.AspNetCore.Razor.Language;

Expand Down Expand Up @@ -216,14 +217,16 @@ private bool AssemblyContainsTagHelpers(string assemblyName, IReadOnlyList<TagHe

internal sealed class ComponentDirectiveVisitor : DirectiveVisitor
{
private readonly List<TagHelperDescriptor> _notFullyQualifiedComponents;
private readonly ImmutableArray<TagHelperDescriptor> _notFullyQualifiedComponents;
private readonly string _filePath;
private RazorSourceDocument _source;

public ComponentDirectiveVisitor(string filePath, IReadOnlyList<TagHelperDescriptor> tagHelpers, string currentNamespace)
{
_filePath = filePath;

using var builder = new PooledArrayBuilder<TagHelperDescriptor>(capacity: tagHelpers.Count);

for (var i = 0; i < tagHelpers.Count; i++)
{
var tagHelper = tagHelpers[i];
Expand All @@ -240,8 +243,7 @@ public ComponentDirectiveVisitor(string filePath, IReadOnlyList<TagHelperDescrip
continue;
}

_notFullyQualifiedComponents ??= new();
_notFullyQualifiedComponents.Add(tagHelper);
builder.Add(tagHelper);

if (currentNamespace is null)
{
Expand All @@ -263,6 +265,8 @@ public ComponentDirectiveVisitor(string filePath, IReadOnlyList<TagHelperDescrip
Matches.Add(tagHelper);
}
}

_notFullyQualifiedComponents = builder.DrainToImmutable();
}

public override HashSet<TagHelperDescriptor> Matches { get; } = new HashSet<TagHelperDescriptor>();
Expand Down Expand Up @@ -324,9 +328,8 @@ public override void VisitRazorDirective(RazorDirectiveSyntax node)
continue;
}

for (var i = 0; _notFullyQualifiedComponents is not null && i < _notFullyQualifiedComponents.Count; i++)
foreach (var tagHelper in _notFullyQualifiedComponents)
{
var tagHelper = _notFullyQualifiedComponents[i];
Debug.Assert(!tagHelper.IsComponentFullyQualifiedNameMatch(), "We've already processed these.");

if (tagHelper.IsChildContentTagHelper())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ namespace Microsoft.AspNetCore.Razor.PooledObjects;
internal ref struct PooledArrayBuilder<T>
{
private readonly ObjectPool<ImmutableArray<T>.Builder> _pool;
private readonly int? _capacity;
private ImmutableArray<T>.Builder? _builder;

public PooledArrayBuilder()
: this(ArrayBuilderPool<T>.Default)
: this(null, null)
{
}

public PooledArrayBuilder(ObjectPool<ImmutableArray<T>.Builder> pool)
public PooledArrayBuilder(ObjectPool<ImmutableArray<T>.Builder>? pool = null, int? capacity = null)
{
_pool = pool;
_pool = pool ?? ArrayBuilderPool<T>.Default;
_capacity = capacity;
}

public void Dispose()
Expand All @@ -38,7 +40,7 @@ public readonly int Count

public void Add(T item)
{
_builder ??= _pool.Get();
_builder ??= GetBuilder();
_builder.Add(item);
}

Expand All @@ -49,13 +51,13 @@ public void AddRange(IReadOnlyList<T> items)
return;
}

_builder ??= _pool.Get();
_builder ??= GetBuilder();
_builder.AddRange(items);
}

public void AddRange(IEnumerable<T> items)
{
_builder ??= _pool.Get();
_builder ??= GetBuilder();
_builder.AddRange(items);
}

Expand All @@ -68,6 +70,17 @@ public void ClearAndFree()
}
}

private readonly ImmutableArray<T>.Builder GetBuilder()
{
var result = _pool.Get();
if (_capacity is int capacity)
{
result.SetCapacityIfNeeded(capacity);
}

return result;
}

/// <summary>
/// Returns the current contents as an <see cref="ImmutableArray{T}"/> and sets
/// the collection to a zero length array.
Expand Down