Skip to content

Commit 1689698

Browse files
committed
chore(core): use unsafe.Slice instead of deprecated reflect.SliceHeader
1 parent 4e881c0 commit 1689698

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

tok/index/helper.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ package index
88
import (
99
"encoding/binary"
1010
"math"
11-
"reflect"
1211
"unsafe"
1312

1413
c "github.com/dgraph-io/dgraph/v25/tok/constraints"
1514
"github.com/golang/glog"
1615
)
1716

18-
// BytesAsFloatArray[T c.Float](encoded) converts encoded into a []T,
17+
// BytesAsFloatArray converts encoded into a []T,
1918
// where T is either float32 or float64, depending on the value of floatBits.
2019
// Let floatBytes = floatBits/8. If len(encoded) % floatBytes is
2120
// not 0, it will ignore any trailing bytes, and simply convert floatBytes
@@ -40,10 +39,8 @@ func BytesAsFloatArray[T c.Float](encoded []byte, retVal *[]T, floatBits int) {
4039
*retVal = make([]T, len(encoded)/floatBytes)
4140
}
4241
*retVal = (*retVal)[:0]
43-
header := (*reflect.SliceHeader)(unsafe.Pointer(retVal))
44-
header.Data = uintptr(unsafe.Pointer(&encoded[0]))
45-
header.Len = len(encoded) / floatBytes
46-
header.Cap = len(encoded) / floatBytes
42+
floatSlice := unsafe.Slice((*T)(unsafe.Pointer(&encoded[0])), len(encoded)/floatBytes)
43+
*retVal = append(*retVal, floatSlice...)
4744
}
4845

4946
func BytesToFloat[T c.Float](encoded []byte, floatBits int) T {

tok/index/helper_test.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/dgraph-io/dgraph/v25/protos/pb"
1919
c "github.com/dgraph-io/dgraph/v25/tok/constraints"
20+
"github.com/stretchr/testify/require"
2021
"github.com/viterin/vek/vek32"
2122
"google.golang.org/protobuf/proto"
2223
)
@@ -263,13 +264,13 @@ func BenchmarkEncodeDecodeUint64Matrix(b *testing.B) {
263264
})
264265
}
265266

266-
func dotProductT[T c.Float](a, b []T, floatBits int) {
267-
var dotProduct T
267+
func dotProductT[T c.Float](a, b []T) {
268+
var product T
268269
if len(a) != len(b) {
269270
return
270271
}
271272
for i := range a {
272-
dotProduct += a[i] * b[i]
273+
product += a[i] * b[i]
273274
}
274275
}
275276

@@ -295,7 +296,7 @@ func BenchmarkDotProduct(b *testing.B) {
295296
b.Run(fmt.Sprintf("vek:size=%d", len(data)),
296297
func(b *testing.B) {
297298
temp := make([]float32, num)
298-
BytesAsFloatArray[float32](data, &temp, 32)
299+
BytesAsFloatArray(data, &temp, 32)
299300
for k := 0; k < b.N; k++ {
300301
vek32.Dot(temp, temp)
301302
}
@@ -305,7 +306,7 @@ func BenchmarkDotProduct(b *testing.B) {
305306
func(b *testing.B) {
306307

307308
temp := make([]float32, num)
308-
BytesAsFloatArray[float32](data, &temp, 32)
309+
BytesAsFloatArray(data, &temp, 32)
309310
for k := 0; k < b.N; k++ {
310311
dotProduct(temp, temp)
311312
}
@@ -316,9 +317,9 @@ func BenchmarkDotProduct(b *testing.B) {
316317
func(b *testing.B) {
317318

318319
temp := make([]float32, num)
319-
BytesAsFloatArray[float32](data, &temp, 32)
320+
BytesAsFloatArray(data, &temp, 32)
320321
for k := 0; k < b.N; k++ {
321-
dotProductT[float32](temp, temp, 32)
322+
dotProductT(temp, temp)
322323
}
323324
})
324325
}
@@ -379,7 +380,7 @@ func littleEndianBytesAsFloatArray[T c.Float](encoded []byte, retVal *[]T, float
379380
}
380381
}
381382

382-
func BenchmarkFloatConverstion(b *testing.B) {
383+
func BenchmarkFloatConversion(b *testing.B) {
383384
num := 1500
384385
data := make([]byte, 64*num)
385386
_, err := rand.Read(data)
@@ -391,23 +392,41 @@ func BenchmarkFloatConverstion(b *testing.B) {
391392
func(b *testing.B) {
392393
temp := make([]float32, num)
393394
for k := 0; k < b.N; k++ {
394-
BytesAsFloatArray[float32](data, &temp, 64)
395+
BytesAsFloatArray(data, &temp, 64)
395396
}
396397
})
397398

398399
b.Run(fmt.Sprintf("pointerFloat:size=%d", len(data)),
399400
func(b *testing.B) {
400401
temp := make([]float32, num)
401402
for k := 0; k < b.N; k++ {
402-
pointerFloatConversion[float32](data, &temp, 64)
403+
pointerFloatConversion(data, &temp, 64)
403404
}
404405
})
405406

406407
b.Run(fmt.Sprintf("littleEndianFloat:size=%d", len(data)),
407408
func(b *testing.B) {
408409
temp := make([]float32, num)
409410
for k := 0; k < b.N; k++ {
410-
littleEndianBytesAsFloatArray[float32](data, &temp, 64)
411+
littleEndianBytesAsFloatArray(data, &temp, 64)
411412
}
412413
})
413414
}
415+
416+
func TestBytesAsFloatArray(t *testing.T) {
417+
var out32 []float32
418+
in32 := []float32{0.1, 1.123456789, 50000.00005}
419+
420+
inf32 := unsafe.Slice((*byte)(unsafe.Pointer(&in32[0])), len(in32)*int(unsafe.Sizeof(in32[0])))
421+
422+
BytesAsFloatArray(inf32, &out32, 32)
423+
require.Equal(t, in32, out32)
424+
425+
var out64 []float64
426+
in64 := []float64{0.1, 1.123456789, 50000.00005}
427+
428+
inf64 := unsafe.Slice((*byte)(unsafe.Pointer(&in64[0])), len(in64)*int(unsafe.Sizeof(in64[0])))
429+
430+
BytesAsFloatArray(inf64, &out64, 64)
431+
require.Equal(t, in64, out64)
432+
}

0 commit comments

Comments
 (0)