-
Notifications
You must be signed in to change notification settings - Fork 212
*** 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
Changes from all commits
247a2ef
928e139
008da56
6942324
95e730f
605b4d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use our |
||
|
||
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() | ||
|
@@ -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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use Alternatively, you could use a |
||
|
||
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) | ||
|
@@ -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) | ||
|
@@ -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]; | ||
|
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does a ref struct need to marked as
[NonCopyable]
? Isn't this mixing two different worlds?