refactor(seahorse): use strings.Builder in compaction string helpers - #3243
Closed
corporatepiyush wants to merge 1 commit into
Closed
refactor(seahorse): use strings.Builder in compaction string helpers#3243corporatepiyush wants to merge 1 commit into
corporatepiyush wants to merge 1 commit into
Conversation
The compaction helpers assemble prompt text from every message in a chunk using `result += fmt.Sprintf(...)` / `content += s`, which is O(n²): each iteration reallocates and copies the whole accumulated string. Switch them to strings.Builder (amortised O(n)). Output is byte-identical (verified against the previous implementation for n = 0, 1, 7, 200). Functions: formatMessagesForSummary, formatSummariesForCondensation, truncateSummary, truncateCondensedSummaries. Benchmarks (200 messages, go1.26, darwin/arm64, -benchmem): formatMessagesForSummary 375µs -> 53µs 1.78MB -> 95KB/op (7.1x faster, 94.7% less mem) truncateSummary 254µs -> 10µs 1.24MB -> 49KB/op (25x faster, 96% less mem) Adds BenchmarkFormatMessagesForSummary and BenchmarkTruncateSummary.
|
|
|
This PR has had no activity for 7 days and has been marked as stale. If you are still working on it, please push an update or leave a comment; otherwise it will be closed automatically in 7 days. |
|
This PR has been closed after 14 days of inactivity. If you would like to continue, feel free to reopen it or submit a new PR. |
This was referenced Jul 24, 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.
📝 Description
The seahorse compaction helpers assemble prompt text from every message in a chunk using
result += fmt.Sprintf(...)/content += s. That pattern is O(n²): each+=allocates a new backing array and copies the whole accumulated string. This PR switches the four helpers tostrings.Builder(amortised O(n), single growing buffer).Output is byte-identical to the previous implementation (verified against the old code for
n = 0, 1, 7, 200).Functions changed (all unexported helpers, no API change):
formatMessagesForSummary,formatSummariesForCondensation,truncateSummary,truncateCondensedSummaries.🗣️ Type of Change
🤖 AI Code Generation
🔗 Related Issue
N/A — self-contained allocation cleanup on the context-compaction path.
📚 Technical Context
+=on a growing string is quadratic in the number of iterations;strings.Buildergrows one backing buffer and appends in place. These helpers run during context compaction on chunks that can hold hundreds of messages, so this is directly on-brand for PicoClaw's low-memory goals.Benchmarks (200 messages,
go1.26,darwin/arm64,-benchmem):formatMessagesForSummarytruncateSummaryReproduce:
(Both benchmarks are added in this PR.)
🧪 Test Environment
Verification:
go vet ./pkg/seahorse/andgo test ./pkg/seahorse/pass;gofmt -sclean. Existingparts_roundtrip_test.go(which asserts the output offormatMessagesForSummaryandtruncateSummary) still passes.☑️ Checklist