Skip to content

Use strings.Builder instead of += concatenation in loops#524

Merged
mengzhuo merged 2 commits intosipeed:mainfrom
mattn:perf/strings-builder
Feb 20, 2026
Merged

Use strings.Builder instead of += concatenation in loops#524
mengzhuo merged 2 commits intosipeed:mainfrom
mattn:perf/strings-builder

Conversation

@mattn
Copy link
Contributor

@mattn mattn commented Feb 20, 2026

📝 Description

Replace string concatenation with strings.Builder in:

  • LoadBootstrapFiles (context.go)
  • GetRecentDailyNotes, GetMemoryContext (memory.go)
  • formatMessagesForLog, formatToolsForLog, summarizeBatch (loop.go)

Reduces allocations from O(n^2) to O(n) for repeated appends.

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written (Human lead, AI assisted or none)

🔗 Related Issue

📚 Technical Context (Skip for Docs)

  • Reference URL:
  • Reasoning:

🧪 Test Environment

  • Hardware:
  • OS:
  • Model/Provider:
  • Channels:

📸 Evidence (Optional)

Click to view Logs/Screenshots

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly.

Copilot AI review requested due to automatic review settings February 20, 2026 11:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors string concatenation operations to use strings.Builder instead of += operators in loops, improving performance from O(n²) to O(n) complexity for repeated string appends. The changes target utility and formatting functions in the agent package that build text output for prompts, logs, and memory contexts.

Changes:

  • Replaced string concatenation with strings.Builder in multiple functions across memory.go, loop.go, and context.go
  • Renamed parameter in formatToolsForLog from tools to toolDefs for clarity
  • Consolidated logic in GetMemoryContext and GetRecentDailyNotes to use strings.Builder pattern

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
pkg/agent/memory.go Refactored GetRecentDailyNotes and GetMemoryContext to use strings.Builder, but introduced a bug with duplicate "# Memory" header
pkg/agent/loop.go Converted formatMessagesForLog, formatToolsForLog, and summarizeBatch to use strings.Builder for efficient string building
pkg/agent/context.go Updated LoadBootstrapFiles to use strings.Builder instead of string concatenation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@nikolasdehor nikolasdehor left a comment

Choose a reason for hiding this comment

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

Clean refactor. The string concatenation → strings.Builder conversions are correct and well-scoped.

A few notes:

  1. context.go L152fmt.Fprintf(&sb, "...", filename, data) where data is []byte: this works because %s handles []byte correctly in Go, so this is fine and avoids the string(data) allocation. Good catch.

  2. memory.go GetMemoryContext() — The refactored version has a subtle behavior change: the old code only prepended # Memory\n\n at the end via fmt.Sprintf, but the new code writes it to the builder first. Functionally equivalent, just noting it for review.

  3. loop.go — The toolstoolDefs rename in formatToolsForLog is a nice bonus to avoid shadowing the parameter name with the range variable, though Go wouldn't shadow it here since the types differ. Still cleaner.

LGTM — straightforward performance improvement with no functional changes.

@mengzhuo mengzhuo merged commit 5522776 into sipeed:main Feb 20, 2026
4 checks passed
hyperwd pushed a commit to hyperwd/picoclaw that referenced this pull request Mar 5, 2026
Use strings.Builder instead of += concatenation in loops
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.

4 participants