mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/mergeset: speed up binarySearchKey by skipping the first item during binary search
This commit is contained in:
parent
74c0fb04f3
commit
0c88afa386
1 changed files with 9 additions and 10 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue