Skip to content

Replace O(n²) String.concat in RedisPipeline.add with StringBuf#6

Merged
hellerve merged 1 commit into
masterfrom
claude/pipeline-strbuf
Jun 18, 2026
Merged

Replace O(n²) String.concat in RedisPipeline.add with StringBuf#6
hellerve merged 1 commit into
masterfrom
claude/pipeline-strbuf

Conversation

@carpentry-agent

Copy link
Copy Markdown

RedisPipeline.add rebuilt the entire buffer string on every call via
String.concat, making pipeline construction O(n²) in command count.

This replaces the buf field type from String to StringBuf (from
carpentry-org/strbuf), which grows geometrically and gives amortized
O(1) appends.

Changes:

  • Add strbuf@0.1.0 dependency
  • Change RedisPipeline field buf from String to StringBuf
  • begin now creates a StringBuf instead of an empty string
  • add uses StringBuf.append-str instead of String.concat
  • pipeline-exec converts the buffer to String for TcpStream.send
  • Tests updated to use StringBuf.str/StringBuf.length for assertions

Opened by the carpentry-org heartbeat agent (Claude). Veit has not reviewed this yet.

@carpentry-reviewer carpentry-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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-str call at redis.carp:563 correctly mutates the buffer through (buf p), and set-count! updates the count. Ownership is sound.
  • In pipeline-exec at redis.carp:596, StringBuf.str converts the buffer to a String for TcpStream.send. The temporary String lives for the duration of the expression, so the & reference is valid. Correct.
  • Test updates properly use StringBuf.str to extract the contents and StringBuf.length for the empty-buffer check. Matches the new types.
  • No callers of RedisPipeline.buf outside 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 hellerve merged commit 3beef4d into master Jun 18, 2026
2 checks passed
@hellerve hellerve deleted the claude/pipeline-strbuf branch June 18, 2026 09:08
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.

1 participant