Skip to content

Commit dd3344f

Browse files
When appropriate, use custom pool types rather than ObjectPool<T>
1 parent 525d170 commit dd3344f

File tree

10 files changed

+21
-29
lines changed

10 files changed

+21
-29
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultRazorTagHelperContextDiscoveryPhase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using Microsoft.AspNetCore.Razor.Language.Legacy;
1313
using Microsoft.AspNetCore.Razor.Language.Syntax;
1414
using Microsoft.AspNetCore.Razor.PooledObjects;
15-
using Microsoft.Extensions.ObjectPool;
1615

1716
namespace Microsoft.AspNetCore.Razor.Language;
1817

@@ -173,7 +172,7 @@ internal sealed class TagHelperDirectiveVisitor : DirectiveVisitor
173172
/// A larger pool of <see cref="TagHelperDescriptor"/> lists to handle scenarios where tag helpers
174173
/// originate from a large number of assemblies.
175174
/// </summary>
176-
private static readonly ObjectPool<List<TagHelperDescriptor>> s_pool = ListPool<TagHelperDescriptor>.Create(100);
175+
private static readonly ListPool<TagHelperDescriptor> s_pool = ListPool<TagHelperDescriptor>.Create(poolSize: 100);
177176

178177
/// <summary>
179178
/// A map from assembly name to list of <see cref="TagHelperDescriptor"/>. Lists are allocated from and returned to

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/RazorEditHelper.TextChangeBuilder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
using Microsoft.AspNetCore.Razor.PooledObjects;
1717
using Microsoft.CodeAnalysis.Razor.Protocol;
1818
using Microsoft.CodeAnalysis.Text;
19-
using Microsoft.Extensions.ObjectPool;
2019

2120
namespace Microsoft.CodeAnalysis.Razor.DocumentMapping;
2221

2322
internal static partial class RazorEditHelper
2423
{
2524
private sealed class TextChangeBuilder : IDisposable
2625
{
27-
private ObjectPool<ImmutableArray<RazorTextChange>.Builder> Pool => ArrayBuilderPool<RazorTextChange>.Default;
26+
private static ArrayBuilderPool<RazorTextChange> Pool => ArrayBuilderPool<RazorTextChange>.Default;
2827
private readonly ImmutableArray<RazorTextChange>.Builder _builder;
2928
private readonly IDocumentMappingService _documentMappingService;
3029

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectState.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
using Microsoft.CodeAnalysis.Razor.ProjectEngineHost;
1616
using Microsoft.CodeAnalysis.Razor.Utilities;
1717
using Microsoft.CodeAnalysis.Text;
18-
using Microsoft.Extensions.ObjectPool;
1918

2019
namespace Microsoft.CodeAnalysis.Razor.ProjectSystem;
2120

2221
internal sealed class ProjectState
2322
{
24-
private static readonly ObjectPool<Dictionary<string, ImmutableHashSet<string>.Builder>> s_importMapBuilderPool =
23+
private static readonly DictionaryPool<string, ImmutableHashSet<string>.Builder> s_importMapBuilderPool =
2524
DictionaryPool<string, ImmutableHashSet<string>.Builder>.Create(FilePathNormalizingComparer.Instance);
2625

2726
private static readonly ImmutableDictionary<string, DocumentState> s_emptyDocuments

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Serialization/MessagePack/Formatters/SerializerCachingOptions.ReferenceMap`1.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using Microsoft.AspNetCore.Razor.PooledObjects;
7-
using Microsoft.Extensions.ObjectPool;
87

98
namespace Microsoft.CodeAnalysis.Razor.Serialization.MessagePack.Formatters;
109

@@ -13,11 +12,11 @@ internal partial class SerializerCachingOptions
1312
public struct ReferenceMap<T> : IDisposable
1413
where T : notnull
1514
{
16-
private readonly ObjectPool<Dictionary<T, int>> _dictionaryPool;
15+
private readonly DictionaryPool<T, int> _dictionaryPool;
1716
private List<T> _values;
1817
private Dictionary<T, int> _valueToIdMap;
1918

20-
public ReferenceMap(ObjectPool<Dictionary<T, int>> dictionaryPool)
19+
public ReferenceMap(DictionaryPool<T, int> dictionaryPool)
2120
{
2221
_dictionaryPool = dictionaryPool;
2322
_values = ListPool<T>.Default.Get();

src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared.Test/PooledObjects/TestArrayBuilderPool`1.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33

44
using System.Collections.Immutable;
55
using Microsoft.AspNetCore.Razor.PooledObjects;
6-
using Microsoft.Extensions.ObjectPool;
76

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

109
internal static class TestArrayBuilderPool<T>
1110
{
12-
public static ObjectPool<ImmutableArray<T>.Builder> Create(
13-
IPooledObjectPolicy<ImmutableArray<T>.Builder>? policy = null, int size = 1)
14-
=> DefaultPool.Create(policy ?? NoReturnPolicy.Instance, size);
11+
public static ArrayBuilderPool<T> Create(
12+
ArrayBuilderPool<T>.PooledObjectPolicy? policy = null, int size = 1)
13+
=> ArrayBuilderPool<T>.Create(policy ?? NoReturnPolicy.Instance, size);
1514

16-
public sealed class NoReturnPolicy : IPooledObjectPolicy<ImmutableArray<T>.Builder>
15+
public sealed class NoReturnPolicy : ArrayBuilderPool<T>.PooledObjectPolicy
1716
{
1817
public static readonly NoReturnPolicy Instance = new();
1918

2019
private NoReturnPolicy()
2120
{
2221
}
2322

24-
public ImmutableArray<T>.Builder Create()
23+
public override ImmutableArray<T>.Builder Create()
2524
=> ImmutableArray.CreateBuilder<T>();
2625

27-
public bool Return(ImmutableArray<T>.Builder obj)
26+
public override bool Return(ImmutableArray<T>.Builder obj)
2827
=> false;
2928
}
3029
}

src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/PooledObjects/DictionaryBuilderPool`2.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ private DictionaryBuilderPool(PooledObjectPolicy policy, Optional<int> poolSize)
2323
: base(policy, poolSize)
2424
{
2525
}
26+
2627
public static DictionaryBuilderPool<TKey, TValue> Create(
2728
Optional<IEqualityComparer<TKey>?> keyComparer = default,
2829
Optional<int> poolSize = default)

src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/PooledObjects/PooledArrayBuilder`1.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Runtime.CompilerServices;
1010
using System.Runtime.InteropServices;
1111
using Microsoft.AspNetCore.Razor.Utilities;
12-
using Microsoft.Extensions.ObjectPool;
1312

1413
namespace Microsoft.AspNetCore.Razor.PooledObjects;
1514

@@ -34,7 +33,7 @@ internal partial struct PooledArrayBuilder<T> : IDisposable
3433
/// </summary>
3534
private const int InlineCapacity = 4;
3635

37-
private ObjectPool<ImmutableArray<T>.Builder>? _builderPool;
36+
private ArrayBuilderPool<T>? _builderPool;
3837

3938
/// <summary>
4039
/// A builder to be used as storage after the first time that the number
@@ -59,7 +58,7 @@ internal partial struct PooledArrayBuilder<T> : IDisposable
5958
/// </summary>
6059
private int _inlineCount;
6160

62-
public PooledArrayBuilder(int? capacity = null, ObjectPool<ImmutableArray<T>.Builder>? builderPool = null)
61+
public PooledArrayBuilder(int? capacity = null, ArrayBuilderPool<T>? builderPool = null)
6362
{
6463
_capacity = capacity is > InlineCapacity ? capacity : null;
6564
_builderPool = builderPool;

src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/PooledObjects/PooledDictionaryBuilder`2.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Collections.Immutable;
7-
using Microsoft.Extensions.ObjectPool;
87

98
namespace Microsoft.AspNetCore.Razor.PooledObjects;
109

@@ -13,10 +12,10 @@ namespace Microsoft.AspNetCore.Razor.PooledObjects;
1312
/// it's needed. Note: Dispose this to ensure that the pooled array builder is returned
1413
/// to the pool.
1514
/// </summary>
16-
internal ref struct PooledDictionaryBuilder<TKey, TValue>(ObjectPool<ImmutableDictionary<TKey, TValue>.Builder>? pool)
15+
internal ref struct PooledDictionaryBuilder<TKey, TValue>(DictionaryBuilderPool<TKey, TValue>? pool)
1716
where TKey : notnull
1817
{
19-
private readonly ObjectPool<ImmutableDictionary<TKey, TValue>.Builder> _pool = pool ?? DictionaryBuilderPool<TKey, TValue>.Default;
18+
private readonly DictionaryBuilderPool<TKey, TValue> _pool = pool ?? DictionaryBuilderPool<TKey, TValue>.Default;
2019

2120
private ImmutableDictionary<TKey, TValue>.Builder? _builder;
2221

src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/PooledObjects/PooledHashSet`1.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Collections.Immutable;
7-
using Microsoft.Extensions.ObjectPool;
87

98
namespace Microsoft.AspNetCore.Razor.PooledObjects;
109

@@ -15,7 +14,7 @@ namespace Microsoft.AspNetCore.Razor.PooledObjects;
1514
/// </summary>
1615
internal ref struct PooledHashSet<T>
1716
{
18-
private readonly ObjectPool<HashSet<T>> _pool;
17+
private readonly HashSetPool<T> _pool;
1918
#pragma warning disable IDE0052 // Used in NET only code below. Called API doesn't exist on framework.
2019
private readonly int? _capacity;
2120
#pragma warning restore IDE0052
@@ -26,7 +25,7 @@ public PooledHashSet()
2625
{
2726
}
2827

29-
public PooledHashSet(ObjectPool<HashSet<T>> pool)
28+
public PooledHashSet(HashSetPool<T> pool)
3029
: this(pool, capacity: null)
3130
{
3231
}
@@ -36,7 +35,7 @@ public PooledHashSet(int capacity)
3635
{
3736
}
3837

39-
public PooledHashSet(ObjectPool<HashSet<T>>? pool, int? capacity)
38+
public PooledHashSet(HashSetPool<T>? pool, int? capacity)
4039
{
4140
_pool = pool ?? HashSetPool<T>.Default;
4241
_capacity = capacity;

src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/PooledObjects/PooledList`1.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics.CodeAnalysis;
7-
using Microsoft.Extensions.ObjectPool;
87

98
namespace Microsoft.AspNetCore.Razor.PooledObjects;
109

@@ -15,15 +14,15 @@ namespace Microsoft.AspNetCore.Razor.PooledObjects;
1514
/// </summary>
1615
internal ref partial struct PooledList<T>
1716
{
18-
private readonly ObjectPool<List<T>> _pool;
17+
private readonly ListPool<T> _pool;
1918
private List<T>? _list;
2019

2120
public PooledList()
2221
: this(ListPool<T>.Default)
2322
{
2423
}
2524

26-
public PooledList(ObjectPool<List<T>> pool)
25+
public PooledList(ListPool<T> pool)
2726
{
2827
_pool = pool;
2928
}

0 commit comments

Comments
 (0)