From 9a3a88b321f0c45538a2d9a94c10a077233c2a20 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 8 Feb 2024 13:47:21 +0200 Subject: [PATCH] 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. --- lib/mergeset/encoding.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/mergeset/encoding.go b/lib/mergeset/encoding.go index f59bdc58d..3e2feedb8 100644 --- a/lib/mergeset/encoding.go +++ b/lib/mergeset/encoding.go @@ -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...) }