lib/storage: use unsafe.Slice instead of deprecated reflect.SliceHeader

This commit is contained in:
Aliaksandr Valialkin 2024-02-29 17:24:34 +02:00
parent a9fb2e91a6
commit 22acd84019
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"io" "io"
"path/filepath" "path/filepath"
"reflect"
"sort" "sort"
"strconv" "strconv"
"sync" "sync"
@ -415,14 +414,7 @@ func marshalMetricIDs(dst []byte, metricIDs []uint64) []byte {
// Compress metricIDs, so they occupy less space in the cache. // Compress metricIDs, so they occupy less space in the cache.
// //
// The srcBuf is a []byte cast of metricIDs. // The srcBuf is a []byte cast of metricIDs.
var srcBuf []byte srcBuf := unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(metricIDs))), 8*len(metricIDs))
if len(metricIDs) > 0 {
sh := (*reflect.SliceHeader)(unsafe.Pointer(&srcBuf))
sh.Data = uintptr(unsafe.Pointer(&metricIDs[0]))
sh.Cap = 8 * len(metricIDs)
sh.Len = 8 * len(metricIDs)
}
dst = encoding.CompressZSTDLevel(dst, srcBuf, 1) dst = encoding.CompressZSTDLevel(dst, srcBuf, 1)
return dst return dst
} }
@ -431,13 +423,8 @@ func mustUnmarshalMetricIDs(dst []uint64, src []byte) []uint64 {
// Decompress src into dstBuf. // Decompress src into dstBuf.
// //
// dstBuf is a []byte cast of dst. // dstBuf is a []byte cast of dst.
var dstBuf []byte dstBuf := unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(dst))), 8*cap(dst))
if len(dst) > 0 { dstBuf = dstBuf[:8*len(dst)]
sh := (*reflect.SliceHeader)(unsafe.Pointer(&dstBuf))
sh.Data = uintptr(unsafe.Pointer(&dst[0]))
sh.Cap = 8 * cap(dst)
sh.Len = 8 * len(dst)
}
dstBufLen := len(dstBuf) dstBufLen := len(dstBuf)
var err error var err error
dstBuf, err = encoding.DecompressZSTD(dstBuf, src) dstBuf, err = encoding.DecompressZSTD(dstBuf, src)
@ -453,10 +440,8 @@ func mustUnmarshalMetricIDs(dst []uint64, src []byte) []uint64 {
} }
// Convert dstBuf back to dst // Convert dstBuf back to dst
sh := (*reflect.SliceHeader)(unsafe.Pointer(&dst)) dst = unsafe.Slice((*uint64)(unsafe.Pointer(unsafe.SliceData(dstBuf))), cap(dstBuf)/8)
sh.Data = uintptr(unsafe.Pointer(&dstBuf[0])) dst = dst[:len(dstBuf)/8]
sh.Cap = cap(dstBuf) / 8
sh.Len = len(dstBuf) / 8
return dst return dst
} }