@@ -354,23 +354,29 @@ func (i *Ingester) TransferTSDB(stream client.Ingester_TransferTSDBServer) error
354
354
// The passed wireChunks slice is for re-use.
355
355
func toWireChunks (descs []* desc , wireChunks []client.Chunk ) ([]client.Chunk , error ) {
356
356
if cap (wireChunks ) < len (descs ) {
357
- wireChunks = make ([]client.Chunk , 0 , len (descs ))
357
+ wireChunks = make ([]client.Chunk , len (descs ))
358
+ } else {
359
+ wireChunks = wireChunks [:len (descs )]
358
360
}
359
- wireChunks = wireChunks [:0 ]
360
- for _ , d := range descs {
361
+ for i , d := range descs {
361
362
wireChunk := client.Chunk {
362
363
StartTimestampMs : int64 (d .FirstTime ),
363
364
EndTimestampMs : int64 (d .LastTime ),
364
365
Encoding : int32 (d .C .Encoding ()),
365
366
}
366
367
367
- buf := bytes .NewBuffer (make ([]byte , 0 , d .C .Size ()))
368
+ slice := wireChunks [i ].Data [:0 ] // try to re-use the memory from last time
369
+ if cap (slice ) < d .C .Size () {
370
+ slice = make ([]byte , 0 , d .C .Size ())
371
+ }
372
+ buf := bytes .NewBuffer (slice )
373
+
368
374
if err := d .C .Marshal (buf ); err != nil {
369
375
return nil , err
370
376
}
371
377
372
378
wireChunk .Data = buf .Bytes ()
373
- wireChunks = append ( wireChunks , wireChunk )
379
+ wireChunks [ i ] = wireChunk
374
380
}
375
381
return wireChunks , nil
376
382
}
0 commit comments