lib/mergeset: do not store commonPrefix in blockHeader if the block contains only a single item

There is no sense in storing commonPrefix for blockHeader containing only a single item,
since this only increases blockHeader size without any benefits.
This commit is contained in:
Aliaksandr Valialkin 2024-02-08 13:47:21 +02:00
parent 84a3f4719d
commit 9a3a88b321
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -104,17 +104,18 @@ func (ib *inmemoryBlock) Reset() {
} }
func (ib *inmemoryBlock) updateCommonPrefixSorted() { func (ib *inmemoryBlock) updateCommonPrefixSorted() {
ib.commonPrefix = ib.commonPrefix[:0]
items := ib.items items := ib.items
if len(items) == 0 { if len(items) <= 1 {
// There is no sense in duplicating a single item or zero items into commonPrefix,
// since this only can increase blockHeader size without any benefits.
ib.commonPrefix = ib.commonPrefix[:0]
return return
} }
data := ib.data data := ib.data
cp := items[0].Bytes(data) cp := items[0].Bytes(data)
if len(items) > 1 { cpLen := commonPrefixLen(cp, items[len(items)-1].Bytes(data))
cpLen := commonPrefixLen(cp, items[len(items)-1].Bytes(data)) cp = cp[:cpLen]
cp = cp[:cpLen]
}
ib.commonPrefix = append(ib.commonPrefix[:0], cp...) ib.commonPrefix = append(ib.commonPrefix[:0], cp...)
} }