Skip to content

Commit f844d3c

Browse files
Remove PooledArrayBuilder<T>.SetCapacityIfLarger method
This method is a bit tricky because it can cause a pooled `ImmutableArray<T>.Builder` to grow with a capacity that isn't a power of two. However, none of the callers of this method really need to use it. So, we can just remove it.
1 parent 709dfde commit f844d3c

File tree

3 files changed

+8
-31
lines changed

3 files changed

+8
-31
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Components/ComponentEventHandlerLoweringPass.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ private static IntermediateNode RewriteUsage(IntermediateNode parent, TagHelperD
172172
// correct context for intellisense when typing in the attribute.
173173
var eventArgsType = node.TagHelper.GetEventArgsType();
174174

175-
using var tokens = new PooledArrayBuilder<IntermediateToken>(original.Length + 2);
176-
tokens.SetCapacityIfLarger(original.Length + 2);
175+
using var tokens = new PooledArrayBuilder<IntermediateToken>(capacity: original.Length + 2);
177176

178177
tokens.Add(
179178
new IntermediateToken()

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Syntax/SyntaxRewriter.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public virtual SyntaxList<TNode> VisitList<TNode>(SyntaxList<TNode> list)
4747
return list;
4848
}
4949

50-
using PooledArrayBuilder<TNode> builder = [];
50+
using var builder = new PooledArrayBuilder<TNode>(capacity: count);
5151

5252
var isUpdating = false;
5353

@@ -61,8 +61,6 @@ public virtual SyntaxList<TNode> VisitList<TNode>(SyntaxList<TNode> list)
6161
{
6262
// The list is being updated, so we need to initialize the builder
6363
// add the items we've seen so far.
64-
builder.SetCapacityIfLarger(count);
65-
6664
builder.AddRange(list, startIndex: 0, count: i);
6765

6866
isUpdating = true;
@@ -93,7 +91,7 @@ public virtual SyntaxTokenList VisitList(SyntaxTokenList list)
9391
return list;
9492
}
9593

96-
using PooledArrayBuilder<SyntaxToken> builder = [];
94+
using var builder = new PooledArrayBuilder<SyntaxToken>(count);
9795

9896
var isUpdating = false;
9997

@@ -107,8 +105,6 @@ public virtual SyntaxTokenList VisitList(SyntaxTokenList list)
107105
{
108106
// The list is being updated, so we need to initialize the builder
109107
// add the items we've seen so far.
110-
builder.SetCapacityIfLarger(count);
111-
112108
builder.AddRange(list, startIndex: 0, count: i);
113109

114110
isUpdating = true;

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Diagnostics.CodeAnalysis;
99
using System.Runtime.CompilerServices;
1010
using System.Runtime.InteropServices;
11-
using Microsoft.AspNetCore.Razor;
1211
using Microsoft.AspNetCore.Razor.Utilities;
1312
using Microsoft.Extensions.ObjectPool;
1413

@@ -251,27 +250,6 @@ public readonly int Count
251250
public readonly int Capacity
252251
=> _builder?.Capacity ?? _capacity ?? InlineCapacity;
253252

254-
public void SetCapacityIfLarger(int value)
255-
{
256-
if (value > Capacity)
257-
{
258-
// For pooled arrays, we prefer exponential growth minimize the number of times
259-
// the internal array has to be resized.
260-
var newCapacity = Math.Max(value, Capacity * 2);
261-
262-
if (TryGetBuilder(out var builder))
263-
{
264-
Debug.Assert(newCapacity > builder.Capacity);
265-
builder.Capacity = newCapacity;
266-
}
267-
else
268-
{
269-
Debug.Assert(newCapacity > (_capacity ?? InlineCapacity));
270-
_capacity = newCapacity;
271-
}
272-
}
273-
}
274-
275253
public void Add(T item)
276254
=> Insert(Count, item);
277255

@@ -1600,7 +1578,11 @@ private void MoveInlineItemsToBuilder()
16001578

16011579
if (_capacity is int capacity)
16021580
{
1603-
builder.SetCapacityIfLarger(capacity);
1581+
builder.Capacity = capacity;
1582+
}
1583+
else if (builder.Capacity < InlineCapacity)
1584+
{
1585+
builder.Capacity = InlineCapacity;
16041586
}
16051587

16061588
_builder = builder;

0 commit comments

Comments
 (0)