github.com/kiali/kiali@v1.84.0/tracing/tempo/tempopb/prealloc.go (about)

     1  package tempopb
     2  
     3  import "github.com/kiali/kiali/tracing/tempo/tempopb/pool"
     4  
     5  // buckets: [0.5KiB, 1KiB, 2KiB, 4KiB, 8KiB, 16KiB] ...
     6  var bytePool = pool.New(500, 64_000, 2, func(size int) []byte { return make([]byte, 0, size) })
     7  
     8  // PreallocBytes is a (repeated bytes slices) which preallocs slices on Unmarshal.
     9  type PreallocBytes struct {
    10  	Slice []byte
    11  }
    12  
    13  // Unmarshal implements proto.Message.
    14  func (r *PreallocBytes) Unmarshal(dAtA []byte) error {
    15  	r.Slice = bytePool.Get(len(dAtA))
    16  	r.Slice = r.Slice[:len(dAtA)]
    17  	copy(r.Slice, dAtA)
    18  	return nil
    19  }
    20  
    21  // MarshalTo implements proto.Marshaller.
    22  // returned int is not used
    23  func (r *PreallocBytes) MarshalTo(dAtA []byte) (int, error) {
    24  	copy(dAtA[:], r.Slice[:])
    25  	return len(r.Slice), nil
    26  }
    27  
    28  // Size implements proto.Sizer.
    29  func (r *PreallocBytes) Size() (n int) {
    30  	if r == nil {
    31  		return 0
    32  	}
    33  	return len(r.Slice)
    34  }
    35  
    36  // ReuseByteSlices puts the byte slice back into bytePool for reuse.
    37  func ReuseByteSlices(buffs [][]byte) {
    38  	for _, b := range buffs {
    39  		bytePool.Put(b[:0])
    40  	}
    41  }