Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

block: remove unnecessary CompressAndChecksum allocation #4091

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions sstable/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,26 @@ func (t ChecksumType) String() string {

// A Checksummer calculates checksums for blocks.
type Checksummer struct {
Type ChecksumType
xxHasher *xxhash.Digest
Type ChecksumType
xxHasher *xxhash.Digest
blockTypeBuf [1]byte
}

// Checksum computes a checksum over the provided block and block type.
func (c *Checksummer) Checksum(block []byte, blockType []byte) (checksum uint32) {
func (c *Checksummer) Checksum(block []byte, blockType byte) (checksum uint32) {
// Calculate the checksum.
c.blockTypeBuf[0] = blockType
switch c.Type {
case ChecksumTypeCRC32c:
checksum = crc.New(block).Update(blockType).Value()
checksum = crc.New(block).Update(c.blockTypeBuf[:]).Value()
case ChecksumTypeXXHash64:
if c.xxHasher == nil {
c.xxHasher = xxhash.New()
} else {
c.xxHasher.Reset()
}
c.xxHasher.Write(block)
c.xxHasher.Write(blockType)
c.xxHasher.Write(c.blockTypeBuf[:])
checksum = uint32(c.xxHasher.Sum64())
default:
panic(errors.Newf("unsupported checksum type: %d", c.Type))
Expand Down
3 changes: 1 addition & 2 deletions sstable/block/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ func CompressAndChecksum(

// Calculate the checksum.
pb := PhysicalBlock{data: block}
pb.trailer[0] = byte(algo)
checksum := checksummer.Checksum(block, pb.trailer[:1])
checksum := checksummer.Checksum(block, byte(algo))
pb.trailer = MakeTrailer(byte(algo), checksum)
return pb
}
Expand Down
2 changes: 1 addition & 1 deletion sstable/value_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func (w *valueBlockWriter) compressAndFlush() {

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

Expand Down
2 changes: 1 addition & 1 deletion sstable/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ func BenchmarkWriter(b *testing.B) {
binary.BigEndian.PutUint64(key[16:], uint64(i))
keys[i] = key
}
for _, format := range []TableFormat{TableFormatPebblev2, TableFormatPebblev3} {
for _, format := range []TableFormat{TableFormatPebblev2, TableFormatPebblev3, TableFormatPebblev5} {
b.Run(fmt.Sprintf("format=%s", format.String()), func(b *testing.B) {
runWriterBench(b, keys, nil, format)
})
Expand Down
Loading