Skip to content

Commit 13f37fa

Browse files
committed
block: remove unnecessary CompressAndChecksum allocation
This allocation snuck in during the block package refactors. ``` │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ Writer/format=(Pebble,v2)/block=4.0KB/filter=true/compression=NoCompression-10 18.985k ± 0% 9.533k ± 0% -49.79% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=4.0KB/filter=true/compression=Snappy-10 18.991k ± 0% 9.536k ± 0% -49.79% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=4.0KB/filter=true/compression=ZSTD-10 66.29k ± 0% 56.84k ± 0% -14.25% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=4.0KB/filter=false/compression=NoCompression-10 18.974k ± 0% 9.521k ± 0% -49.82% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=4.0KB/filter=false/compression=Snappy-10 18.978k ± 0% 9.525k ± 0% -49.81% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=4.0KB/filter=false/compression=ZSTD-10 66.27k ± 0% 56.83k ± 0% -14.26% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=32KB/filter=true/compression=NoCompression-10 2.377k ± 0% 1.218k ± 0% -48.76% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=32KB/filter=true/compression=Snappy-10 2.379k ± 0% 1.220k ± 0% -48.72% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=32KB/filter=true/compression=ZSTD-10 8.184k ± 0% 7.024k ± 0% -14.17% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=32KB/filter=false/compression=NoCompression-10 2.365k ± 0% 1.207k ± 0% -48.96% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=32KB/filter=false/compression=Snappy-10 2.366k ± 0% 1.208k ± 0% -48.95% (p=0.000 n=10) Writer/format=(Pebble,v2)/block=32KB/filter=false/compression=ZSTD-10 8.172k ± 0% 7.011k ± 0% -14.21% (p=0.000 n=10) ``` Fix #4090.
1 parent a685eea commit 13f37fa

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

sstable/block/block.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,26 @@ func (t ChecksumType) String() string {
119119

120120
// A Checksummer calculates checksums for blocks.
121121
type Checksummer struct {
122-
Type ChecksumType
123-
xxHasher *xxhash.Digest
122+
Type ChecksumType
123+
xxHasher *xxhash.Digest
124+
blockTypeBuf [1]byte
124125
}
125126

126127
// Checksum computes a checksum over the provided block and block type.
127-
func (c *Checksummer) Checksum(block []byte, blockType []byte) (checksum uint32) {
128+
func (c *Checksummer) Checksum(block []byte, blockType byte) (checksum uint32) {
128129
// Calculate the checksum.
130+
c.blockTypeBuf[0] = blockType
129131
switch c.Type {
130132
case ChecksumTypeCRC32c:
131-
checksum = crc.New(block).Update(blockType).Value()
133+
checksum = crc.New(block).Update(c.blockTypeBuf[:]).Value()
132134
case ChecksumTypeXXHash64:
133135
if c.xxHasher == nil {
134136
c.xxHasher = xxhash.New()
135137
} else {
136138
c.xxHasher.Reset()
137139
}
138140
c.xxHasher.Write(block)
139-
c.xxHasher.Write(blockType)
141+
c.xxHasher.Write(c.blockTypeBuf[:])
140142
checksum = uint32(c.xxHasher.Sum64())
141143
default:
142144
panic(errors.Newf("unsupported checksum type: %d", c.Type))

sstable/block/compression.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ func CompressAndChecksum(
254254

255255
// Calculate the checksum.
256256
pb := PhysicalBlock{data: block}
257-
pb.trailer[0] = byte(algo)
258-
checksum := checksummer.Checksum(block, pb.trailer[:1])
257+
checksum := checksummer.Checksum(block, byte(algo))
259258
pb.trailer = MakeTrailer(byte(algo), checksum)
260259
return pb
261260
}

sstable/value_block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ func (w *valueBlockWriter) compressAndFlush() {
519519

520520
func (w *valueBlockWriter) computeChecksum(blk []byte) {
521521
n := len(blk) - block.TrailerLen
522-
checksum := w.checksummer.Checksum(blk[:n], blk[n:n+1])
522+
checksum := w.checksummer.Checksum(blk[:n], blk[n])
523523
binary.LittleEndian.PutUint32(blk[n+1:], checksum)
524524
}
525525

sstable/writer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ func BenchmarkWriter(b *testing.B) {
11061106
binary.BigEndian.PutUint64(key[16:], uint64(i))
11071107
keys[i] = key
11081108
}
1109-
for _, format := range []TableFormat{TableFormatPebblev2, TableFormatPebblev3} {
1109+
for _, format := range []TableFormat{TableFormatPebblev2, TableFormatPebblev3, TableFormatPebblev5} {
11101110
b.Run(fmt.Sprintf("format=%s", format.String()), func(b *testing.B) {
11111111
runWriterBench(b, keys, nil, format)
11121112
})

0 commit comments

Comments
 (0)