mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Neater string vs byte-slice conversions (#14425)
Some checks failed
buf.build / lint and publish (push) Has been cancelled
CI / Go tests (push) Has been cancelled
CI / More Go tests (push) Has been cancelled
CI / Go tests with previous Go version (push) Has been cancelled
CI / UI tests (push) Has been cancelled
CI / Go tests on Windows (push) Has been cancelled
CI / Mixins tests (push) Has been cancelled
CI / Build Prometheus for common architectures (0) (push) Has been cancelled
CI / Build Prometheus for common architectures (1) (push) Has been cancelled
CI / Build Prometheus for common architectures (2) (push) Has been cancelled
CI / Build Prometheus for all architectures (0) (push) Has been cancelled
CI / Build Prometheus for all architectures (1) (push) Has been cancelled
CI / Build Prometheus for all architectures (10) (push) Has been cancelled
CI / Build Prometheus for all architectures (11) (push) Has been cancelled
CI / Build Prometheus for all architectures (2) (push) Has been cancelled
CI / Build Prometheus for all architectures (3) (push) Has been cancelled
CI / Build Prometheus for all architectures (4) (push) Has been cancelled
CI / Build Prometheus for all architectures (5) (push) Has been cancelled
CI / Build Prometheus for all architectures (6) (push) Has been cancelled
CI / Build Prometheus for all architectures (7) (push) Has been cancelled
CI / Build Prometheus for all architectures (8) (push) Has been cancelled
CI / Build Prometheus for all architectures (9) (push) Has been cancelled
CI / Check generated parser (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
CI / fuzzing (push) Has been cancelled
CI / codeql (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
CI / Report status of build Prometheus for all architectures (push) Has been cancelled
CI / Publish main branch artifacts (push) Has been cancelled
CI / Publish release artefacts (push) Has been cancelled
CI / Publish UI on npm Registry (push) Has been cancelled
Some checks failed
buf.build / lint and publish (push) Has been cancelled
CI / Go tests (push) Has been cancelled
CI / More Go tests (push) Has been cancelled
CI / Go tests with previous Go version (push) Has been cancelled
CI / UI tests (push) Has been cancelled
CI / Go tests on Windows (push) Has been cancelled
CI / Mixins tests (push) Has been cancelled
CI / Build Prometheus for common architectures (0) (push) Has been cancelled
CI / Build Prometheus for common architectures (1) (push) Has been cancelled
CI / Build Prometheus for common architectures (2) (push) Has been cancelled
CI / Build Prometheus for all architectures (0) (push) Has been cancelled
CI / Build Prometheus for all architectures (1) (push) Has been cancelled
CI / Build Prometheus for all architectures (10) (push) Has been cancelled
CI / Build Prometheus for all architectures (11) (push) Has been cancelled
CI / Build Prometheus for all architectures (2) (push) Has been cancelled
CI / Build Prometheus for all architectures (3) (push) Has been cancelled
CI / Build Prometheus for all architectures (4) (push) Has been cancelled
CI / Build Prometheus for all architectures (5) (push) Has been cancelled
CI / Build Prometheus for all architectures (6) (push) Has been cancelled
CI / Build Prometheus for all architectures (7) (push) Has been cancelled
CI / Build Prometheus for all architectures (8) (push) Has been cancelled
CI / Build Prometheus for all architectures (9) (push) Has been cancelled
CI / Check generated parser (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
CI / fuzzing (push) Has been cancelled
CI / codeql (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
CI / Report status of build Prometheus for all architectures (push) Has been cancelled
CI / Publish main branch artifacts (push) Has been cancelled
CI / Publish release artefacts (push) Has been cancelled
CI / Publish UI on npm Registry (push) Has been cancelled
unsafe.Slice and unsafe.StringData were added in Go 1.20 Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
parent
6bcb064d93
commit
31c5760551
|
@ -230,5 +230,5 @@ func contains(s []Label, n string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func yoloString(b []byte) string {
|
func yoloString(b []byte) string {
|
||||||
return *((*string)(unsafe.Pointer(&b)))
|
return unsafe.String(unsafe.SliceData(b), len(b))
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
package labels
|
package labels
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
@ -299,10 +298,8 @@ func Equal(ls, o Labels) bool {
|
||||||
func EmptyLabels() Labels {
|
func EmptyLabels() Labels {
|
||||||
return Labels{}
|
return Labels{}
|
||||||
}
|
}
|
||||||
func yoloBytes(s string) (b []byte) {
|
func yoloBytes(s string) []byte {
|
||||||
*(*string)(unsafe.Pointer(&b)) = s
|
return unsafe.Slice(unsafe.StringData(s), len(s))
|
||||||
(*reflect.SliceHeader)(unsafe.Pointer(&b)).Cap = len(s)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a sorted Labels from the given labels.
|
// New returns a sorted Labels from the given labels.
|
||||||
|
@ -338,8 +335,8 @@ func Compare(a, b Labels) int {
|
||||||
}
|
}
|
||||||
i := 0
|
i := 0
|
||||||
// First, go 8 bytes at a time. Data strings are expected to be 8-byte aligned.
|
// First, go 8 bytes at a time. Data strings are expected to be 8-byte aligned.
|
||||||
sp := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&shorter)).Data)
|
sp := unsafe.Pointer(unsafe.StringData(shorter))
|
||||||
lp := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&longer)).Data)
|
lp := unsafe.Pointer(unsafe.StringData(longer))
|
||||||
for ; i < len(shorter)-8; i += 8 {
|
for ; i < len(shorter)-8; i += 8 {
|
||||||
if *(*uint64)(unsafe.Add(sp, i)) != *(*uint64)(unsafe.Add(lp, i)) {
|
if *(*uint64)(unsafe.Add(sp, i)) != *(*uint64)(unsafe.Add(lp, i)) {
|
||||||
break
|
break
|
||||||
|
|
|
@ -502,7 +502,7 @@ func unreplace(s string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func yoloString(b []byte) string {
|
func yoloString(b []byte) string {
|
||||||
return *((*string)(unsafe.Pointer(&b)))
|
return unsafe.String(unsafe.SliceData(b), len(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFloat(s string) (float64, error) {
|
func parseFloat(s string) (float64, error) {
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"hash"
|
"hash"
|
||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
"math"
|
"math"
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/dennwc/varint"
|
"github.com/dennwc/varint"
|
||||||
)
|
)
|
||||||
|
@ -75,8 +74,7 @@ func (e *Encbuf) PutVarint64(x int64) {
|
||||||
|
|
||||||
// PutUvarintStr writes a string to the buffer prefixed by its varint length (in bytes!).
|
// PutUvarintStr writes a string to the buffer prefixed by its varint length (in bytes!).
|
||||||
func (e *Encbuf) PutUvarintStr(s string) {
|
func (e *Encbuf) PutUvarintStr(s string) {
|
||||||
b := *(*[]byte)(unsafe.Pointer(&s))
|
e.PutUvarint(len(s))
|
||||||
e.PutUvarint(len(b))
|
|
||||||
e.PutString(s)
|
e.PutString(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2063,5 +2063,5 @@ func (dec *Decoder) Series(b []byte, builder *labels.ScratchBuilder, chks *[]chu
|
||||||
}
|
}
|
||||||
|
|
||||||
func yoloString(b []byte) string {
|
func yoloString(b []byte) string {
|
||||||
return *((*string)(unsafe.Pointer(&b)))
|
return unsafe.String(unsafe.SliceData(b), len(b))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue