Skip to content

Commit c21fd8d

Browse files
committed
colblk: remove unnecessary start ptr from RawBytes
Remove an unnecessary 'start' pointer from RawBytes. It was only used when formatting the structure. This allows us to reduce to the size of the block.Metadata by 24 bytes. Additionally, we add test assertions on 64-bit platforms that ensure we don't leave MetadataSize larger than is necessary. MetadataSize is allocated per block in the block cache (~ on the order of hundreds of thousands), so its overhead can be material.
1 parent 635423e commit c21fd8d

File tree

12 files changed

+63
-29
lines changed

12 files changed

+63
-29
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use
2+
// of this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
5+
//go:build arm64 || amd64
6+
7+
package cockroachkvs
8+
9+
import (
10+
"unsafe"
11+
12+
"github.com/cockroachdb/pebble/sstable/colblk"
13+
)
14+
15+
// Assert that KeySeekerMetadata isn't larger than it needs to be to fit a
16+
// cockroachKeySeeker on a 64-bit platform.
17+
var _ uint = uint(unsafe.Sizeof(cockroachKeySeeker{})) - colblk.KeySeekerMetadataSize

sstable/block/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func CastMetadata[T any](md *Metadata) *T {
257257

258258
// MetadataSize is the size of the metadata. The value is chosen to fit a
259259
// colblk.DataBlockDecoder and a CockroachDB colblk.KeySeeker.
260-
const MetadataSize = 336
260+
const MetadataSize = 312
261261

262262
// Assert that MetadataSize is a multiple of 8. This is necessary to keep the
263263
// block data buffer aligned.

sstable/block/testdata/flush_governor

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ should not flush
2929
init target-block-size=800 size-class-aware-threshold=60 size-classes=(820, 1020, 1320, 1820)
3030
----
3131
low watermark: 480
32-
high watermark: 952
33-
targetBoundary: 652
32+
high watermark: 976
33+
targetBoundary: 676
3434

3535
# Should not flush when the "after" block fits in the same size class.
3636
should-flush size-before=600 size-after=650
@@ -51,7 +51,7 @@ should not flush
5151

5252
should-flush size-before=600 size-after=960
5353
----
54-
should flush
54+
should not flush
5555

5656
# Should flush when the after size is above the high watermark.
5757
should-flush size-before=600 size-after=1500
@@ -82,8 +82,8 @@ targetBoundary: 1000
8282
init target-block-size=32768 jemalloc-size-classes
8383
----
8484
low watermark: 19661
85-
high watermark: 40592
86-
targetBoundary: 32400
85+
high watermark: 40616
86+
targetBoundary: 32424
8787

8888
# We should not flush until exceeding the boundary.
8989
should-flush size-before=30000 size-after=31000
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use
2+
// of this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
5+
//go:build arm64 || amd64
6+
7+
package colblk
8+
9+
import "github.com/cockroachdb/pebble/sstable/block"
10+
11+
// Assert that block.MetadataSize is not larger than it needs to be on a 64-bit
12+
// platform.
13+
const _ uint = uint(dataBlockDecoderSize) + KeySeekerMetadataSize - block.MetadataSize

sstable/colblk/data_block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type KeySeekerMetadata [KeySeekerMetadataSize]byte
6666

6767
// KeySeekerMetadataSize is chosen to fit the CockroachDB key seeker
6868
// implementation.
69-
const KeySeekerMetadataSize = 176
69+
const KeySeekerMetadataSize = 160
7070

7171
// A KeyWriter maintains ColumnWriters for a data block for writing user keys
7272
// into the database-specific key schema. Users may define their own key schema

sstable/colblk/prefix_bytes.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,13 +615,16 @@ func prefixBytesToBinFormatter(
615615
if sliceFormatter == nil {
616616
sliceFormatter = defaultSliceFormatter
617617
}
618-
pb, _ := DecodePrefixBytes(f.RelativeData(), uint32(f.RelativeOffset()), count)
618+
data := f.RelativeData()
619+
off := f.RelativeOffset()
620+
start := unsafe.Pointer(&data[off])
621+
pb, _ := DecodePrefixBytes(data, uint32(off), count)
619622

620623
f.HexBytesln(1, "bundle size: %d", 1<<pb.bundleShift)
621624
f.ToTreePrinter(tp)
622625

623626
n := tp.Child("offsets table")
624-
dataOffset := uint64(f.RelativeOffset()) + uint64(uintptr(pb.rawBytes.data)-uintptr(pb.rawBytes.start))
627+
dataOffset := uint64(off) + uint64(uintptr(pb.rawBytes.data)-uintptr(start))
625628
uintsToBinFormatter(f, n, pb.rawBytes.slices+1, func(offsetDelta, offsetBase uint64) string {
626629
// NB: offsetBase will always be zero for PrefixBytes columns.
627630
return fmt.Sprintf("%d [%d overall]", offsetDelta+offsetBase, offsetDelta+offsetBase+dataOffset)

sstable/colblk/raw_bytes.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import (
4949
type RawBytes struct {
5050
slices int
5151
offsets UnsafeOffsets
52-
start unsafe.Pointer
5352
data unsafe.Pointer
5453
}
5554

@@ -67,7 +66,6 @@ func DecodeRawBytes(b []byte, offset uint32, count int) (rawBytes RawBytes, endO
6766
return RawBytes{
6867
slices: count,
6968
offsets: offsets,
70-
start: unsafe.Pointer(&b[offset]),
7169
data: unsafe.Pointer(&b[dataOff]),
7270
}, dataOff + offsets.At(count)
7371
}
@@ -92,8 +90,11 @@ func rawBytesToBinFormatter(
9290
sliceFormatter = defaultSliceFormatter
9391
}
9492

95-
rb, _ := DecodeRawBytes(f.RelativeData(), uint32(f.RelativeOffset()), count)
96-
dataOffset := uint64(f.RelativeOffset()) + uint64(uintptr(rb.data)-uintptr(rb.start))
93+
data := f.RelativeData()
94+
off := f.RelativeOffset()
95+
start := unsafe.Pointer(&data[off])
96+
rb, _ := DecodeRawBytes(data, uint32(off), count)
97+
dataOffset := uint64(off) + uint64(uintptr(rb.data)-uintptr(start))
9798
n := tp.Child("offsets table")
9899
uintsToBinFormatter(f, n, count+1, func(offset, base uint64) string {
99100
// NB: base is always zero for RawBytes columns.

sstable/colblk/unsafe_uints.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
//
2020
// The At() method is defined in endian_little.go and endian_big.go.
2121
type UnsafeUints struct {
22-
base uint64
2322
ptr unsafe.Pointer
23+
base uint64
2424
width uint8
2525
}
2626

testdata/compaction/value_separation

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ ITERATORS
155155
block cache | file cache | filter | sst iters | snapshots
156156
entries | hit rate | entries | hit rate | util | open | open
157157
-------------+-------------+--------------+-------------+--------------+-------------+------------
158-
5 (1.9KB) | 81.8% | 1 (376B) | 89.2% | 0.0% | 0 | 0
158+
5 (1.8KB) | 81.8% | 1 (376B) | 89.2% | 0.0% | 0 | 0
159159
--------------------------------------------------------------------------------------------------
160160
FILES tables | blob files | blob values
161161
stats prog | backing | zombie | loc zomb | live | zombie | total | refed | refed %
@@ -423,7 +423,7 @@ ITERATORS
423423
block cache | file cache | filter | sst iters | snapshots
424424
entries | hit rate | entries | hit rate | util | open | open
425425
-------------+-------------+--------------+-------------+--------------+-------------+------------
426-
7 (2.7KB) | 59.6% | 1 (472B) | 78.6% | 0.0% | 0 | 0
426+
7 (2.5KB) | 59.6% | 1 (472B) | 78.6% | 0.0% | 0 | 0
427427
--------------------------------------------------------------------------------------------------
428428
FILES tables | blob files | blob values
429429
stats prog | backing | zombie | loc zomb | live | zombie | total | refed | refed %

testdata/event_listener

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ ITERATORS
277277
block cache | file cache | filter | sst iters | snapshots
278278
entries | hit rate | entries | hit rate | util | open | open
279279
-------------+-------------+--------------+-------------+--------------+-------------+------------
280-
2 (784B) | 0.0% | 0 (0B) | 50.0% | 0.0% | 0 | 0
280+
2 (736B) | 0.0% | 0 (0B) | 50.0% | 0.0% | 0 | 0
281281
--------------------------------------------------------------------------------------------------
282282
FILES tables | blob files | blob values
283283
stats prog | backing | zombie | loc zomb | live | zombie | total | refed | refed %
@@ -410,7 +410,7 @@ ITERATORS
410410
block cache | file cache | filter | sst iters | snapshots
411411
entries | hit rate | entries | hit rate | util | open | open
412412
-------------+-------------+--------------+-------------+--------------+-------------+------------
413-
6 (2.3KB) | 0.0% | 0 (0B) | 50.0% | 0.0% | 0 | 0
413+
6 (2.2KB) | 0.0% | 0 (0B) | 50.0% | 0.0% | 0 | 0
414414
--------------------------------------------------------------------------------------------------
415415
FILES tables | blob files | blob values
416416
stats prog | backing | zombie | loc zomb | live | zombie | total | refed | refed %

0 commit comments

Comments
 (0)