-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
Since @bors try @rust-timer queue |
Awaiting bors try build completion. @rustbot label: +S-waiting-on-perf |
⌛ Trying commit 3ad6a9cebd6932b0f8c9b44462442f0f1f08ec46 with merge 1ad6c9ca95479a2d0a3f62df1c16af2064866c0d... |
This comment has been minimized.
This comment has been minimized.
3ad6a9c
to
3da0201
Compare
This comment has been minimized.
This comment has been minimized.
3da0201
to
8e14d30
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
8e14d30
to
ce0b668
Compare
This comment has been minimized.
This comment has been minimized.
@bors try |
⌛ Trying commit ce0b668c792325b8e8482ed5bbcad5f305af83fb with merge 00118a4eca2c471243a0886aa38756a6236daa23... |
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 Am I missing something? |
|
☀️ Try build successful - checks-actions |
1 similar comment
☀️ Try build successful - checks-actions |
Queued 00118a4eca2c471243a0886aa38756a6236daa23 with parent 60e70cc, future comparison URL. |
I would prefer if we wrapped the value in a proper error type instead of just returning a raw |
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.
LGTM!
/// 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 |
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.
Use .unwrap()
to show that this cannot fail instead of ignoring the error.
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.
I wrote it this way to avoid panic codegen which can be one of the benefits of this method.
@the8472 any updates on this? thanks |
dc55456
to
bb74f97
Compare
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
@rustbot ready |
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 |
☀️ Test successful - checks-actions |
Finished benchmarking commit (1a7c203): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis 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.
CyclesResultsThis 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.
Footnotes |
This method can serve several purposes. It
reserve
andpush
separately to avoid pulling in the allocation machinery a second time in thepush
part which should make things easier on the optimizerArrayVec
a bit since - compared topush()
- there are fewer questions around how it should be implementedI 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