Skip to content

Fix Buffer<T>.GrowBuffer() using wrong variable for allocation size#104

Open
Beldaa wants to merge 1 commit intotoptensoftware:mainfrom
Beldaa:fix/buffer-grow-allocation
Open

Fix Buffer<T>.GrowBuffer() using wrong variable for allocation size#104
Beldaa wants to merge 1 commit intotoptensoftware:mainfrom
Beldaa:fix/buffer-grow-allocation

Conversation

@Beldaa
Copy link
Copy Markdown

@Beldaa Beldaa commented Apr 18, 2026

Fixes #103

Summary

GrowBuffer() calculated an optimal capacity in newLength (doubling below 1MB, linear growth above) but then allocated the new array with requiredLength instead, making the entire growth strategy dead code. This caused O(n) reallocations instead of O(log n), degrading sequential Add/Insert operations from O(n) amortized to O(n^2).

Change

One-line fix in Buffer.cs line 113:

  • Before: var newData = new T[requiredLength];
    • After: var newData = new T[newLength];

Affected components

Buffer is used throughout the library: BiDi algorithm, text shaping, Utf32Buffer, and TextDocument. Any non-trivial text processing benefits from this fix.

GrowBuffer() calculated an optimal capacity in
ewLength (doubling below 1MB, linear growth above) but then allocated the new array with 
equiredLength instead, making the growth strategy dead code. This caused O(n) reallocations instead of O(log n), degrading sequential Add/Insert operations from O(n) amortized to O(n²).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Buffer<T>.GrowBuffer() allocates with wrong variable, defeating growth strategy

1 participant