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

add Vec::push_within_capacity - fallible, does not allocate #89123

Merged
merged 2 commits into from
Oct 10, 2022

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Sep 20, 2021

This method can serve several purposes. It

  • is fallible
  • guarantees that items in Vec aren't moved
  • allows loops that do reserve and push separately to avoid pulling in the allocation machinery a second time in the push part which should make things easier on the optimizer
  • eases the path towards ArrayVec a bit since - compared to push() - there are fewer questions around how it should be implemented

I haven't named it try_push because that should probably occupy a middle ground that will still try to reserve and only return an error in the unlikely OOM case.

resolves #84649

@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 Sep 20, 2021
@the8472 the8472 added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Sep 20, 2021
@the8472
Copy link
Member Author

the8472 commented Sep 20, 2021

Since push and extend_desugared are very hot code:

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 20, 2021
@bors
Copy link
Contributor

bors commented Sep 20, 2021

⌛ Trying commit 3ad6a9cebd6932b0f8c9b44462442f0f1f08ec46 with merge 1ad6c9ca95479a2d0a3f62df1c16af2064866c0d...

@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.

@the8472

This comment has been minimized.

@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 Sep 20, 2021
@the8472
Copy link
Member Author

the8472 commented Sep 20, 2021

@bors try

@bors
Copy link
Contributor

bors commented Sep 20, 2021

⌛ Trying commit ce0b668c792325b8e8482ed5bbcad5f305af83fb with merge 00118a4eca2c471243a0886aa38756a6236daa23...

@lukaslueg
Copy link
Contributor

Quick thought: The machinery provided and the example given could be expressed more concise as

let mut vec = Vec::new();
for value in iter {
    // push() will not reallocate
    vec.try_reserve(1).and_then(|v| v.push(value))?;
}
Ok(vec)

if try_reserve(&mut self) -> Result<&mut Self, TryReserveError> instead of Result<(), TryReserveError>.

Am I missing something?

@the8472
Copy link
Member Author

the8472 commented Sep 20, 2021

Am I missing something?

push is #[cfg(not(no_global_oom_handling))] which means it's not available in code that doesn't want OOM panics. And it contains a call to reserve() which makes things more difficult for the optimizer when you call it in a loop.

@bors
Copy link
Contributor

bors commented Sep 20, 2021

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

1 similar comment
@bors
Copy link
Contributor

bors commented Sep 20, 2021

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

@rust-timer
Copy link
Collaborator

Queued 00118a4eca2c471243a0886aa38756a6236daa23 with parent 60e70cc, future comparison URL.

@Amanieu
Copy link
Member

Amanieu commented Sep 20, 2021

I would prefer if we wrapped the value in a proper error type instead of just returning a raw T. It's a common pattern in the standard library (see BTreeMap::try_insert which returns an OccupiedError).

@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 Jul 24, 2022
@JohnTitor
Copy link
Member

This hasn't received a review from @kennytm for 10 months so let's re-assign, maybe r? @Amanieu as you left a comment in the past

@rust-highfive rust-highfive assigned Amanieu and unassigned kennytm Jul 25, 2022
Copy link
Member

@Amanieu Amanieu left a comment

Choose a reason for hiding this comment

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

LGTM!

library/alloc/src/vec/mod.rs Outdated Show resolved Hide resolved
library/alloc/src/vec/mod.rs Outdated Show resolved Hide resolved
/// for value in iter {
/// if let Err(value) = vec.push_within_capacity(value) {
/// vec.try_reserve(1)?;
/// // this cannot fail, the previous line either returned or added at least 1 free slot
Copy link
Member

Choose a reason for hiding this comment

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

Use .unwrap() to show that this cannot fail instead of ignoring the error.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wrote it this way to avoid panic codegen which can be one of the benefits of this method.

library/alloc/src/vec/mod.rs Outdated Show resolved Hide resolved
@Dylan-DPC Dylan-DPC 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 10, 2022
@Dylan-DPC
Copy link
Member

@the8472 any updates on this? thanks

@rustbot
Copy link
Collaborator

rustbot commented Aug 13, 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

@the8472
Copy link
Member Author

the8472 commented Aug 13, 2022

@rustbot ready

@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 Aug 13, 2022
@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 Oct 8, 2022
@the8472
Copy link
Member Author

the8472 commented Oct 9, 2022

I addressed review comments except the one on the doc comment which I disagree with and haven't received further feedback on it. Since the reviewer gave his LGTM I'll take it as approval.

@bors r=amanieu

@bors
Copy link
Contributor

bors commented Oct 9, 2022

📌 Commit bb74f97 has been approved by amanieu

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 Oct 9, 2022
@bors
Copy link
Contributor

bors commented Oct 9, 2022

⌛ Testing commit bb74f97 with merge 1a7c203...

@bors
Copy link
Contributor

bors commented Oct 10, 2022

☀️ Test successful - checks-actions
Approved by: amanieu
Pushing 1a7c203 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 10, 2022
@bors bors merged commit 1a7c203 into rust-lang:master Oct 10, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 10, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1a7c203): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

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.

mean1 range count2
Regressions ❌
(primary)
0.8% [0.8%, 0.8%] 1
Regressions ❌
(secondary)
2.9% [2.9%, 2.9%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.6% [-2.6%, -2.6%] 1
All ❌✅ (primary) 0.8% [0.8%, 0.8%] 1

Cycles

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.

mean1 range count2
Regressions ❌
(primary)
2.5% [2.5%, 2.5%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.5% [2.5%, 2.5%] 1

Footnotes

  1. the arithmetic mean of the percent change 2

  2. number of relevant changes 2

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. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vec::push_in_capacity ?