Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions SpellingExclusions.dic
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ microsoft
vsls
Blazor
Metacode
Poolable
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static TagHelperDescriptor CreateNameMatchingDescriptor(
{
metadata.IsGeneric = true;

using var cascadeGenericTypeAttributes = new PooledHashSet<string>(StringHashSetPool.Ordinal);
using var cascadeGenericTypeAttributes = new PooledHashSet<string>(StringComparer.Ordinal);

foreach (var attribute in type.GetAttributes())
{
Expand Down Expand Up @@ -610,7 +610,7 @@ private static void CreateContextParameter(TagHelperDescriptorBuilder builder, s
// - are not indexers
private static ImmutableArray<(IPropertySymbol property, PropertyKind kind)> GetProperties(INamedTypeSymbol type)
{
using var names = new PooledHashSet<string>(StringHashSetPool.Ordinal);
using var names = new PooledHashSet<string>(StringComparer.Ordinal);
using var results = new PooledArrayBuilder<(IPropertySymbol, PropertyKind)>();

var currentType = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private static bool IsPotentialDictionaryProperty(IPropertySymbol property)
private static void CollectAccessibleProperties(
INamedTypeSymbol typeSymbol, ref PooledArrayBuilder<IPropertySymbol> properties)
{
using var names = new PooledHashSet<string>(StringHashSetPool.Ordinal);
using var names = new PooledHashSet<string>(StringComparer.Ordinal);

// Traverse the type hierarchy to find all accessible properties.
var currentType = typeSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Microsoft.AspNetCore.Razor.Language;

public partial class AllowedChildTagDescriptorBuilder
{
internal static readonly ObjectPool<AllowedChildTagDescriptorBuilder> Pool = DefaultPool.Create(Policy.Instance);
internal static readonly ObjectPool<AllowedChildTagDescriptorBuilder> Pool =
DefaultPool.Create(static () => new AllowedChildTagDescriptorBuilder());

internal static AllowedChildTagDescriptorBuilder GetInstance(TagHelperDescriptorBuilder parent)
{
Expand All @@ -26,15 +27,4 @@ private protected override void Reset()
Name = null;
DisplayName = null;
}

private sealed class Policy : PooledBuilderPolicy<AllowedChildTagDescriptorBuilder>
{
public static readonly Policy Instance = new();

private Policy()
{
}

public override AllowedChildTagDescriptorBuilder Create() => new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Microsoft.AspNetCore.Razor.Language;

public partial class BoundAttributeDescriptorBuilder
{
internal static readonly ObjectPool<BoundAttributeDescriptorBuilder> Pool = DefaultPool.Create(Policy.Instance);
internal static readonly ObjectPool<BoundAttributeDescriptorBuilder> Pool =
DefaultPool.Create(static () => new BoundAttributeDescriptorBuilder());

internal static BoundAttributeDescriptorBuilder GetInstance(TagHelperDescriptorBuilder parent)
{
Expand Down Expand Up @@ -36,15 +37,4 @@ private protected override void Reset()
ContainingType = null;
Parameters.Clear();
}

private sealed class Policy : PooledBuilderPolicy<BoundAttributeDescriptorBuilder>
{
public static readonly Policy Instance = new();

private Policy()
{
}

public override BoundAttributeDescriptorBuilder Create() => new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Microsoft.AspNetCore.Razor.Language;

public partial class BoundAttributeParameterDescriptorBuilder
{
internal static readonly ObjectPool<BoundAttributeParameterDescriptorBuilder> Pool = DefaultPool.Create(Policy.Instance);
internal static readonly ObjectPool<BoundAttributeParameterDescriptorBuilder> Pool =
DefaultPool.Create(static () => new BoundAttributeParameterDescriptorBuilder());

internal static BoundAttributeParameterDescriptorBuilder GetInstance(BoundAttributeDescriptorBuilder parent)
{
Expand All @@ -29,15 +30,4 @@ private protected override void Reset()
Name = null;
PropertyName = null;
}

private sealed class Policy : PooledBuilderPolicy<BoundAttributeParameterDescriptorBuilder>
{
public static readonly Policy Instance = new();

private Policy()
{
}

public override BoundAttributeParameterDescriptorBuilder Create() => new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private static void ProcessDuplicates(
ref PooledArrayBuilder<IntermediateNodeReference<TagHelperDirectiveAttributeIntermediateNode>> references,
ref PooledArrayBuilder<IntermediateNodeReference<TagHelperDirectiveAttributeParameterIntermediateNode>> parameterReferences)
{
using var _ = ReferenceEqualityHashSetPool<IntermediateNode>.GetPooledObject(out var parents);
using var _ = SpecializedPools.GetPooledReferenceEqualityHashSet<IntermediateNode>(out var parents);

foreach (var reference in references)
{
Expand Down Expand Up @@ -307,7 +307,7 @@ private static void ProcessDuplicateAttributes(IntermediateNode node)
// If we still have duplicates at this point then they are genuine conflicts.
// Use a hash set to quickly determine whether there are any duplicates.
// If so, we need to do a more expensive pass to identify and remove them.
using var _ = StringHashSetPool.Ordinal.GetPooledObject(out var duplicates);
using var _ = SpecializedPools.GetPooledStringHashSet(out var duplicates);

foreach (var child in children)
{
Expand All @@ -329,7 +329,7 @@ static void ReportDiagnosticAndRemoveDuplicates(IntermediateNode node)
{
var children = node.Children;

using var _ = StringDictionaryPool<ImmutableArray<AttributeInfo>.Builder>.Ordinal.GetPooledObject(out var duplicates);
using var _ = SpecializedPools.GetPooledStringDictionary<ImmutableArray<AttributeInfo>.Builder>(out var duplicates);

for (var i = 0; i < children.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override void ExecuteCore(
// For each event handler *usage* we need to rewrite the tag helper node to map to basic constructs.
// Each usage will be represented by a tag helper property that is a descendant of either
// a component or element.
using var _ = ReferenceEqualityHashSetPool<IntermediateNode>.GetPooledObject(out var parents);
using var _ = SpecializedPools.GetPooledReferenceEqualityHashSet<IntermediateNode>(out var parents);
var references = documentNode.FindDescendantReferences<TagHelperDirectiveAttributeIntermediateNode>();

foreach (var reference in references)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private static IReadOnlyList<UsingReference> ImportDirectives(

private static void PostProcessImportedDirectives(DocumentIntermediateNode document)
{
using var _ = ReferenceEqualityHashSetPool<DirectiveDescriptor>.GetPooledObject(out var seenDirectives);
using var _ = SpecializedPools.GetPooledReferenceEqualityHashSet<DirectiveDescriptor>(out var seenDirectives);
var references = document.FindDescendantReferences<DirectiveIntermediateNode>();

for (var i = references.Length - 1; i >= 0; i--)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.Extensions.ObjectPool;

namespace Microsoft.AspNetCore.Razor.Language;

Expand Down Expand Up @@ -73,7 +72,7 @@ internal static ReadOnlyMemory<char> GetMemoryWithoutGlobalPrefix(string s)
return mem;
}

internal abstract class DirectiveVisitor : SyntaxWalker
internal abstract class DirectiveVisitor : SyntaxWalker, IPoolableObject
{
private bool _isInitialized;
private string? _filePath;
Expand Down Expand Up @@ -173,7 +172,7 @@ internal sealed class TagHelperDirectiveVisitor : DirectiveVisitor
/// A larger pool of <see cref="TagHelperDescriptor"/> lists to handle scenarios where tag helpers
/// originate from a large number of assemblies.
/// </summary>
private static readonly ObjectPool<List<TagHelperDescriptor>> s_pool = ListPool<TagHelperDescriptor>.Create(100);
private static readonly ListPool<TagHelperDescriptor> s_pool = ListPool<TagHelperDescriptor>.Create(poolSize: 100);

/// <summary>
/// A map from assembly name to list of <see cref="TagHelperDescriptor"/>. Lists are allocated from and returned to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,8 @@ namespace Microsoft.AspNetCore.Razor.Language;

internal partial class DefaultRazorTagHelperContextDiscoveryPhase
{
private static readonly ObjectPool<TagHelperDirectiveVisitor> s_tagHelperDirectiveVisitorPool = DefaultPool.Create(DirectiveVisitorPolicy<TagHelperDirectiveVisitor>.Instance);
private static readonly ObjectPool<ComponentDirectiveVisitor> s_componentDirectiveVisitorPool = DefaultPool.Create(DirectiveVisitorPolicy<ComponentDirectiveVisitor>.Instance);

private sealed class DirectiveVisitorPolicy<T> : IPooledObjectPolicy<T>
where T : DirectiveVisitor, new()
{
public static readonly DirectiveVisitorPolicy<T> Instance = new();

private DirectiveVisitorPolicy()
{
}

public T Create() => new();

public bool Return(T visitor)
{
visitor.Reset();

return true;
}
}
private static readonly ObjectPool<TagHelperDirectiveVisitor> s_tagHelperDirectiveVisitorPool = DefaultPool.Create<TagHelperDirectiveVisitor>();
private static readonly ObjectPool<ComponentDirectiveVisitor> s_componentDirectiveVisitorPool = DefaultPool.Create<ComponentDirectiveVisitor>();

internal readonly ref struct PooledDirectiveVisitor(DirectiveVisitor visitor, bool isComponentDirectiveVisitor)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

namespace Microsoft.AspNetCore.Razor.Language.Legacy;

internal sealed class ClassifiedSpanVisitor : SyntaxWalker
internal sealed class ClassifiedSpanVisitor : SyntaxWalker, IPoolableObject
{
private static readonly ObjectPool<ClassifiedSpanVisitor> Pool = DefaultPool.Create(Policy.Instance, size: 5);
// Significantly larger than DefaultPool.MaximumObjectSize as there shouldn't be much concurrency
// of these arrays (we limit the number of pooled items to 5) and they are commonly large
public const int MaximumObjectSize = DefaultPool.DefaultMaximumObjectSize * 32;

private static readonly ObjectPool<ClassifiedSpanVisitor> Pool = DefaultPool.Create(static () => new ClassifiedSpanVisitor(), poolSize: 5);

private readonly ImmutableArray<ClassifiedSpanInternal>.Builder _spans;

Expand Down Expand Up @@ -433,11 +437,11 @@ private void AddSpan(SyntaxToken token, SpanKindInternal kind, AcceptedCharacter
private void AddSpan(SourceSpan span, SpanKindInternal kind, AcceptedCharactersInternal acceptedCharacters)
=> _spans.Add(new(span, CurrentBlockSpan, kind, _currentBlockKind, acceptedCharacters));

private void Reset()
void IPoolableObject.Reset()
{
_spans.Clear();

if (_spans.Capacity > Policy.MaximumObjectSize)
if (_spans.Capacity > MaximumObjectSize)
{
// Differs from ArrayBuilderPool.Policy's behavior as we allow our array to grow significantly larger
_spans.Capacity = 0;
Expand All @@ -448,26 +452,4 @@ private void Reset()
_currentBlockSpan = null;
_currentBlockKind = BlockKindInternal.Markup;
}

private sealed class Policy : IPooledObjectPolicy<ClassifiedSpanVisitor>
{
public static readonly Policy Instance = new();

// Significantly larger than DefaultPool.MaximumObjectSize as there shouldn't be much concurrency
// of these arrays (we limit the number of pooled items to 5) and they are commonly large
public const int MaximumObjectSize = DefaultPool.MaximumObjectSize * 32;

private Policy()
{
}

public ClassifiedSpanVisitor Create() => new();

public bool Return(ClassifiedSpanVisitor visitor)
{
visitor.Reset();

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Microsoft.AspNetCore.Razor.PooledObjects;

Expand Down Expand Up @@ -38,7 +37,7 @@ public ParserContext(RazorSourceDocument source, RazorParserOptions options, Can
_errorSinkStack = StackPool<ErrorSink>.Default.Get();
_errorSinkStack.Push(new ErrorSink());

_seenDirectivesSet = StringHashSetPool.Ordinal.Get();
_seenDirectivesSet = SpecializedPools.StringHashSet.Ordinal.Get();
Comment thread
ToddGrun marked this conversation as resolved.

Source = new SeekableTextReader(SourceDocument);
}
Expand All @@ -52,7 +51,7 @@ public void Dispose()
}

StackPool<ErrorSink>.Default.Return(_errorSinkStack);
StringHashSetPool.Ordinal.Return(_seenDirectivesSet);
SpecializedPools.StringHashSet.Ordinal.Return(_seenDirectivesSet);
}

public ErrorSink ErrorSink => _errorSinkStack.Peek();
Expand All @@ -74,7 +73,7 @@ public ErrorScope PushNewErrorScope(ErrorSink errorSink)
// Debug Helpers

#if DEBUG
[DebuggerDisplay("{" + nameof(DebuggerToString) + "(),nq}")]
[System.Diagnostics.DebuggerDisplay("{" + nameof(DebuggerToString) + "(),nq}")]
internal partial class ParserContext
{
private string Unparsed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.CodeAnalysis;

namespace Microsoft.AspNetCore.Razor.Language;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.AspNetCore.Razor.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.Internal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Microsoft.AspNetCore.Razor.Language;

public partial class RequiredAttributeDescriptorBuilder
{
internal static readonly ObjectPool<RequiredAttributeDescriptorBuilder> Pool = DefaultPool.Create(Policy.Instance);
internal static readonly ObjectPool<RequiredAttributeDescriptorBuilder> Pool =
DefaultPool.Create(static () => new RequiredAttributeDescriptorBuilder());

internal static RequiredAttributeDescriptorBuilder GetInstance(TagMatchingRuleDescriptorBuilder parent)
{
Expand All @@ -29,15 +30,4 @@ private protected override void Reset()
Value = null;
ValueComparison = default;
}

private sealed class Policy : PooledBuilderPolicy<RequiredAttributeDescriptorBuilder>
{
public static readonly Policy Instance = new();

private Policy()
{
}

public override RequiredAttributeDescriptorBuilder Create() => new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void ProcessDescriptors(
using var catchAllToAdd = new MemoryBuilder<TagHelperDescriptor>(initialCapacity: descriptors.Length, clearArray: true);

// The builders are indexed using a map of "tag name" to the index of the builder in the array.
using var _1 = StringDictionaryPool<int>.OrdinalIgnoreCase.GetPooledObject(out var tagNameToBuilderIndexMap);
using var _1 = SpecializedPools.GetPooledStringDictionary<int>(ignoreCase: true, out var tagNameToBuilderIndexMap);
using var _2 = HashSetPool<TagHelperDescriptor>.GetPooledObject(out var tagHelperSet);

#if NET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace Microsoft.AspNetCore.Razor.Language;

public partial class TagHelperDescriptorBuilder
{
private static readonly ObjectPool<TagHelperDescriptorBuilder> s_pool = DefaultPool.Create(Policy.Instance);
private static readonly ObjectPool<TagHelperDescriptorBuilder> s_pool =
DefaultPool.Create(static () => new TagHelperDescriptorBuilder());

internal static TagHelperDescriptorBuilder GetInstance(string name, string assemblyName)
=> GetInstance(TagHelperKind.ITagHelper, name, assemblyName);
Expand Down Expand Up @@ -46,17 +47,6 @@ private protected override void Reset()
TagMatchingRules.Clear();
}

private sealed class Policy : PooledBuilderPolicy<TagHelperDescriptorBuilder>
{
public static readonly Policy Instance = new();

private Policy()
{
}

public override TagHelperDescriptorBuilder Create() => new();
}

/// <summary>
/// Retrieves a pooled <see cref="TagHelperDescriptorBuilder"/> instance.
/// </summary>
Expand Down
Loading