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

Optimize BinaryHeap::extend from Vec #88282

Merged
merged 1 commit into from
Nov 14, 2021
Merged

Optimize BinaryHeap::extend from Vec #88282

merged 1 commit into from
Nov 14, 2021

Conversation

Neutron3529
Copy link
Contributor

@Neutron3529 Neutron3529 commented Aug 24, 2021

This improves the performance of extending BinaryHeaps from vectors directly. Future work may involve extending this optimization to other, similar, cases where the length of the added elements is well-known, but this is not yet done in this PR.

@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(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 Aug 24, 2021
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Neutron3529 Neutron3529 marked this pull request as draft August 24, 2021 07:26
@rust-log-analyzer

This comment has been minimized.

@Neutron3529 Neutron3529 marked this pull request as ready for review August 24, 2021 09:20
@the8472
Copy link
Member

the8472 commented Aug 24, 2021

BinaryHeap doesn't have many benchmarks, maybe some could be added to show an improvement.

@the8472 the8472 changed the title optimize the extend method optimize the BinaryHeap::extend method Aug 24, 2021
@SkiFire13
Copy link
Contributor

I'm not sure if this is something for this PR, but I just noticed that the current specialization is on I rather than I::IntoIter. This means that only BinaryHeap<T> and Vec<T> benefit from these specializations, while std::vec::IntoIter and std::collections::binary_heap::IntoIter do not.

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
<Self as SpecExtend<I>>::spec_extend(self, iter);
}

For comparison this is how it's implemented for Vec:

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
<Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter())
}

I believe we should copy Vec's code and specialize SpecExtend for std::vec::IntoIter and std::collections::binary_heap::IntoIter instead.

@Mark-Simulacrum
Copy link
Member

Can you please inline the relevant pieces of discussion / why this is a win in general, etc. from the internals thread? This both avoids having to re-read that for context and ensures it's preserved for any future visitor (into git commit history).

@Mark-Simulacrum Mark-Simulacrum 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 Aug 24, 2021
@Neutron3529
Copy link
Contributor Author

Neutron3529 commented Aug 25, 2021

Can you please inline the relevant pieces of discussion / why this is a win in general, etc. from the internals thread? This both avoids having to re-read that for context and ensures it's preserved for any future visitor (into git commit history).

I edited the first post:

as scottmcm mentioned:

BinaryHeap::append already has heuristics for how to extend BinaryHeap<T> with Vec<T>; updating extend to do the same seems perfectly reasonable.

is it enough? @Mark-Simulacrum

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 12, 2021
@JohnCSimon
Copy link
Member

Triage: setting this to waiting on review

@Mark-Simulacrum
Copy link
Member

Mark-Simulacrum commented Sep 15, 2021

It looks like this PR is doing two separate things:

  • Adding a specialization for extending with a Vec<T>
  • Modifying the non-specialized extend algorithm

This first of these makes sense, seems fine, but the second I don't see a description of the goal in this PR. It also seems like these two changes are orthogonal and should belong in separate PRs?

FWIW, saying "discussion is here" and pointing into the middle of an internals thread is not ideal; the PR description should ideally be self-contained or at least only point to documents or summary comments, not just discussion threads. This will speed up review and make the flow easier for everyone.

@rustbot author

(Please adjust the label with @rustbot ready when you're ready for another review).

@rustbot

This comment has been minimized.

@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 Sep 15, 2021
@rustbot
Copy link
Collaborator

rustbot commented Sep 15, 2021

@Neutron3529

@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 Oct 3, 2021
@apiraino apiraino added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Oct 14, 2021
@apiraino apiraino added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 14, 2021
@JohnCSimon
Copy link
Member

Ping from triage:

@Neutron3529 - can you please address the merge conflicts and
adjust the label with @rustbot ready when you're ready for another review

@Neutron3529
Copy link
Contributor Author

@rustbot ready
I deleted the improvement that could speed up the default extend method which may end up with a double panic.
then, there is no conflict.

It might be meanful that mark fn rebuild_tail as pub unsafe, or add some new function like extend_batch to gain benefit from the fast rebuild_tail algorithm

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 8, 2021
@Mark-Simulacrum
Copy link
Member

Please update the PR description to reflect the current state of this PR. Otherwise, this PR looks good to me, r=me with the description updated and commits squashed.

@Mark-Simulacrum Mark-Simulacrum 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 Nov 9, 2021
@Neutron3529 Neutron3529 changed the title optimize the BinaryHeap::extend method provide a SpecExtend trait for Vec<T> Nov 12, 2021
@Neutron3529
Copy link
Contributor Author

@rustbot ready
done~

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 12, 2021
@Mark-Simulacrum Mark-Simulacrum changed the title provide a SpecExtend trait for Vec<T> Optimize BinaryHeap::extend from Vec Nov 14, 2021
@Mark-Simulacrum
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Nov 14, 2021

📌 Commit 2feee36 has been approved by Mark-Simulacrum

@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-review Status: Awaiting review from the assignee but also interested parties. labels Nov 14, 2021
@bors
Copy link
Contributor

bors commented Nov 14, 2021

⌛ Testing commit 2feee36 with merge ad44239...

@bors
Copy link
Contributor

bors commented Nov 14, 2021

☀️ Test successful - checks-actions
Approved by: Mark-Simulacrum
Pushing ad44239 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 14, 2021
@bors bors merged commit ad44239 into rust-lang:master Nov 14, 2021
@rustbot rustbot added this to the 1.58.0 milestone Nov 14, 2021
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ad44239): comparison url.

Summary: This change led to moderate relevant mixed results 🤷 in compiler performance.

  • Moderate improvement in instruction counts (up to -0.3% on incr-unchanged builds of ripgrep)
  • Small regression in instruction counts (up to 0.4% on incr-unchanged builds of regression-31157)

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression

@rustbot rustbot added the perf-regression Performance regression. label Nov 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. 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.