Skip to content

Commit 82fecfe

Browse files
Prefer exponential growth in PooledArrayBuilder<T>.SetCapacityIfLarger
Fixes #12019
1 parent e019d19 commit 82fecfe

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,19 @@ public void SetCapacityIfLarger(int value)
255255
{
256256
if (value > Capacity)
257257
{
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+
258262
if (TryGetBuilder(out var builder))
259263
{
260-
Debug.Assert(value > builder.Capacity);
261-
builder.Capacity = value;
264+
Debug.Assert(newCapacity > builder.Capacity);
265+
builder.Capacity = newCapacity;
262266
}
263267
else
264268
{
265-
Debug.Assert(value > (_capacity ?? InlineCapacity));
266-
_capacity = value;
269+
Debug.Assert(newCapacity > (_capacity ?? InlineCapacity));
270+
_capacity = newCapacity;
267271
}
268272
}
269273
}

0 commit comments

Comments
 (0)