From 0c88afa386ddf7aa43a9709e423902ed739d3223 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 29 Jun 2019 13:45:49 +0300 Subject: [PATCH] lib/mergeset: speed up binarySearchKey by skipping the first item during binary search --- lib/mergeset/part_search.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/mergeset/part_search.go b/lib/mergeset/part_search.go index 876932c66..935af21cc 100644 --- a/lib/mergeset/part_search.go +++ b/lib/mergeset/part_search.go @@ -356,16 +356,15 @@ func (ps *partSearch) readInmemoryBlock(bh *blockHeader) (*inmemoryBlock, error) } func binarySearchKey(items [][]byte, key []byte) int { - if len(items) > 0 { - if string(key) <= string(items[0]) { - // Fast path - the item is the first. - return 0 - } - if len(items) > 1 && string(key) <= string(items[1]) { - // Fast path - the item is the second. - return 1 - } + if len(items) == 0 { + return 0 } + if string(key) <= string(items[0]) { + // Fast path - the item is the first. + return 0 + } + items = items[1:] + offset := uint(1) // This has been copy-pasted from https://golang.org/src/sort/search.go n := uint(len(items)) @@ -378,5 +377,5 @@ func binarySearchKey(items [][]byte, key []byte) int { j = h } } - return int(i) + return int(i+offset) }