Skip to content

Commit 53f12aa

Browse files
committed
sstable: optimize blobReferenceValues allocation
When writing a sstable containing references to blob files, the sstable writer constructs the blob reference liveness index—an accounting of which values in which blob files are referenced. Once we've finalized the data for an individual block within a blob file, we encode the data to a buffer through appending. These appends show up as 3% of all allocations in heap profiles from a recent run of the pebble ycsb/D/values=1024 benchmark. This commit adds a call to slices.Grow to avoid unnecessary intermediate allocations. Additionally, when constructing state for a new blob file, we allocate an initial 512B under the expectation that most referenced blob files will require around 512B to encode all the values that are referenced from the sstable.
1 parent 9d1f319 commit 53f12aa

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

sstable/blob_reference_index.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package sstable
77
import (
88
"encoding/binary"
99
"iter"
10+
"slices"
1011

1112
"github.com/cockroachdb/errors"
1213
"github.com/cockroachdb/pebble/internal/base"
@@ -55,9 +56,12 @@ func (s *blobReferenceValues) finishCurrentBlock() {
5556
if s.currentBlock.valuesSize == 0 {
5657
panic(errors.AssertionFailedf("no pending current block"))
5758
}
59+
// Ensure there's enough space for the bytes we're about to append.
60+
bitmapSize := s.currentBlock.bitmap.Size()
61+
s.encodedFinishedBlocks = slices.Grow(s.encodedFinishedBlocks, 3*binary.MaxVarintLen64+bitmapSize)
5862
s.encodedFinishedBlocks = binary.AppendUvarint(s.encodedFinishedBlocks, uint64(s.currentBlock.blockID))
5963
s.encodedFinishedBlocks = binary.AppendUvarint(s.encodedFinishedBlocks, s.currentBlock.valuesSize)
60-
s.encodedFinishedBlocks = binary.AppendUvarint(s.encodedFinishedBlocks, uint64(s.currentBlock.bitmap.Size()))
64+
s.encodedFinishedBlocks = binary.AppendUvarint(s.encodedFinishedBlocks, uint64(bitmapSize))
6165
s.encodedFinishedBlocks = s.currentBlock.bitmap.FinishAndAppend(s.encodedFinishedBlocks)
6266
}
6367

@@ -107,6 +111,7 @@ func (w *blobRefValueLivenessWriter) addLiveValue(
107111

108112
// We have a new reference.
109113
state := blobReferenceValues{}
114+
state.encodedFinishedBlocks = slices.Grow(state.encodedFinishedBlocks, 512)
110115
state.initNewBlock(blockID)
111116
w.refState = append(w.refState, state)
112117
}

0 commit comments

Comments
 (0)