Skip to content

Commit

Permalink
refactor: faster computation for binary search (acts-project#3095)
Browse files Browse the repository at this point in the history
Faster binary search then `std::lower_bound`
  • Loading branch information
CarloVarni authored and asalzburger committed May 21, 2024
1 parent 68838b2 commit ab9077d
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Core/include/Acts/Seeding/Neighbour.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,33 @@ Neighbour<grid_t>::Neighbour(const grid_t& grid, std::size_t idx,
else if (collection.back()->radius() < lowerBound) {
itr = collection.end();
}
/// Cannot decide a priori. We need to find the first element suche that it's
/// Cannot decide a priori. We need to find the first element such that it's
/// radius is > lower bound. We use a binary search in this case
else {
itr = std::lower_bound(collection.begin(), collection.end(), lowerBound,
[](const auto& sp, const float target) -> bool {
return sp->radius() < target;
});
// custom binary search which was observed to be faster than
// `std::lower_bound` see https://github.com/acts-project/acts/pull/3095
std::size_t start = 0ul;
std::size_t stop = collection.size() - 1;
while (start <= stop) {
std::size_t mid = (start + stop) / 2;
if (collection[mid]->radius() == lowerBound) {
itr = collection.begin() + mid;
return;
} else if (collection[mid]->radius() > lowerBound) {
if (mid > 0 && collection[mid - 1]->radius() < lowerBound) {
itr = collection.begin() + mid;
return;
}
stop = mid - 1;
} else {
if (mid + 1 < collection.size() &&
collection[mid + 1]->radius() > lowerBound) {
itr = collection.begin() + mid + 1;
return;
}
start = mid + 1;
}
} // while loop
}
}

Expand Down

0 comments on commit ab9077d

Please sign in to comment.