Skip to content

refactor(seahorse): use strings.Builder in compaction string helpers - #3243

Closed
corporatepiyush wants to merge 1 commit into
sipeed:mainfrom
corporatepiyush:refactor/seahorse-compaction-strings-builder
Closed

refactor(seahorse): use strings.Builder in compaction string helpers#3243
corporatepiyush wants to merge 1 commit into
sipeed:mainfrom
corporatepiyush:refactor/seahorse-compaction-strings-builder

Conversation

@corporatepiyush

Copy link
Copy Markdown

📝 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 to strings.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

  • 🐞 Bug fix
  • ✨ New feature
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written

🔗 Related Issue

N/A — self-contained allocation cleanup on the context-compaction path.

📚 Technical Context

  • Reasoning: += on a growing string is quadratic in the number of iterations; strings.Builder grows 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):

function before after speedup memory
formatMessagesForSummary 375µs · 1.78 MB · 1004 allocs 53µs · 95 KB · 615 allocs 7.1× −94.7%
truncateSummary 254µs · 1.24 MB · 204 allocs 10µs · 49 KB · 15 allocs 25× −96%

Reproduce:

go test -run='^$' -bench='BenchmarkFormatMessagesForSummary|BenchmarkTruncateSummary' -benchmem ./pkg/seahorse/

(Both benchmarks are added in this PR.)

🧪 Test Environment

  • Hardware: Apple Silicon (arm64)
  • OS: macOS 15
  • Model/Provider: N/A — pure refactor, no LLM calls exercised
  • Channels: N/A

Verification: go vet ./pkg/seahorse/ and go test ./pkg/seahorse/ pass; gofmt -s clean. Existing parts_roundtrip_test.go (which asserts the output of formatMessagesForSummary and truncateSummary) still passes.

☑️ Checklist

  • My code follows the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly. (N/A — internal helpers, no documented behavior changes.)

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.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions

Copy link
Copy Markdown

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.

@github-actions

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants