Skip to content

Commit beb33bd

Browse files
committed
Optimize ts v2 pool
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
1 parent 25e8d7b commit beb33bd

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

pkg/cortexpb/timeseriesv2.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func ReuseWriteRequestV2(req *PreallocWriteRequestV2) {
7777
req.data = nil
7878
}
7979
req.Source = 0
80+
81+
for i := range req.Symbols {
82+
req.Symbols[i] = ""
83+
}
8084
req.Symbols = req.Symbols[:0]
8185
if req.Timeseries != nil {
8286
ReuseSliceV2(req.Timeseries)
@@ -121,11 +125,6 @@ func ReuseTimeseriesV2(ts *TimeSeriesV2) {
121125
ts.Metadata.UnitRef = 0
122126
ts.Metadata.HelpRef = 0
123127

124-
// clear exemplar label refs
125-
for i := range ts.Exemplars {
126-
ts.Exemplars[i].LabelsRefs = ts.Exemplars[i].LabelsRefs[:0]
127-
}
128-
129128
for i := range ts.Histograms {
130129
ts.Histograms[i].Reset()
131130
}

pkg/cortexpb/timeseriesv2_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ func TestTimeseriesV2FromPool(t *testing.T) {
5252
})
5353
}
5454

55+
func TestReuseWriteRequestV2(t *testing.T) {
56+
req := PreallocWriteRequestV2FromPool()
57+
58+
// Populate req with some data.
59+
req.Source = RULE
60+
req.Symbols = append(req.Symbols, "", "__name__", "test")
61+
62+
tsSlice := PreallocTimeseriesV2SliceFromPool()
63+
tsSlice = append(tsSlice, PreallocTimeseriesV2{TimeSeriesV2: TimeseriesV2FromPool()})
64+
req.Timeseries = tsSlice
65+
66+
// Put the request back into the pool
67+
ReuseWriteRequestV2(req)
68+
69+
// Retrieve it from the pool again
70+
reused := PreallocWriteRequestV2FromPool()
71+
72+
// Source is reset to default
73+
assert.Equal(t, API, reused.Source)
74+
// The symbol length is properly reset to 0.
75+
assert.Len(t, reused.Symbols, 0)
76+
// Timeseries slice is nil
77+
assert.Nil(t, reused.Timeseries)
78+
79+
// The underlying array's string pointers are cleared to "" to prevent memory leak.
80+
if cap(reused.Symbols) > 0 {
81+
underlyingArray := reused.Symbols[:cap(reused.Symbols)]
82+
assert.Equal(t, "", underlyingArray[0])
83+
assert.Equal(t, "", underlyingArray[1])
84+
}
85+
}
86+
5587
func BenchmarkMarshallWriteRequestV2(b *testing.B) {
5688
ts := PreallocTimeseriesV2SliceFromPool()
5789

0 commit comments

Comments
 (0)