mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Use unsafe []byte->string cast to reuse buffer
Signed-off-by: Nicolás Pazos <npazosmendez@gmail.com>
This commit is contained in:
parent
5dbe3c1919
commit
f46a104752
|
@ -18,9 +18,9 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/go-kit/log"
|
"github.com/go-kit/log"
|
||||||
"github.com/go-kit/log/level"
|
"github.com/go-kit/log/level"
|
||||||
|
@ -1877,7 +1877,7 @@ type offLenPair struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type rwSymbolTable struct {
|
type rwSymbolTable struct {
|
||||||
symbols strings.Builder
|
symbols []byte
|
||||||
symbolsMap map[string]offLenPair
|
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 {
|
if offlen, ok := r.symbolsMap[str]; ok {
|
||||||
return offlen.Off, offlen.Len
|
return offlen.Off, offlen.Len
|
||||||
}
|
}
|
||||||
off, leng = uint32(r.symbols.Len()), uint32(len(str))
|
off, leng = uint32(len(r.symbols)), uint32(len(str))
|
||||||
r.symbols.WriteString(str)
|
r.symbols = append(r.symbols, str...)
|
||||||
r.symbolsMap[str] = offLenPair{off, leng}
|
r.symbolsMap[str] = offLenPair{off, leng}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rwSymbolTable) LabelsString() string {
|
func (r *rwSymbolTable) LabelsString() string {
|
||||||
return r.symbols.String()
|
return *((*string)(unsafe.Pointer(&r.symbols)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rwSymbolTable) clear() {
|
func (r *rwSymbolTable) clear() {
|
||||||
for k := range r.symbolsMap {
|
for k := range r.symbolsMap {
|
||||||
delete(r.symbolsMap, k)
|
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) {
|
func buildMinimizedWriteRequest(samples []prompb.MinimizedTimeSeries, labels string, pBuf *[]byte, buf *[]byte) ([]byte, int64, error) {
|
||||||
|
|
|
@ -1564,12 +1564,12 @@ func BenchmarkBuildMinimizedWriteRequest(b *testing.B) {
|
||||||
// todo: add other types
|
// todo: add other types
|
||||||
//seriesBuff[i].Exemplars = []prompb.Exemplar{{}}
|
//seriesBuff[i].Exemplars = []prompb.Exemplar{{}}
|
||||||
}
|
}
|
||||||
pBuf := proto.NewBuffer(nil)
|
pBuf := []byte{}
|
||||||
|
|
||||||
// Warmup buffers
|
// Warmup buffers
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
populateMinimizedTimeSeries(&symbolTable, tc.batch, seriesBuff, true, true)
|
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) {
|
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++ {
|
for j := 0; j < b.N; j++ {
|
||||||
populateMinimizedTimeSeries(&symbolTable, tc.batch, seriesBuff, true, true)
|
populateMinimizedTimeSeries(&symbolTable, tc.batch, seriesBuff, true, true)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
req, _, err := buildMinimizedWriteRequest(seriesBuff, symbolTable.symbols.String(), pBuf, &buff)
|
req, _, err := buildMinimizedWriteRequest(seriesBuff, symbolTable.LabelsString(), &pBuf, &buff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue