Skip to content

*** WIP: Create PooledSpanBuilder, strongly based on PooledArrayBuilder #12095

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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 @@ -62,15 +62,15 @@ public void CreateAndCompareCollections(int size)
[InlineData(64)]
public void EnumeratorReturnsAllItemsInCollection(int size)
{
var pairs = new List<KeyValuePair<string, string?>>();
var pairs = new KeyValuePair<string, string?>[size];
var map = new Dictionary<string, bool>();

for (var i = 0; i < size; i++)
{
var key = i.ToString(CultureInfo.InvariantCulture);
var value = (int.MaxValue - i).ToString(CultureInfo.InvariantCulture);

pairs.Add(new(key, value));
pairs[i] = new(key, value);
map.Add(key, false);
}

Expand Down Expand Up @@ -196,8 +196,8 @@ static void Swap(ref KeyValuePair<string, string?> pair1, ref KeyValuePair<strin
[MemberData(nameof(TwoPairs))]
public void TestEquality_TwoItems(ImmutableArray<KeyValuePair<string, string?>> pairs1, ImmutableArray<KeyValuePair<string, string?>> pairs2)
{
var collection1 = MetadataCollection.Create(pairs1);
var collection2 = MetadataCollection.Create(pairs2);
var collection1 = MetadataCollection.Create(pairs1.AsSpan());
var collection2 = MetadataCollection.Create(pairs2.AsSpan());

Assert.Equal(collection1, collection2);
Assert.Equal(collection1.GetHashCode(), collection2.GetHashCode());
Expand All @@ -207,8 +207,8 @@ public void TestEquality_TwoItems(ImmutableArray<KeyValuePair<string, string?>>
[MemberData(nameof(ThreePairs))]
public void TestEquality_ThreeItems(ImmutableArray<KeyValuePair<string, string?>> pairs1, ImmutableArray<KeyValuePair<string, string?>> pairs2)
{
var collection1 = MetadataCollection.Create(pairs1);
var collection2 = MetadataCollection.Create(pairs2);
var collection1 = MetadataCollection.Create(pairs1.AsSpan());
var collection2 = MetadataCollection.Create(pairs2.AsSpan());

Assert.Equal(collection1, collection2);
Assert.Equal(collection1.GetHashCode(), collection2.GetHashCode());
Expand All @@ -218,8 +218,8 @@ public void TestEquality_ThreeItems(ImmutableArray<KeyValuePair<string, string?>
[MemberData(nameof(FourPairs))]
public void TestEquality_FourItems(ImmutableArray<KeyValuePair<string, string?>> pairs1, ImmutableArray<KeyValuePair<string, string?>> pairs2)
{
var collection1 = MetadataCollection.Create(pairs1);
var collection2 = MetadataCollection.Create(pairs2);
var collection1 = MetadataCollection.Create(pairs1.AsSpan());
var collection2 = MetadataCollection.Create(pairs2.AsSpan());

Assert.Equal(collection1, collection2);
Assert.Equal(collection1.GetHashCode(), collection2.GetHashCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ private static void CreateTypeParameterProperty(TagHelperDescriptorBuilder build
pb.Name = typeParameter.Name;
pb.TypeName = typeof(Type).FullName;

using var _ = ListPool<KeyValuePair<string, string?>>.GetPooledObject(out var metadataPairs);
using var metadataPairs = PooledSpanBuilder<KeyValuePair<string, string?>>.Empty;
metadataPairs.Add(PropertyName(typeParameter.Name));
metadataPairs.Add(MakeTrue(ComponentMetadata.Component.TypeParameterKey));
metadataPairs.Add(new(ComponentMetadata.Component.TypeParameterIsCascadingKey, cascade.ToString()));
Expand Down Expand Up @@ -504,7 +504,7 @@ private static void CreateTypeParameterProperty(TagHelperDescriptorBuilder build
metadataPairs.Add(new(ComponentMetadata.Component.TypeParameterWithAttributesKey, withAttributes.ToString()));
}

pb.SetMetadata(MetadataCollection.Create(metadataPairs));
pb.SetMetadata(MetadataCollection.Create(metadataPairs.AsSpan()));

pb.SetDocumentation(
DocumentationDescriptor.From(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.Extensions.ObjectPool;
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.Utilities;

namespace Microsoft.AspNetCore.Razor.Language;

[NonCopyable]
internal ref struct MetadataBuilder
{
private readonly ObjectPool<List<KeyValuePair<string, string?>>> _pool;
private List<KeyValuePair<string, string?>>? _list;
private PooledSpanBuilder<KeyValuePair<string, string?>> _list;

public MetadataBuilder()
: this(ListPool<KeyValuePair<string, string?>>.Default)
{
}

public MetadataBuilder(ObjectPool<List<KeyValuePair<string, string?>>> pool)
{
_pool = pool;
_list = PooledSpanBuilder<KeyValuePair<string, string?>>.Empty;
}

public void Dispose()
Expand All @@ -28,32 +23,23 @@ public void Dispose()
}

public void Add(string key, string? value)
{
_list ??= _pool.Get();
_list.Add(new(key, value));
}
=> _list.Add(new(key, value));

public void Add(KeyValuePair<string, string?> pair)
{
_list ??= _pool.Get();
_list.Add(pair);
}
=> Add(pair.Key, pair.Value);

public MetadataCollection Build()
{
var result = MetadataCollection.CreateOrEmpty(_list);
var result = MetadataCollection.CreateOrEmpty(_list.AsSpan());

_list = null;
ClearAndFree();

return result;
}

public void ClearAndFree()
{
if (_list is { } list)
{
_pool.Return(list);
_list = null;
}
_list.Dispose();
_list = PooledSpanBuilder<KeyValuePair<string, string?>>.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -87,17 +88,9 @@ public static MetadataCollection Create(KeyValuePair<string, string?> pair1, Key
=> new OneToThreeItems(pair1.Key, pair1.Value, pair2.Key, pair2.Value, pair3.Key, pair3.Value);

public static MetadataCollection Create(params KeyValuePair<string, string?>[] pairs)
=> pairs switch
{
[] => Empty,
[var pair] => new OneToThreeItems(pair.Key, pair.Value),
[var pair1, var pair2] => new OneToThreeItems(pair1.Key, pair1.Value, pair2.Key, pair2.Value),
[var pair1, var pair2, var pair3] => new OneToThreeItems(pair1.Key, pair1.Value, pair2.Key, pair2.Value, pair3.Key, pair3.Value),
_ => new FourOrMoreItems(pairs),
};
=> Create(pairs.AsSpan());

public static MetadataCollection Create<T>(T pairs)
where T : IReadOnlyList<KeyValuePair<string, string?>>
public static MetadataCollection Create(ReadOnlySpan<KeyValuePair<string, string?>> pairs)
=> pairs switch
{
[] => Empty,
Expand Down Expand Up @@ -160,18 +153,17 @@ public static MetadataCollection Create(Dictionary<string, string?> map)
return new OneToThreeItems(pair1.Key, pair1.Value, pair2.Key, pair2.Value, pair3.Key, pair3.Value);
}

// Finally, if there are four or more items, add the pairs to a list in order to construct
// Finally, if there are four or more items, add the pairs to an array in order to construct
// a FourOrMoreItems instance. Note that the constructor will copy the key-value pairs and won't
// hold onto the list we're passing, so it's safe to use a pooled list.
using var _ = ListPool<KeyValuePair<string, string?>>.GetPooledObject(out var list);
list.SetCapacityIfLarger(count);
// hold onto the list we're passing, so it's safe to use a pooled array.
using var array = new PooledSpanBuilder<KeyValuePair<string, string?>>(count);

foreach (var pair in map)
{
list.Add(pair);
array.Add(pair);
}

return Create(list);
return Create(array.AsSpan());
}

public static MetadataCollection Create(IReadOnlyDictionary<string, string?> map)
Expand All @@ -188,8 +180,7 @@ public static MetadataCollection Create(IReadOnlyDictionary<string, string?> map
return Create(map.ToArray());
}

public static MetadataCollection CreateOrEmpty<T>(T? pairs)
where T : IReadOnlyList<KeyValuePair<string, string?>>
public static MetadataCollection CreateOrEmpty(ReadOnlySpan<KeyValuePair<string, string?>> pairs)
=> pairs is { } realPairs ? Create(realPairs) : Empty;

public static MetadataCollection CreateOrEmpty(Dictionary<string, string?>? map)
Expand Down Expand Up @@ -533,14 +524,9 @@ private sealed class FourOrMoreItems : MetadataCollection

private readonly int _count;

public FourOrMoreItems(IReadOnlyList<KeyValuePair<string, string?>> pairs)
public FourOrMoreItems(ReadOnlySpan<KeyValuePair<string, string?>> pairs)
{
if (pairs is null)
{
throw new ArgumentNullException(nameof(pairs));
}

var count = pairs.Count;
var count = pairs.Length;

// Create a sorted array of keys.
var keys = new string[count];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;
using Microsoft.AspNetCore.Razor.PooledObjects;

namespace Microsoft.AspNetCore.Razor.Utilities.Shared.Test.PooledObjects;

public class PooledSpanBuilderTests
{
[Fact]
public void Create_FromSpan_CreatesWithElements()
{
var source = new[] { 1, 2, 3, 4 };
using var builder = PooledSpanBuilder.Create(source);

Assert.Equal(source.Length, builder.Count);
Assert.True(builder.Any());
Assert.Equal(source, builder.AsSpan().ToArray());
}

[Fact]
public void Add_AddsElements()
{
using var builder = PooledSpanBuilder<int>.Empty;
builder.Add(10);
builder.Add(20);

Assert.Equal(2, builder.Count);
Assert.Equal([10, 20], builder.AsSpan().ToArray());
}

[Fact]
public void AddRange_AddsSpan()
{
using var builder = PooledSpanBuilder<int>.Empty;
builder.AddRange(new[] { 1, 2, 3 });

Assert.Equal([1, 2, 3], builder.AsSpan().ToArray());
}

[Fact]
public void Insert_InsertsAtIndex()
{
using var builder = PooledSpanBuilder<int>.Empty;
builder.AddRange(new[] { 1, 3 });
builder.Insert(1, 2);

Assert.Equal([1, 2, 3], builder.AsSpan().ToArray());
}

[Fact]
public void RemoveAt_RemovesElement()
{
using var builder = PooledSpanBuilder<int>.Empty;
builder.AddRange(new[] { 1, 2, 3 });
builder.RemoveAt(1);

Assert.Equal([1, 3], builder.AsSpan().ToArray());
}

[Fact]
public void Clear_ResetsCount()
{
using var builder = PooledSpanBuilder<int>.Empty;
builder.AddRange(new[] { 1, 2, 3 });
builder.Clear();

Assert.Equal(0, builder.Count);
Assert.Empty(builder.AsSpan().ToArray());
}

[Fact]
public void ToImmutableAndClear_ReturnsImmutableAndClears()
{
using var builder = PooledSpanBuilder<int>.Empty;
builder.AddRange(new[] { 1, 2, 3 });
var immutable = builder.ToImmutableAndClear();

Assert.Equal(new[] { 1, 2, 3 }, immutable);
Assert.Equal(0, builder.Count);
}

[Fact]
public void PeekAndPop_WorkAsStack()
{
using var builder = PooledSpanBuilder<int>.Empty;
builder.Push(1);
builder.Push(2);

Assert.Equal(2, builder.Peek());
Assert.Equal(2, builder.Pop());
Assert.Equal(1, builder.Peek());
}

[Fact]
public void TryPop_Empty_ReturnsFalse()
{
using var builder = PooledSpanBuilder<int>.Empty;
var result = builder.TryPop(out var value);

Assert.False(result);
Assert.Equal(0, value);
}

[Fact]
public void Any_All_First_Last_Single()
{
using var builder = PooledSpanBuilder<int>.Empty;
builder.AddRange(new[] { 1, 2, 3 });

Assert.True(builder.Any());
Assert.True(builder.Any(x => x == 2));
Assert.True(builder.All(x => x > 0));
Assert.Equal(1, builder.First());
Assert.Equal(3, builder.Last());
Assert.Equal(2, builder.Single(x => x == 2));
}

[Fact]
public void ToArrayAndClear_ReturnsArrayAndClears()
{
using var builder = PooledSpanBuilder<int>.Empty;
builder.AddRange(new[] { 5, 6, 7 });
var arr = builder.ToArrayAndClear();

Assert.Equal([5, 6, 7], arr);
Assert.Equal(0, builder.Count);
}

[Fact]
public void Dispose_ReturnsArrayToPool()
{
var builder = new PooledSpanBuilder<int>(4);
builder.AddRange(new[] { 1, 2, 3, 4 });
builder.Dispose();

Assert.Equal(0, builder.Count);
Assert.Equal(0, builder.Capacity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.AspNetCore.Razor.PooledObjects;

internal static class PooledSpanBuilder
{
public static PooledSpanBuilder<T> Create<T>(ReadOnlySpan<T> source)
{
var spanBuilder = new PooledSpanBuilder<T>(source.Length);
spanBuilder.AddRange(source);
return spanBuilder;
}
}
Loading