Skip to content

Commit 0e544d5

Browse files
committed
refactor: Grow function grows always by pow of 2
1 parent 1cef5b0 commit 0e544d5

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,14 @@ public readonly void Dispose()
290290
[MethodImpl(MethodImplOptions.AggressiveInlining)]
291291
private void Grow(int capacity = 0)
292292
{
293-
var currentSize = buffer.Length;
293+
var size = buffer.Length == 0 ? 8 : buffer.Length;
294294

295-
var newSize = capacity > currentSize ? capacity : currentSize * 2;
296-
var rented = ArrayPool<char>.Shared.Rent(newSize);
295+
while (size < capacity)
296+
{
297+
size *= 2;
298+
}
299+
300+
var rented = ArrayPool<char>.Shared.Rent(size);
297301
buffer[..bufferPosition].CopyTo(rented);
298302
var oldBufferFromPool = arrayFromPool;
299303
buffer = arrayFromPool = rented;

0 commit comments

Comments
 (0)