|
|
|
|
|
by pkhuong
577 days ago
|
|
auto length = last - first;
while (length > 0) {
auto half = length / 2;
if (comp(first[half], value)) {
// length - half equals half + length % 2
first += length - half;
}
length = half;
}
return first;
can be restructured with a conditional `first += half` and instead `length -= half` (see, e.g., Listing 2 in https://arxiv.org/abs/1509.05053). Doing it this way requires one final comparison when `length > 0` on entry, because the while condition is `length > 1`, but moves the subtraction off the critical path. |
|
I devised this a couple years ago for use in ZFS’ btree code:
https://github.com/openzfs/zfs/commit/677c6f8457943fe5b56d7a...
Feel to have it under your choice of OSI approved license, including CC-0.