Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: faster computation for binary search #3095

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
CarloVarni marked this conversation as resolved.
Show resolved Hide resolved
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) {
AJPfleger marked this conversation as resolved.
Show resolved Hide resolved
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
Loading