Skip to content

Commit

Permalink
colblk: check first timestamp before binary search
Browse files Browse the repository at this point in the history
This commit improves the code in the cockroach key seeker to first
check the latest timestamp (which will be the one we're looking for in
a majority of cases).

```
name                                                                                                                 old time/op    new time/op    delta
CockroachDataBlockIterShort/AlphaLen=8,Prefix=8,Shared=4,KeysPerPrefix=4,Logical=10,ValueLen=8/Next-10                 11.4ns ± 4%    11.2ns ± 1%    ~     (p=0.308 n=8+8)
CockroachDataBlockIterShort/AlphaLen=8,Prefix=8,Shared=4,KeysPerPrefix=4,Logical=10,ValueLen=8/SeekGE-10                118ns ± 0%     118ns ± 0%  +0.24%  (p=0.023 n=8+8)
CockroachDataBlockIterShort/AlphaLen=8,Prefix=8,Shared=4,KeysPerPrefix=4,Logical=10,ValueLen=8/SeekGELatest-10          109ns ± 0%     108ns ± 6%  -1.29%  (p=0.019 n=8+7)
CockroachDataBlockIterShort/AlphaLen=8,Prefix=128,Shared=64,KeysPerPrefix=4,Logical=10,ValueLen=128/Next-10            10.6ns ± 0%    10.6ns ± 0%  -0.64%  (p=0.000 n=7+8)
CockroachDataBlockIterShort/AlphaLen=8,Prefix=128,Shared=64,KeysPerPrefix=4,Logical=10,ValueLen=128/SeekGE-10          95.0ns ± 0%    92.2ns ± 0%  -2.94%  (p=0.000 n=8+8)
CockroachDataBlockIterShort/AlphaLen=8,Prefix=128,Shared=64,KeysPerPrefix=4,Logical=10,ValueLen=128/SeekGELatest-10    88.2ns ± 0%    83.6ns ± 0%  -5.23%  (p=0.000 n=8+7)
````
  • Loading branch information
RaduBerinde committed Oct 8, 2024
1 parent f012e3e commit b6df3e9
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions sstable/colblk/cockroach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,18 @@ func (ks *cockroachKeySeeker) seekGEOnSuffix(index int, seekSuffix []byte) (row

// TODO(jackson): What if the row has an untyped suffix?

// Binary search between [index, prefixChanged.SeekSetBitGE(index+1)].
// First check the suffix at index, because querying for the latest value is
// the most common case.
if latestWallTime := ks.mvccWallTimes.At(index); latestWallTime < seekWallTime ||
(latestWallTime == seekWallTime && uint32(ks.mvccLogical.At(index)) <= seekLogicalTime) {
return index
}

// Binary search between [index+1, prefixChanged.SeekSetBitGE(index+1)].
//
// Define f(i) = true iff key at i is >= seek key.
// Invariant: f(l-1) == false, f(u) == true.
l := index
l := index + 1
u := ks.reader.prefixChanged.SeekSetBitGE(index + 1)
for l < u {
h := int(uint(l+u) >> 1) // avoid overflow when computing h
Expand Down

0 comments on commit b6df3e9

Please sign in to comment.