Skip to content

pool string builders used in aggregator to concatenate tags#336

Open
jstncbllr wants to merge 2 commits into
DataDog:masterfrom
jstncbllr:master
Open

pool string builders used in aggregator to concatenate tags#336
jstncbllr wants to merge 2 commits into
DataDog:masterfrom
jstncbllr:master

Conversation

@jstncbllr

Copy link
Copy Markdown

This reduced allocations significantly for us.

@jstncbllr jstncbllr requested a review from a team as a code owner September 12, 2025 21:22
…ncbllr/master

# Conflicts:
#	statsd/aggregator.go
#	statsd/aggregator_test.go
@carlosroman

Copy link
Copy Markdown
Contributor

@review codex

Comment thread statsd/aggregator.go
var sb strings.Builder
sb := stringBuilderPool.Get().(*strings.Builder)
defer func() {
sb.Reset()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have a concern about correctness of this approach.

AFAIR sb.String() returns a string backed directly by the builder's internal buffer, so it does not copy. The defer here resets and returns the builder to the pool before the caller ever uses the returned strings, meaning the next goroutine to get the builder will overwrite the buffer that the returned string values still point into. This is silent heap corruption under concurrent use and might not be caught by the race detector.

@atanzu

atanzu commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Hi @jstncbllr ,

Thank you for suggesting this improvement!
I left a comment because this part looks a bit suspicious to me. Could you please share your thoughts?

@atanzu atanzu self-assigned this Jun 3, 2026
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