Skip to content

fix: Use in-place append for buffer concatenation to fix O(n^2) performance#80

Merged
keelerm84 merged 2 commits intomainfrom
mk/sdk-1963/performance
Mar 2, 2026
Merged

fix: Use in-place append for buffer concatenation to fix O(n^2) performance#80
keelerm84 merged 2 commits intomainfrom
mk/sdk-1963/performance

Conversation

@keelerm84
Copy link
Member

@keelerm84 keelerm84 commented Mar 2, 2026

The previous change (0e2e80d) replaced buffer << chunk with
buffer += chunk to handle frozen strings. However, += creates a new
string on every iteration, resulting in O(n^2) behavior for large
payloads. The buffer is already explicitly unfrozen (+"".b), so <<
is safe to use. This restores the in-place append while keeping the
.dup on the incoming chunk to avoid mutating frozen caller strings.


Note

Low Risk
Low risk: small, localized change to streaming buffer concatenation; main concern is any subtle string mutability/encoding edge cases during line parsing.

Overview
Restores in-place buffer growth in BufferedLineReader.lines_from by switching from buffer += chunk back to buffer << chunk, avoiding per-chunk string allocations and O(n^2) behavior on large streams.

Also ensures the buffer reset path reinitializes to an unfrozen binary string (+"".b) rather than a potentially frozen empty literal.

Written by Cursor Bugbot for commit 37d5f65. This will update automatically on new commits. Configure here.

…rmance

The previous change (0e2e80d) replaced `buffer << chunk` with
`buffer += chunk` to handle frozen strings. However, `+=` creates a new
string on every iteration, resulting in O(n^2) behavior for large
payloads. The buffer is already explicitly unfrozen (`+"".b`), so `<<`
is safe to use. This restores the in-place append while keeping the
`.dup` on the incoming chunk to avoid mutating frozen caller strings.
@keelerm84 keelerm84 requested a review from a team as a code owner March 2, 2026 15:08
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Replace `buffer = ""` with `buffer = +"".b` so the reset buffer remains
unfrozen and binary-encoded, matching the initial assignment. Without
this, `buffer << chunk` after the reset would raise a FrozenError in
Ruby 3.4+ where string literals are frozen by default.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@keelerm84 keelerm84 merged commit a7e6c23 into main Mar 2, 2026
10 checks passed
@keelerm84 keelerm84 deleted the mk/sdk-1963/performance branch March 2, 2026 16:30
keelerm84 pushed a commit that referenced this pull request Mar 2, 2026
🤖 I have created a release *beep* *boop*
---


##
[2.5.1](2.5.0...2.5.1)
(2026-03-02)


### Bug Fixes

* Use in-place append for buffer concatenation to fix O(n^2) performance
([#80](#80))
([a7e6c23](a7e6c23))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

2 participants