Replace O(n²) String.concat in RedisPipeline.add with StringBuf#6
Merged
Conversation
There was a problem hiding this comment.
Build & Tests
Build: pass (local, aarch64-linux)
Tests: 77/77 pass
CI: pass (macOS + Ubuntu)
One minor warning during compilation: [Warning] No 'prn' function found with type (Fn [StringBuf] String) — cosmetic, since StringBuf doesn't derive prn. Not a blocker.
Findings
Clean, focused change. Reviewed redis.carp:544-597 and test/resp.carp diff in full.
- The
StringBuf.append-strcall atredis.carp:563correctly mutates the buffer through(buf p), andset-count!updates the count. Ownership is sound. - In
pipeline-execatredis.carp:596,StringBuf.strconverts the buffer to aStringforTcpStream.send. The temporaryStringlives for the duration of the expression, so the&reference is valid. Correct. - Test updates properly use
StringBuf.strto extract the contents andStringBuf.lengthfor the empty-buffer check. Matches the new types. - No callers of
RedisPipeline.bufoutside the test file access the raw field, so the type change is self-contained.
No bugs, edge cases, or regressions found.
Verdict: merge
Straightforward performance fix. The O(n²) → O(1) analysis is correct, the implementation is clean, and the tests cover the same scenarios as before with the new types.
hellerve
approved these changes
Jun 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RedisPipeline.addrebuilt the entire buffer string on every call viaString.concat, making pipeline construction O(n²) in command count.This replaces the
buffield type fromStringtoStringBuf(fromcarpentry-org/strbuf), which grows geometrically and gives amortized
O(1) appends.
Changes:
strbuf@0.1.0dependencyRedisPipelinefieldbuffromStringtoStringBufbeginnow creates aStringBufinstead of an empty stringaddusesStringBuf.append-strinstead ofString.concatpipeline-execconverts the buffer toStringforTcpStream.sendStringBuf.str/StringBuf.lengthfor assertionsOpened by the carpentry-org heartbeat agent (Claude). Veit has not reviewed this yet.