Skip to content

Optimize ts v2 pool#7373

Open
SungJin1212 wants to merge 1 commit intocortexproject:masterfrom
SungJin1212:Optimize-tsv2-pool
Open

Optimize ts v2 pool#7373
SungJin1212 wants to merge 1 commit intocortexproject:masterfrom
SungJin1212:Optimize-tsv2-pool

Conversation

@SungJin1212
Copy link
Copy Markdown
Member

This PR improves V2 write request memory pooling by preventing string pointer leaks, adding explicit string clearing in ReuseWriteRequestV2 to prevent memory leaks when reusing pooled requests, and removing redundant exemplar label cleanup from ReuseTimeseriesV2.

Which issue(s) this PR fixes:
Fixes #

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Copy link
Copy Markdown
Member

@CharlieTLe CharlieTLe left a comment

Choose a reason for hiding this comment

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

The V2 pool was introduced in 918aa41 and shipped in v1.20.0, so the missing symbol clearing is a bug in a released version. Should a [BUGFIX] CHANGELOG entry be added?

Comment on lines +83 to +84
assert.Equal(t, "", underlyingArray[1])
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Three symbols are appended ("", "name", "test") but only indices 0 and 1 are checked. Should index 2 be asserted as well?

Suggested change
assert.Equal(t, "", underlyingArray[1])
}
assert.Equal(t, "", underlyingArray[1])
assert.Equal(t, "", underlyingArray[2])
}

})
}

func TestReuseWriteRequestV2(t *testing.T) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could this test flake since sync.Pool doesn't guarantee Get returns the same object that was just Put? If the GC runs between the ReuseWriteRequestV2 and PreallocWriteRequestV2FromPool calls, a fresh object would be returned and the backing array assertions would be skipped.

Would it be more robust to capture a reference to the backing array before the reuse call, so the clearing can be verified directly without depending on the pool?

// Capture backing array before reuse
backingArray := req.Symbols[:cap(req.Symbols)]

ReuseWriteRequestV2(req)

// Verify clearing directly on the backing array
for i, s := range backingArray[:3] {
    assert.Equalf(t, "", s, "symbol at index %d not cleared", i)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I fixed it at the latest commit to check if the symbol table is reset without GC.
Thanks for the comment!

@SungJin1212 SungJin1212 force-pushed the Optimize-tsv2-pool branch 2 times, most recently from fdb24ce to f263b6c Compare March 30, 2026 00:07
@SungJin1212 SungJin1212 requested a review from CharlieTLe March 30, 2026 00:30
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants