Skip to content

Commit

Permalink
Migrate off deprecated unsafe API
Browse files Browse the repository at this point in the history
Signed-off-by: Arve Knudsen <[email protected]>
  • Loading branch information
aknuds1 committed Apr 12, 2024
1 parent f9ea6a5 commit 0b8160c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
5 changes: 1 addition & 4 deletions pkg/mimirpb/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package mimirpb
import (
stdlibjson "encoding/json"
"math"
"reflect"
"strconv"
"testing"
"unsafe"
Expand Down Expand Up @@ -197,9 +196,7 @@ func TestFromLabelAdaptersToLabelsWithCopy(t *testing.T) {

// All strings must be copied.
actualValue := actual.Get("hello")
hInputValue := (*reflect.StringHeader)(unsafe.Pointer(&input[0].Value))
hActualValue := (*reflect.StringHeader)(unsafe.Pointer(&actualValue))
assert.NotEqual(t, hInputValue.Data, hActualValue.Data)
assert.NotSame(t, unsafe.StringData(input[0].Value), unsafe.StringData(actualValue))
}

func BenchmarkFromLabelAdaptersToLabelsWithCopy(b *testing.B) {
Expand Down
19 changes: 9 additions & 10 deletions pkg/mimirpb/timeseries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,31 @@ func TestTimeseriesFromPool(t *testing.T) {
}

func TestCopyToYoloString(t *testing.T) {
stringByteArray := func(val string) uintptr {
return (*reflect.SliceHeader)(unsafe.Pointer(&val)).Data
}

testString := yoloString([]byte("testString"))
testStringByteArray := stringByteArray(testString)
testStringByteArray := unsafe.StringData(testString)

// Verify that the unsafe copy is unsafe.
unsafeCopy := testString
unsafeCopyByteArray := stringByteArray(unsafeCopy)
assert.Equal(t, testStringByteArray, unsafeCopyByteArray)
unsafeCopyByteArray := unsafe.StringData(unsafeCopy)
assert.Same(t, testStringByteArray, unsafeCopyByteArray)

// Create a safe copy by using the newBuf byte slice.
newBuf := make([]byte, 0, len(testString))
safeCopy, remainingBuf := copyToYoloString(newBuf, unsafeCopy)

// Verify that the safe copy is safe by checking that the underlying byte arrays are different.
safeCopyByteArray := stringByteArray(safeCopy)
assert.NotEqual(t, testStringByteArray, safeCopyByteArray)
safeCopyByteArray := unsafe.StringData(safeCopy)
assert.NotSame(t, testStringByteArray, safeCopyByteArray)

// Verify that the remainingBuf has been used up completely.
assert.Len(t, remainingBuf, 0)

// Verify that the remainingBuf is using the same underlying byte array as safeCopy but advanced by the length.
remainingBufArrayData := unsafe.SliceData(remainingBuf)
remainingBufArray := (*reflect.SliceHeader)(unsafe.Pointer(&remainingBuf)).Data
assert.Equal(t, int(safeCopyByteArray)+len(newBuf), int(remainingBufArray))
t.Logf("remainingBufArray should be at %d, is at %d", remainingBufArray, int(uintptr(unsafe.Pointer(&safeCopy)))+len(newBuf))
//assert.Equal(t, int(safeCopyByteArray)+len(newBuf), int(remainingBufArray))
assert.Equal(t, uintptr(unsafe.Add(unsafe.Pointer(&safeCopy), len(newBuf))), uintptr(unsafe.Pointer(remainingBufArrayData)))
}

func TestDeepCopyTimeseries(t *testing.T) {
Expand Down

0 comments on commit 0b8160c

Please sign in to comment.