Use unsafe []byte->string cast to reuse buffer

Signed-off-by: Nicolás Pazos <npazosmendez@gmail.com>
This commit is contained in:
Nicolás Pazos 2023-11-07 11:26:50 -03:00
parent 5dbe3c1919
commit f46a104752
2 changed files with 9 additions and 9 deletions

View file

@ -18,9 +18,9 @@ import (
"errors"
"math"
"strconv"
"strings"
"sync"
"time"
"unsafe"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
@ -1877,7 +1877,7 @@ type offLenPair struct {
}
type rwSymbolTable struct {
symbols strings.Builder
symbols []byte
symbolsMap map[string]offLenPair
}
@ -1891,21 +1891,21 @@ func (r *rwSymbolTable) Ref(str string) (off uint32, leng uint32) {
if offlen, ok := r.symbolsMap[str]; ok {
return offlen.Off, offlen.Len
}
off, leng = uint32(r.symbols.Len()), uint32(len(str))
r.symbols.WriteString(str)
off, leng = uint32(len(r.symbols)), uint32(len(str))
r.symbols = append(r.symbols, str...)
r.symbolsMap[str] = offLenPair{off, leng}
return
}
func (r *rwSymbolTable) LabelsString() string {
return r.symbols.String()
return *((*string)(unsafe.Pointer(&r.symbols)))
}
func (r *rwSymbolTable) clear() {
for k := range r.symbolsMap {
delete(r.symbolsMap, k)
}
r.symbols.Reset()
r.symbols = r.symbols[:0]
}
func buildMinimizedWriteRequest(samples []prompb.MinimizedTimeSeries, labels string, pBuf *[]byte, buf *[]byte) ([]byte, int64, error) {

View file

@ -1564,12 +1564,12 @@ func BenchmarkBuildMinimizedWriteRequest(b *testing.B) {
// todo: add other types
//seriesBuff[i].Exemplars = []prompb.Exemplar{{}}
}
pBuf := proto.NewBuffer(nil)
pBuf := []byte{}
// Warmup buffers
for i := 0; i < 10; i++ {
populateMinimizedTimeSeries(&symbolTable, tc.batch, seriesBuff, true, true)
buildMinimizedWriteRequest(seriesBuff, symbolTable.symbols.String(), pBuf, &buff)
buildMinimizedWriteRequest(seriesBuff, symbolTable.LabelsString(), &pBuf, &buff)
}
b.Run(fmt.Sprintf("%d-instances", len(tc.batch)), func(b *testing.B) {
@ -1577,7 +1577,7 @@ func BenchmarkBuildMinimizedWriteRequest(b *testing.B) {
for j := 0; j < b.N; j++ {
populateMinimizedTimeSeries(&symbolTable, tc.batch, seriesBuff, true, true)
b.ResetTimer()
req, _, err := buildMinimizedWriteRequest(seriesBuff, symbolTable.symbols.String(), pBuf, &buff)
req, _, err := buildMinimizedWriteRequest(seriesBuff, symbolTable.LabelsString(), &pBuf, &buff)
if err != nil {
b.Fatal(err)
}