Skip to content

refactor: use strings.Builder for message merging in oaistream#2986

Merged
dgageot merged 1 commit into
docker:mainfrom
dgageot:board/5c138ac7425ba4ff
Jun 3, 2026
Merged

refactor: use strings.Builder for message merging in oaistream#2986
dgageot merged 1 commit into
docker:mainfrom
dgageot:board/5c138ac7425ba4ff

Conversation

@dgageot
Copy link
Copy Markdown
Member

@dgageot dgageot commented Jun 3, 2026

Consecutive same-role messages in the OpenAI stream handler were being merged by concatenating strings with += in a loop. This causes repeated allocations and copying as the accumulated content grows.

The MergeConsecutiveMessages function now uses strings.Builder instead, which buffers the concatenation and reduces allocations to a single final operation. The change is strictly behavior-preserving: the condition mergedContent.Len() > 0 replaces mergedContent != "", and mergedContent.String() yields the same accumulated value.

Testing confirmed the change passes go build ./..., unit tests in pkg/model/provider/oaistream/, and golangci-lint with zero issues.

@dgageot dgageot requested a review from a team as a code owner June 3, 2026 09:47
Copy link
Copy Markdown

@docker-agent docker-agent left a comment

Choose a reason for hiding this comment

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

Assessment: 🟢 APPROVE

The strings.Builder refactor in MergeConsecutiveMessages is behaviorally equivalent to the original += concatenation in all code paths:

  • var mergedContent strings.Builder is declared inside the per-group if block, so it is freshly zero-valued on every outer-loop iteration — no cross-group state leakage.
  • mergedContent.Len() > 0 is semantically identical to mergedContent != "" for the newline-separator guard.
  • mergedContent.String() on a never-written builder returns "", matching the old zero-value string — so calls to openai.SystemMessage("") / openai.UserMessage("") when only multi-content parts exist are unaffected.
  • WriteString("") (for an empty str returned by getStringContent) is a no-op, matching the old mergedContent += "" behaviour.

No bugs or regressions identified. The change correctly reduces allocations without altering observable behaviour.

@dgageot dgageot force-pushed the board/5c138ac7425ba4ff branch from 1f38a45 to ca223e7 Compare June 3, 2026 09:57
Replace string concatenation in the consecutive-message merge loop with
a strings.Builder to avoid repeated reallocation as content grows.
Behavior is unchanged.
@dgageot dgageot force-pushed the board/5c138ac7425ba4ff branch from ca223e7 to 302668d Compare June 3, 2026 12:24
@dgageot dgageot merged commit 0ff1168 into docker:main Jun 3, 2026
5 checks passed
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.

3 participants