Skip to content

Commit 062bccc

Browse files
committed
Store *TimeSeries in the pool, to avoid boxing
Casting a struct to {}interface has an overhead, because the runtime needs something more like a pointer so it creates a small descriptor object. Changing the data structure to use a pointer avoids this overhead. Signed-off-by: Bryan Boreham <bryan@weave.works>
1 parent 64d04ae commit 062bccc

File tree

6 files changed

+11
-11
lines changed

6 files changed

+11
-11
lines changed

pkg/distributor/distributor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ func (d *Distributor) Push(ctx context.Context, req *client.WriteRequest) (*clie
348348

349349
keys = append(keys, key)
350350
validatedTimeseries = append(validatedTimeseries, client.PreallocTimeseries{
351-
TimeSeries: client.TimeSeries{
351+
TimeSeries: &client.TimeSeries{
352352
Labels: ts.Labels,
353353
Samples: samples,
354354
},

pkg/distributor/distributor_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func makeWriteRequest(samples int) *client.WriteRequest {
384384
request := &client.WriteRequest{}
385385
for i := 0; i < samples; i++ {
386386
ts := client.PreallocTimeseries{
387-
TimeSeries: client.TimeSeries{
387+
TimeSeries: &client.TimeSeries{
388388
Labels: []client.LabelAdapter{
389389
{Name: model.MetricNameLabel, Value: "foo"},
390390
{Name: "bar", Value: "baz"},
@@ -407,7 +407,7 @@ func makeWriteRequestHA(samples int, replica, cluster string) *client.WriteReque
407407
request := &client.WriteRequest{}
408408
for i := 0; i < samples; i++ {
409409
ts := client.PreallocTimeseries{
410-
TimeSeries: client.TimeSeries{
410+
TimeSeries: &client.TimeSeries{
411411
Labels: []client.LabelAdapter{
412412
{Name: "__name__", Value: "foo"},
413413
{Name: "bar", Value: "baz"},
@@ -555,7 +555,7 @@ func (i *mockIngester) Query(ctx context.Context, req *client.QueryRequest, opts
555555
response := client.QueryResponse{}
556556
for _, ts := range i.timeseries {
557557
if match(ts.Labels, matchers) {
558-
response.Timeseries = append(response.Timeseries, ts.TimeSeries)
558+
response.Timeseries = append(response.Timeseries, *ts.TimeSeries)
559559
}
560560
}
561561
return &response, nil

pkg/ingester/client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestMarshall(t *testing.T) {
1717
req := WriteRequest{}
1818
for i := 0; i < 10; i++ {
1919
req.Timeseries = append(req.Timeseries, PreallocTimeseries{
20-
TimeSeries{
20+
&TimeSeries{
2121
Labels: []LabelAdapter{
2222
{"foo", strconv.Itoa(i)},
2323
},

pkg/ingester/client/compat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func ToWriteRequest(lbls []labels.Labels, samples []Sample, source WriteRequest_
2727
}
2828

2929
for i, s := range samples {
30-
ts := timeSeriesPool.Get().(TimeSeries)
30+
ts := timeSeriesPool.Get().(*TimeSeries)
3131
ts.Labels = append(ts.Labels, FromLabelsToLabelAdapters(lbls[i])...)
3232
ts.Samples = append(ts.Samples, s)
3333
req.Timeseries = append(req.Timeseries, PreallocTimeseries{TimeSeries: ts})

pkg/ingester/client/timeseries.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var (
2424

2525
timeSeriesPool = sync.Pool{
2626
New: func() interface{} {
27-
return TimeSeries{
27+
return &TimeSeries{
2828
Labels: make([]LabelAdapter, 0, expectedLabels),
2929
Samples: make([]Sample, 0, expectedSamplesPerSeries),
3030
}
@@ -56,12 +56,12 @@ func (p *PreallocWriteRequest) Unmarshal(dAtA []byte) error {
5656

5757
// PreallocTimeseries is a TimeSeries which preallocs slices on Unmarshal.
5858
type PreallocTimeseries struct {
59-
TimeSeries
59+
*TimeSeries
6060
}
6161

6262
// Unmarshal implements proto.Message.
6363
func (p *PreallocTimeseries) Unmarshal(dAtA []byte) error {
64-
p.TimeSeries = timeSeriesPool.Get().(TimeSeries)
64+
p.TimeSeries = timeSeriesPool.Get().(*TimeSeries)
6565
return p.TimeSeries.Unmarshal(dAtA)
6666
}
6767

@@ -249,7 +249,7 @@ func ReuseSlice(slice []PreallocTimeseries) {
249249
}
250250

251251
// ReuseTimeseries puts the timeseries back into a sync.Pool for reuse.
252-
func ReuseTimeseries(ts TimeSeries) {
252+
func ReuseTimeseries(ts *TimeSeries) {
253253
ts.Labels = ts.Labels[:0]
254254
ts.Samples = ts.Samples[:0]
255255
timeSeriesPool.Put(ts)

pkg/ingester/ingester_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ func benchmarkIngesterSeriesCreationLocking(b *testing.B, parallelism int) {
498498
_, err := ing.Push(ctx, &client.WriteRequest{
499499
Timeseries: []client.PreallocTimeseries{
500500
{
501-
TimeSeries: client.TimeSeries{
501+
TimeSeries: &client.TimeSeries{
502502
Labels: []client.LabelAdapter{
503503
{Name: model.MetricNameLabel, Value: fmt.Sprintf("metric_%d", j)},
504504
},

0 commit comments

Comments
 (0)