refactor: use strings.Builder for message merging in oaistream#2986
Merged
Conversation
docker-agent
reviewed
Jun 3, 2026
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟢 APPROVE
The strings.Builder refactor in MergeConsecutiveMessages is behaviorally equivalent to the original += concatenation in all code paths:
var mergedContent strings.Builderis declared inside the per-groupifblock, so it is freshly zero-valued on every outer-loop iteration — no cross-group state leakage.mergedContent.Len() > 0is semantically identical tomergedContent != ""for the newline-separator guard.mergedContent.String()on a never-written builder returns"", matching the old zero-value string — so calls toopenai.SystemMessage("")/openai.UserMessage("")when only multi-content parts exist are unaffected.WriteString("")(for an emptystrreturned bygetStringContent) is a no-op, matching the oldmergedContent += ""behaviour.
No bugs or regressions identified. The change correctly reduces allocations without altering observable behaviour.
1f38a45 to
ca223e7
Compare
rumpl
approved these changes
Jun 3, 2026
Replace string concatenation in the consecutive-message merge loop with a strings.Builder to avoid repeated reallocation as content grows. Behavior is unchanged.
ca223e7 to
302668d
Compare
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.
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
MergeConsecutiveMessagesfunction now usesstrings.Builderinstead, which buffers the concatenation and reduces allocations to a single final operation. The change is strictly behavior-preserving: the conditionmergedContent.Len() > 0replacesmergedContent != "", andmergedContent.String()yields the same accumulated value.Testing confirmed the change passes
go build ./..., unit tests inpkg/model/provider/oaistream/, and golangci-lint with zero issues.