Skip to content

Commit 3a0250e

Browse files
BradLugohdonnay
authored andcommitted
ctxlock: use safe implementation of keyify
The benchmarks for the unsafe implementation of keyify don't show a significant improvement over the safe implementation. These changes also fix a bug where keyify panics when an empty string input is received. Signed-off-by: Brad Lugo <[email protected]>
1 parent 06429a8 commit 3a0250e

File tree

2 files changed

+1
-27
lines changed

2 files changed

+1
-27
lines changed

pkg/ctxlock/keyify.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
//go:build !safe
2-
31
package ctxlock
42

53
import (
64
"hash/fnv"
7-
"reflect"
8-
"unsafe"
95
)
106

117
// Keyify returns an int64 serialized into a []byte.
128
func keyify(key string) []byte {
13-
const maxsize = 0x7fff0000
14-
l := len(key)
159
h := fnv.New64a()
16-
// This is (obviously) unsafe -- it provides mutable access to "key".
17-
// However, it doesn't outlive this Write call, and the implementation
18-
// can be read to ensure it doesn't modify it.
19-
h.Write((*[maxsize]byte)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data))[:l:l])
10+
h.Write([]byte(key))
2011
b := make([]byte, 0, 8)
2112
return h.Sum(b)
2213
}

pkg/ctxlock/keyify_safe.go

-17
This file was deleted.

0 commit comments

Comments
 (0)