-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Change BinaryHeap::append rebuild heuristic #77435
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @withoutboats (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
Given your comment in #77433 (comment) that
I've marked this as waiting-on author for now. Once you've made any changes you think are necessary, please run the following to mark it as needing review again:
|
Ping from triage - |
As discussed in the comments of #77433 the heuristic should be changed (and not removed, as this PR currently does). I've done some benchmarks and I'll update the issue with that later this week. I hope that will shed some light on what the heuristic should be, then the PR can be updated. |
@hanmertens Ping from triage, any updates on this? |
See rust-lang#77433 for why the new heuristic was chosen. Fixes rust-lang#77433
fcf4750
to
32a20f4
Compare
@@ -630,10 +630,16 @@ impl<T: Ord> BinaryHeap<T> { | |||
// and about 2 * (len1 + len2) comparisons in the worst case | |||
// while `extend` takes O(len2 * log(len1)) operations | |||
// and about 1 * len2 * log_2(len1) comparisons in the worst case, | |||
// assuming len1 >= len2. | |||
// assuming len1 >= len2. For larger heaps, the crossover point | |||
// no longer follows this reasoning and was determined empirically. | |||
#[inline] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this still be marked #[inline]
now that it's longer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only used once, so that should still be fine I think.
I haven't received negative feedback on #77433 (comment), so I just pushed the heuristic I proposed there. TLDR; it's not optimal but better than the current one. |
Thanks! Seems reasonable to me, based on the other issue. @bors r+ rollup=maybe (I don't think the compiler uses binary heaps enough for this to affect its perf, so doesn't need to be |
📌 Commit 32a20f4 has been approved by |
You are correct 😆 |
In fact the only place |
…ottmcm Always use extend in BinaryHeap::append This is faster, see rust-lang#77433. Fixes rust-lang#77433
☀️ Test successful - checks-actions |
This commit ports the change in the rebuild heuristic used by `BinaryHeap::append()` that was added in rust-lang/rust#77435: "Change BinaryHeap::append rebuild heuristic". See also the discussion in rust-lang/rust#77433: "Suboptimal performance of BinaryHeap::append" for more information on how the new heuristic was chosen. It also ports the new private method `.rebuild_tail()` now used by `std::collections::BinaryHeap::append()` from rust-lang/rust#78681: "Improve rebuilding behaviour of BinaryHeap::retain". Note that Rust 1.60.0 adds the clippy lint `manual_bits` which warns against code used here. We suppress the lint instead of following the upstream patch which now uses `usize::BITS`, since this was stabilized in Rust 1.53.0 and this crate's MSRV is currently 1.36.0.
This is faster, see #77433.
Fixes #77433