Skip to content

Commit 7fc5f0d

Browse files
committed
chore(core): use unsafe.Slice instead of deprecated reflect.SliceHeader
1 parent 6929c8e commit 7fc5f0d

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-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: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,13 @@ func BenchmarkEncodeDecodeUint64Matrix(b *testing.B) {
263263
})
264264
}
265265

266-
func dotProductT[T c.Float](a, b []T, floatBits int) {
267-
var dotProduct T
266+
func dotProductT[T c.Float](a, b []T) {
267+
var product T
268268
if len(a) != len(b) {
269269
return
270270
}
271271
for i := range a {
272-
dotProduct += a[i] * b[i]
272+
product += a[i] * b[i]
273273
}
274274
}
275275

@@ -295,7 +295,7 @@ func BenchmarkDotProduct(b *testing.B) {
295295
b.Run(fmt.Sprintf("vek:size=%d", len(data)),
296296
func(b *testing.B) {
297297
temp := make([]float32, num)
298-
BytesAsFloatArray[float32](data, &temp, 32)
298+
BytesAsFloatArray(data, &temp, 32)
299299
for k := 0; k < b.N; k++ {
300300
vek32.Dot(temp, temp)
301301
}
@@ -305,7 +305,7 @@ func BenchmarkDotProduct(b *testing.B) {
305305
func(b *testing.B) {
306306

307307
temp := make([]float32, num)
308-
BytesAsFloatArray[float32](data, &temp, 32)
308+
BytesAsFloatArray(data, &temp, 32)
309309
for k := 0; k < b.N; k++ {
310310
dotProduct(temp, temp)
311311
}
@@ -316,9 +316,9 @@ func BenchmarkDotProduct(b *testing.B) {
316316
func(b *testing.B) {
317317

318318
temp := make([]float32, num)
319-
BytesAsFloatArray[float32](data, &temp, 32)
319+
BytesAsFloatArray(data, &temp, 32)
320320
for k := 0; k < b.N; k++ {
321-
dotProductT[float32](temp, temp, 32)
321+
dotProductT(temp, temp)
322322
}
323323
})
324324
}
@@ -379,7 +379,7 @@ func littleEndianBytesAsFloatArray[T c.Float](encoded []byte, retVal *[]T, float
379379
}
380380
}
381381

382-
func BenchmarkFloatConverstion(b *testing.B) {
382+
func BenchmarkFloatConversion(b *testing.B) {
383383
num := 1500
384384
data := make([]byte, 64*num)
385385
_, err := rand.Read(data)
@@ -391,23 +391,23 @@ func BenchmarkFloatConverstion(b *testing.B) {
391391
func(b *testing.B) {
392392
temp := make([]float32, num)
393393
for k := 0; k < b.N; k++ {
394-
BytesAsFloatArray[float32](data, &temp, 64)
394+
BytesAsFloatArray(data, &temp, 64)
395395
}
396396
})
397397

398398
b.Run(fmt.Sprintf("pointerFloat:size=%d", len(data)),
399399
func(b *testing.B) {
400400
temp := make([]float32, num)
401401
for k := 0; k < b.N; k++ {
402-
pointerFloatConversion[float32](data, &temp, 64)
402+
pointerFloatConversion(data, &temp, 64)
403403
}
404404
})
405405

406406
b.Run(fmt.Sprintf("littleEndianFloat:size=%d", len(data)),
407407
func(b *testing.B) {
408408
temp := make([]float32, num)
409409
for k := 0; k < b.N; k++ {
410-
littleEndianBytesAsFloatArray[float32](data, &temp, 64)
410+
littleEndianBytesAsFloatArray(data, &temp, 64)
411411
}
412412
})
413413
}

0 commit comments

Comments
 (0)