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

VecDeque::resize should re-use the buffer in the passed-in element #104435

Merged
merged 2 commits into from
Nov 20, 2022

Conversation

scottmcm
Copy link
Member

Today it always copies it for every appended element, but one of those clones is avoidable.

This adds iter::repeat_n (#104434) as the primitive needed to do this. If this PR is acceptable, I'll also use this in Vec rather than its custom ExtendElement type & infrastructure that is harder to share between multiple different containers:

trait ExtendWith<T> {
fn next(&mut self) -> T;
fn last(self) -> T;
}
struct ExtendElement<T>(T);
impl<T: Clone> ExtendWith<T> for ExtendElement<T> {
fn next(&mut self) -> T {
self.0.clone()
}
fn last(self) -> T {
self.0
}
}

@rustbot
Copy link
Collaborator

rustbot commented Nov 15, 2022

r? @joshtriplett

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 15, 2022
@rustbot
Copy link
Collaborator

rustbot commented Nov 15, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

// CHECK-LABEL: @vec_extend_via_iter_repeat_n
pub fn vec_extend_via_iter_repeat_n() -> Vec<u8> {
// CHECK: %[[ADDR:.+]] = tail call dereferenceable_or_null(1234) ptr @__rust_alloc(i64 1234, i64 1)
// CHECK: tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(1234) %[[ADDR]], i8 42, i64 1234,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helps show that we can probably use the iterator rather than special custom extend code like

if n > 0 {
// We can write the last element directly without cloning needlessly
ptr::write(ptr, value.last());
local_len.increment_len(1);
}

but Vec is really important so I don't want to do that in the same PR as this fix.

@scottmcm
Copy link
Member Author

Hopefully I can do this despite the new API -- with doc(hidden) if needed -- so I'll try a non-libs-api reviewer who hopefully is less busy than I know Josh is
r? @thomcc

Today it always copies it for *every* appended element, but one of those clones is avoidable.
@thomcc
Copy link
Member

thomcc commented Nov 16, 2022

Do you want to open an ACP for the new API? I don't mind reviewing the impl, though.

@scottmcm
Copy link
Member Author

ACP's already open: rust-lang/libs-team#120

@thomcc
Copy link
Member

thomcc commented Nov 16, 2022

Thanks, I'll get to the review tomorrow or so then.

Copy link
Member

@thomcc thomcc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit tricky but not very much code and it's pretty well commented, so doesn't seem that bad overall.

r=me on impl, I think it needs the ACP to be seconded to land (although I think the API is a good idea)

@scottmcm
Copy link
Member Author

@thomcc To be able to decouple the VecDeque improvement from the ACP discussion, how would you feel about me marking the function & struct [doc(hidden)] for now? (Not pub(crate) so that the codegen tests can verify it and such.)

@thomcc
Copy link
Member

thomcc commented Nov 18, 2022

After some discussion with other reviewers (for example, @cuviper), I think this is fine. We just need to remember to change the approach if the ACP is rejected, and it probably should be doc(hidden) initially.

@scottmcm
Copy link
Member Author

@bors r=thomcc rollup

@bors
Copy link
Contributor

bors commented Nov 19, 2022

📌 Commit 71bb200 has been approved by thomcc

It is now in the queue for this repository.

@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 19, 2022
@thomcc
Copy link
Member

thomcc commented Nov 19, 2022

Unsure about rollup here, shouldn't this be expected to improve perf if the compiler uses VecDeque::resize? (which it might not, but it's hard to say)

@scottmcm
Copy link
Member Author

I'll just ask perf, then!

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 19, 2022
@scottmcm
Copy link
Member Author

@bors try @rust-timer queue

@scottmcm
Copy link
Member Author

@thomcc Given how big the bors queue has been lately, I'd rather do a perf run and rollup than make it run on its own, was my thought.

@bors
Copy link
Contributor

bors commented Nov 19, 2022

☀️ Try build successful - checks-actions
Build commit: ba6c2e262132dbff0c22d04a0c095a8ff5643767 (ba6c2e262132dbff0c22d04a0c095a8ff5643767)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ba6c2e262132dbff0c22d04a0c095a8ff5643767): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.9% [1.6%, 2.2%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.4% [1.4%, 1.4%] 1
Regressions ❌
(secondary)
1.7% [1.0%, 2.4%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.8% [-2.3%, -0.8%] 3
All ❌✅ (primary) 1.4% [1.4%, 1.4%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 19, 2022
@scottmcm

This comment was marked as outdated.

@bors

This comment was marked as outdated.

@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. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 19, 2022
@scottmcm

This comment was marked as outdated.

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 19, 2022
@scottmcm
Copy link
Member Author

@bors r=thomcc rollup

@bors
Copy link
Contributor

bors commented Nov 19, 2022

📌 Commit 71bb200 has been approved by thomcc

It is now in the queue for this repository.

@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 Nov 19, 2022
@scottmcm
Copy link
Member Author

Two secondary regressions in code that doesn't use VecDeque suggest to me that this isn't actually a meaningful change, and thus the rollup is fine.

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 20, 2022
Rollup of 6 pull requests

Successful merges:

 - rust-lang#103901 (Add tracking issue for `const_arguments_as_str`)
 - rust-lang#104112 (rustdoc: Add copy to the description of repeat)
 - rust-lang#104435 (`VecDeque::resize` should re-use the buffer in the passed-in element)
 - rust-lang#104467 (Fix substraction with overflow in `wrong_number_of_generic_args.rs`)
 - rust-lang#104608 (Cleanup macro matching recovery)
 - rust-lang#104626 (Fix doctest errors related to rustc_middle)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 785237d into rust-lang:master Nov 20, 2022
@rustbot rustbot added this to the 1.67.0 milestone Nov 20, 2022
@scottmcm scottmcm deleted the iter-repeat-n branch November 20, 2022 07:54
@scottmcm
Copy link
Member Author

(The rollup had no perf changes, so this was probably neutral.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

6 participants