Skip to content

Commit

Permalink
Neater string vs byte-slice conversions (prometheus#14425)
Browse files Browse the repository at this point in the history
unsafe.Slice and unsafe.StringData were added in Go 1.20

Signed-off-by: Bryan Boreham <[email protected]>
  • Loading branch information
bboreham authored Sep 21, 2024
1 parent 6bcb064 commit 31c5760
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion model/labels/labels_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,5 @@ func contains(s []Label, n string) bool {
}

func yoloString(b []byte) string {
return *((*string)(unsafe.Pointer(&b)))
return unsafe.String(unsafe.SliceData(b), len(b))
}
11 changes: 4 additions & 7 deletions model/labels/labels_stringlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package labels

import (
"reflect"
"slices"
"strings"
"unsafe"
Expand Down Expand Up @@ -299,10 +298,8 @@ func Equal(ls, o Labels) bool {
func EmptyLabels() Labels {
return Labels{}
}
func yoloBytes(s string) (b []byte) {
*(*string)(unsafe.Pointer(&b)) = s
(*reflect.SliceHeader)(unsafe.Pointer(&b)).Cap = len(s)
return
func yoloBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}

// New returns a sorted Labels from the given labels.
Expand Down Expand Up @@ -338,8 +335,8 @@ func Compare(a, b Labels) int {
}
i := 0
// 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)
lp := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&longer)).Data)
sp := unsafe.Pointer(unsafe.StringData(shorter))
lp := unsafe.Pointer(unsafe.StringData(longer))
for ; i < len(shorter)-8; i += 8 {
if *(*uint64)(unsafe.Add(sp, i)) != *(*uint64)(unsafe.Add(lp, i)) {
break
Expand Down
2 changes: 1 addition & 1 deletion model/textparse/promparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ func unreplace(s string) 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) {
Expand Down
4 changes: 1 addition & 3 deletions tsdb/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"hash"
"hash/crc32"
"math"
"unsafe"

"github.com/dennwc/varint"
)
Expand Down Expand Up @@ -75,8 +74,7 @@ func (e *Encbuf) PutVarint64(x int64) {

// PutUvarintStr writes a string to the buffer prefixed by its varint length (in bytes!).
func (e *Encbuf) PutUvarintStr(s string) {
b := *(*[]byte)(unsafe.Pointer(&s))
e.PutUvarint(len(b))
e.PutUvarint(len(s))
e.PutString(s)
}

Expand Down
2 changes: 1 addition & 1 deletion tsdb/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2063,5 +2063,5 @@ func (dec *Decoder) Series(b []byte, builder *labels.ScratchBuilder, chks *[]chu
}

func yoloString(b []byte) string {
return *((*string)(unsafe.Pointer(&b)))
return unsafe.String(unsafe.SliceData(b), len(b))
}

0 comments on commit 31c5760

Please sign in to comment.