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

Improve rebuilding behaviour of BinaryHeap::retain. #78681

Merged
merged 2 commits into from
Apr 23, 2021

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Nov 2, 2020

This changes BinaryHeap::retain such that it doesn't always fully rebuild the heap, but only rebuilds the parts for which that's necessary.

This makes use of the fact that retain gives out &Ts and not &mut Ts.

Retaining every element or removing only elements at the end results in no rebuilding at all. Retaining most elements results in only reordering the elements that got moved (those after the first removed element), using the same logic as was already used for append.

cc @KodrAus @sfackler - We briefly discussed this possibility in the meeting last week while we talked about stabilization of this function (#71503).

@rust-highfive
Copy link
Collaborator

r? @kennytm

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 2, 2020
@m-ou-se m-ou-se added A-collections Area: `std::collection` T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 2, 2020
@kennytm
Copy link
Member

kennytm commented Nov 7, 2020

r? @Amanieu

@rust-highfive rust-highfive assigned Amanieu and unassigned kennytm Nov 7, 2020
@Dylan-DPC-zz
Copy link

r? @KodrAus

@rust-highfive rust-highfive assigned KodrAus and unassigned Amanieu Nov 24, 2020
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 11, 2020
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 11, 2021
@KodrAus
Copy link
Contributor

KodrAus commented Jan 14, 2021

This looks good to me! But it would be nice to see a microbenchmark or two to get an idea of what kind of a difference it makes.

@bors
Copy link
Contributor

bors commented Jan 16, 2021

☔ The latest upstream changes (presumably #77435) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

returning to author to address the merge conflict
@rustbot label: -S-waiting-on-review +S-waiting-on-author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 1, 2021
@camelid camelid added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 26, 2021
@JohnCSimon JohnCSimon added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 16, 2021
@bstrie bstrie added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 31, 2021
@Dylan-DPC-zz
Copy link

r? @Amanieu

@rust-highfive rust-highfive assigned Amanieu and unassigned KodrAus Apr 1, 2021
@Amanieu
Copy link
Member

Amanieu commented Apr 1, 2021

r=me once the merge conflict is fixed.

@crlf0710 crlf0710 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 17, 2021
@Amanieu
Copy link
Member

Amanieu commented Apr 22, 2021

ping @m-ou-se Can you resolve the merge conflict so that we can merge this?

It now doesn't fully rebuild the heap, but only the parts that are
necessary.
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 22, 2021

Thanks for the reminder!

@bors r=Amanieu

@bors
Copy link
Contributor

bors commented Apr 22, 2021

📌 Commit f5d72ab has been approved by Amanieu

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 22, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Apr 22, 2021
Improve rebuilding behaviour of BinaryHeap::retain.

This changes `BinaryHeap::retain` such that it doesn't always fully rebuild the heap, but only rebuilds the parts for which that's necessary.

This makes use of the fact that retain gives out `&T`s and not `&mut T`s.

Retaining every element or removing only elements at the end results in no rebuilding at all. Retaining most elements results in only reordering the elements that got moved (those after the first removed element), using the same logic as was already used for `append`.

cc `@KodrAus` `@sfackler` - We briefly discussed this possibility in the meeting last week while we talked about stabilization of this function (rust-lang#71503).
@bors
Copy link
Contributor

bors commented Apr 23, 2021

⌛ Testing commit f5d72ab with merge f4a8cf0...

@bors
Copy link
Contributor

bors commented Apr 23, 2021

☀️ Test successful - checks-actions
Approved by: Amanieu
Pushing f4a8cf0 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 23, 2021
@bors bors merged commit f4a8cf0 into rust-lang:master Apr 23, 2021
@rustbot rustbot added this to the 1.53.0 milestone Apr 23, 2021
@m-ou-se m-ou-se deleted the binary-heap-retain branch April 24, 2021 16:05
clint-white added a commit to clint-white/binary-heap-plus-rs that referenced this pull request Aug 7, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collection` merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.