Skip to content

Commit 2e9208d

Browse files
committed
manual: remove MaxArrayLen
The hack of casting to an intermediary array is no longer necessary since the addition of `unsafe.Slice` in go 1.17.
1 parent fca2fd5 commit 2e9208d

File tree

6 files changed

+14
-70
lines changed

6 files changed

+14
-70
lines changed

internal/manual/manual_32bit.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

internal/manual/manual_64bit.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

internal/manual/manual_cgo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ func New(purpose Purpose, n int) []byte {
4545
// it cannot allocate memory.
4646
throw("out of memory")
4747
}
48-
// Interpret the C pointer as a pointer to a Go array, then slice.
49-
return (*[MaxArrayLen]byte)(unsafe.Pointer(ptr))[:n:n]
48+
return unsafe.Slice((*byte)(ptr), n)
5049
}
5150

5251
// Free frees the specified slice. It has to be exactly the slice that was

internal/manual/manual_mipsall.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

sstable/rowblk/rowblk_iter.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/cockroachdb/errors"
1717
"github.com/cockroachdb/pebble/internal/base"
1818
"github.com/cockroachdb/pebble/internal/invariants"
19-
"github.com/cockroachdb/pebble/internal/manual"
2019
"github.com/cockroachdb/pebble/internal/treeprinter"
2120
"github.com/cockroachdb/pebble/sstable/block"
2221
)
@@ -382,7 +381,7 @@ func (i *Iter) readEntry() {
382381
ptr = unsafe.Pointer(uintptr(ptr) + 5)
383382
}
384383
shared += i.transforms.SyntheticPrefixAndSuffix.PrefixLen()
385-
unsharedKey := getBytes(ptr, int(unshared))
384+
unsharedKey := unsafe.Slice((*byte)(ptr), int(unshared))
386385
// TODO(sumeer): move this into the else block below.
387386
i.fullKey = append(i.fullKey[:shared], unsharedKey...)
388387
if shared == 0 {
@@ -395,7 +394,7 @@ func (i *Iter) readEntry() {
395394
i.key = i.fullKey
396395
}
397396
ptr = unsafe.Pointer(uintptr(ptr) + uintptr(unshared))
398-
i.val = getBytes(ptr, int(value))
397+
i.val = unsafe.Slice((*byte)(ptr), int(value))
399398
i.nextOffset = int32(uintptr(ptr)-uintptr(i.ptr)) + int32(value)
400399
}
401400

@@ -449,7 +448,7 @@ func (i *Iter) readFirstKey() error {
449448
ptr = unsafe.Pointer(uintptr(ptr) + 5)
450449
}
451450

452-
firstKey := getBytes(ptr, int(unshared))
451+
firstKey := unsafe.Slice((*byte)(ptr), int(unshared))
453452
// Manually inlining base.DecodeInternalKey provides a 5-10% speedup on
454453
// BlockIter benchmarks.
455454
if n := len(firstKey) - 8; n >= 0 {
@@ -611,7 +610,7 @@ func (i *Iter) SeekGE(key []byte, flags base.SeekGEFlags) *base.InternalKV {
611610

612611
// Manually inlining part of base.DecodeInternalKey provides a 5-10%
613612
// speedup on BlockIter benchmarks.
614-
s := getBytes(ptr, int(v1))
613+
s := unsafe.Slice((*byte)(ptr), int(v1))
615614
var k []byte
616615
if n := len(s) - 8; n >= 0 {
617616
k = s[:n:n]
@@ -793,7 +792,7 @@ func (i *Iter) SeekLT(key []byte, flags base.SeekLTFlags) *base.InternalKV {
793792

794793
// Manually inlining part of base.DecodeInternalKey provides a 5-10%
795794
// speedup on BlockIter benchmarks.
796-
s := getBytes(ptr, int(v1))
795+
s := unsafe.Slice((*byte)(ptr), int(v1))
797796
var k []byte
798797
if n := len(s) - 8; n >= 0 {
799798
k = s[:n:n]
@@ -1240,7 +1239,7 @@ func (i *Iter) nextPrefixV3(succKey []byte) *base.InternalKV {
12401239
}
12411240
// The trailer is written in little endian, so the key kind is the first
12421241
// byte in the trailer that is encoded in the slice [unshared-8:unshared].
1243-
keyKind := base.InternalKeyKind((*[manual.MaxArrayLen]byte)(ptr)[unshared-8])
1242+
keyKind := base.InternalKeyKind(*(*byte)(unsafe.Pointer(uintptr(ptr) + uintptr(unshared) - 8)))
12441243
keyKind = keyKind & base.InternalKeyKindSSTableInternalObsoleteMask
12451244
prefixChanged := false
12461245
if keyKind == base.InternalKeyKindSet {
@@ -1334,7 +1333,7 @@ func (i *Iter) nextPrefixV3(succKey []byte) *base.InternalKV {
13341333
// - (Unlikely) The prefix has not changed.
13351334
// We assemble the key etc. under the assumption that it is the likely
13361335
// case.
1337-
unsharedKey := getBytes(ptr, int(unshared))
1336+
unsharedKey := unsafe.Slice((*byte)(ptr), int(unshared))
13381337
// TODO(sumeer): move this into the else block below. This is a bit tricky
13391338
// since the current logic assumes we have always copied the latest key
13401339
// into fullKey, which is why when we get to the next key we can (a)
@@ -1346,7 +1345,7 @@ func (i *Iter) nextPrefixV3(succKey []byte) *base.InternalKV {
13461345
// too. This same comment applies to the other place where we can do this
13471346
// optimization, in readEntry().
13481347
i.fullKey = append(i.fullKey[:shared], unsharedKey...)
1349-
i.val = getBytes(valuePtr, int(value))
1348+
i.val = unsafe.Slice((*byte)(valuePtr), int(value))
13501349
if shared == 0 {
13511350
// Provide stability for the key across positioning calls if the key
13521351
// doesn't share a prefix with the previous key. This removes requiring the
@@ -1419,7 +1418,7 @@ start:
14191418
i.nextOffset = i.offset
14201419
e := &i.cached[n]
14211420
i.offset = e.offset
1422-
i.val = getBytes(unsafe.Pointer(uintptr(i.ptr)+uintptr(e.valStart)), int(e.valSize))
1421+
i.val = unsafe.Slice((*byte)(unsafe.Pointer(uintptr(i.ptr)+uintptr(e.valStart))), int(e.valSize))
14231422
// Manually inlined version of i.decodeInternalKey(i.key).
14241423
i.key = i.cachedBuf[e.keyStart:e.keyEnd]
14251424
if n := len(i.key) - 8; n >= 0 {
@@ -1713,10 +1712,10 @@ func (i *RawIter) readEntry() {
17131712
shared, ptr := decodeVarint(ptr)
17141713
unshared, ptr := decodeVarint(ptr)
17151714
value, ptr := decodeVarint(ptr)
1716-
i.key = append(i.key[:shared], getBytes(ptr, int(unshared))...)
1715+
i.key = append(i.key[:shared], unsafe.Slice((*byte)(ptr), int(unshared))...)
17171716
i.key = i.key[:len(i.key):len(i.key)]
17181717
ptr = unsafe.Pointer(uintptr(ptr) + uintptr(unshared))
1719-
i.val = getBytes(ptr, int(value))
1718+
i.val = unsafe.Slice((*byte)(ptr), int(value))
17201719
i.nextOffset = int32(uintptr(ptr)-uintptr(i.ptr)) + int32(value)
17211720
}
17221721

@@ -1761,7 +1760,7 @@ func (i *RawIter) SeekGE(key []byte) bool {
17611760
// Decode the key at that restart point, and compare it to the key sought.
17621761
v1, ptr := decodeVarint(ptr)
17631762
_, ptr = decodeVarint(ptr)
1764-
s := getBytes(ptr, int(v1))
1763+
s := unsafe.Slice((*byte)(ptr), int(v1))
17651764
return i.cmp(key, s) < 0
17661765
})
17671766

@@ -1828,7 +1827,7 @@ func (i *RawIter) Prev() bool {
18281827
i.nextOffset = i.offset
18291828
e := &i.cached[n-1]
18301829
i.offset = e.offset
1831-
i.val = getBytes(unsafe.Pointer(uintptr(i.ptr)+uintptr(e.valStart)), int(e.valSize))
1830+
i.val = unsafe.Slice((*byte)(unsafe.Pointer(uintptr(i.ptr)+uintptr(e.valStart))), int(e.valSize))
18321831
i.ikey.UserKey = i.cachedBuf[e.keyStart:e.keyEnd]
18331832
i.cached = i.cached[:n]
18341833
return true
@@ -1939,10 +1938,6 @@ func (i *RawIter) Describe(tp treeprinter.Node, fmtKV DescribeKV) {
19391938
}
19401939
}
19411940

1942-
func getBytes(ptr unsafe.Pointer, length int) []byte {
1943-
return (*[manual.MaxArrayLen]byte)(ptr)[:length:length]
1944-
}
1945-
19461941
func decodeVarint(ptr unsafe.Pointer) (uint32, unsafe.Pointer) {
19471942
if a := *((*uint8)(ptr)); a < 128 {
19481943
return uint32(a),

sstable/rowblk/unsafe_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,8 @@ import (
1414
"unsafe"
1515

1616
"github.com/cockroachdb/crlib/testutils/leaktest"
17-
"github.com/stretchr/testify/require"
1817
)
1918

20-
func TestGetBytes(t *testing.T) {
21-
defer leaktest.AfterTest(t)()
22-
const size = (1 << 31) - 1
23-
// No need to actually allocate a huge slice, which can cause OOM on small
24-
// machines (like the GitHub CI runners).
25-
block := make([]byte, 100)
26-
data := getBytes(unsafe.Pointer(&block[0]), size)
27-
require.EqualValues(t, size, len(data))
28-
}
29-
3019
func TestDecodeVarint(t *testing.T) {
3120
defer leaktest.AfterTest(t)()
3221
vals := []uint32{

0 commit comments

Comments
 (0)