Moved to v2 xxhash for improvements.

New:

/tmp/___BenchmarkLabels_Hash_in_github_com_prometheus_prometheus_pkg_labels -test.v -test.bench ^\QBenchmarkLabels_Hash\E$ -test.run ^$ -test.benchtime 10s
goos: linux
goarch: amd64
pkg: github.com/prometheus/prometheus/pkg/labels
BenchmarkLabels_Hash
BenchmarkLabels_Hash/typical_labels_under_1KB
BenchmarkLabels_Hash/typical_labels_under_1KB-12         	53447894	       221 ns/op	       0 B/op	       0 allocs/op
BenchmarkLabels_Hash/bigger_labels_over_1KB
BenchmarkLabels_Hash/bigger_labels_over_1KB-12           	42341754	       326 ns/op	       0 B/op	       0 allocs/op
BenchmarkLabels_Hash/extremely_large_label_value_10MB
BenchmarkLabels_Hash/extremely_large_label_value_10MB-12 	   10000	   1248546 ns/op	       0 B/op	       0 allocs/op
PASS

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
lbl-optimize
Bartlomiej Plotka 5 years ago
parent f226cb6138
commit eb27ddd3f2
  1. 1
      go.mod
  2. 2
      go.sum
  3. 31
      pkg/labels/labels.go
  4. 3
      vendor/github.com/cespare/xxhash/v2/.travis.yml
  5. 3
      vendor/modules.txt

@ -14,6 +14,7 @@ require (
github.com/armon/go-metrics v0.3.3 // indirect
github.com/aws/aws-sdk-go v1.34.9
github.com/cespare/xxhash v1.1.0
github.com/cespare/xxhash/v2 v2.1.2-0.20201007123935-e307e39619f5
github.com/containerd/containerd v1.3.4 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/dgryski/go-sip13 v0.0.0-20190329191031-25c5027a8c7b

@ -128,6 +128,8 @@ github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2-0.20201007123935-e307e39619f5 h1:XKJPW6WmkFsUzf+FaKhxI7XBVqqh5jeqNDE5uyWE4pk=
github.com/cespare/xxhash/v2 v2.1.2-0.20201007123935-e307e39619f5/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=

@ -18,9 +18,8 @@ import (
"encoding/json"
"sort"
"strconv"
"unsafe"
"github.com/cespare/xxhash"
"github.com/cespare/xxhash/v2"
)
// Well-known label names used by Prometheus components.
@ -135,39 +134,19 @@ func (ls Labels) MatchLabels(on bool, names ...string) Labels {
return matchedLabels
}
// noEscape hides a pointer from escape analysis. noEscape is
// the identity function but escape analysis doesn't think the
// output depends on the input. noEscape is inlined and currently
// compiles down to zero instructions.
// USE CAREFULLY!
// This was copied from the runtime; see issues 23382 and 7921.
//go:nosplit
//go:nocheckptr
func noEscape(p unsafe.Pointer) unsafe.Pointer {
x := uintptr(p)
return unsafe.Pointer(x ^ 0) //nolint:staticcheck
}
//go:nosplit
//go:checkptr
func noAllocBytes(buf string) []byte {
return *(*[]byte)(unsafe.Pointer(&buf))
}
// Hash returns a hash value for the label set.
func (ls Labels) Hash() uint64 {
// xxhash.Sum64(b) for fast path as it's faster.
b := make([]byte, 0, 1024)
for i, v := range ls {
if len(b)+len(v.Name)+len(v.Value)+2 >= cap(b) {
// If labels entry is 1KB+ allocate do not allocate whole entry.
h := xxhash.New()
// This allows b to be still on stack and reused. This is safe as xxhash.x
// is never used outside of this function.
_, _ = h.Write(*(*[]byte)(noEscape(unsafe.Pointer(&b))))
_, _ = h.Write(b)
for _, v := range ls[i:] {
_, _ = h.Write(noAllocBytes(v.Name))
_, _ = h.WriteString(v.Name)
_, _ = h.Write(seps)
_, _ = h.Write(noAllocBytes(v.Value))
_, _ = h.WriteString(v.Value)
_, _ = h.Write(seps)
}
return h.Sum64()

@ -1,4 +1,7 @@
language: go
arch:
- amd64
- ppc64le
go:
- "1.x"
- master

@ -91,7 +91,8 @@ github.com/beorn7/perks/quantile
# github.com/cespare/xxhash v1.1.0
## explicit
github.com/cespare/xxhash
# github.com/cespare/xxhash/v2 v2.1.1
# github.com/cespare/xxhash/v2 v2.1.2-0.20201007123935-e307e39619f5
## explicit
github.com/cespare/xxhash/v2
# github.com/containerd/containerd v1.3.4
## explicit

Loading…
Cancel
Save