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 f7b68b466c
commit 077f84964a
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() {
ib.commonPrefix = ib.commonPrefix[:0]
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
}
data := ib.data
cp := items[0].Bytes(data)
if len(items) > 1 {
cpLen := commonPrefixLen(cp, items[len(items)-1].Bytes(data))
cp = cp[:cpLen]
}
cpLen := commonPrefixLen(cp, items[len(items)-1].Bytes(data))
cp = cp[:cpLen]
ib.commonPrefix = append(ib.commonPrefix[:0], cp...)
}