lib/mergeset: use unsafe.Slice and unsafe.String instead of deprecated reflect.SliceHeader with unsafe conversion from slice header to string header

This commit is contained in:
Aliaksandr Valialkin 2024-02-29 17:29:33 +02:00
parent 38e0397ebd
commit 6a8dc74ee7
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"os"
"reflect"
"sort"
"strings"
"sync"
@ -29,21 +28,15 @@ type Item struct {
// The returned bytes representation belongs to data.
func (it Item) Bytes(data []byte) []byte {
n := int(it.End - it.Start)
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
sh.Cap = n
sh.Len = n
sh.Data += uintptr(it.Start)
return data
return unsafe.Slice((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(data)))+uintptr(it.Start))), n)
}
// String returns string representation of it obtained from data.
//
// The returned string representation belongs to data.
func (it Item) String(data []byte) string {
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
sh.Data += uintptr(it.Start)
sh.Len = int(it.End - it.Start)
return *(*string)(unsafe.Pointer(sh))
n := int(it.End - it.Start)
return unsafe.String((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(data)))+uintptr(it.Start))), n)
}
func (ib *inmemoryBlock) Len() int {