-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Vec::try_with_capacity #120504
Vec::try_with_capacity #120504
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
I has been a few months since something related came up but my understanding of the api team's position is that adding more |
5d5dfc9
to
c6a4e5f
Compare
This comment has been minimized.
This comment has been minimized.
c6a4e5f
to
8beb1a0
Compare
This comment has been minimized.
This comment has been minimized.
I think this one method is relatively uncontroversial. The Even if something as ambitious as the keyword generics initiative made progress, it's likely that you would still need some syntax to opt in to fallibility and to help type inference, and it's hard to beat the The alternative designs that are possible today don't work well. Fallibility as a type, like Rust currently has a third approach — |
8beb1a0
to
6877a07
Compare
This comment has been minimized.
This comment has been minimized.
And the next person also only adds one more method 😉 |
6877a07
to
8fee2a9
Compare
This comment has been minimized.
This comment has been minimized.
No, let's be serious. I don't think slippery slope applies here. For example, in #117503 I've added hints that make Addition of I don't think |
I... see the picture differently. Several people have made RFCs and several PRs to add those APIs. Very large API surface, not just 1 or 2 methods. It's listed as a RfL wanted feature. There's an entire crew of slope connoisseurs just waiting to slide it down. And this addition doesn't even seem all that valuable.
You can do that also with |
8fee2a9
to
feec617
Compare
This comment has been minimized.
This comment has been minimized.
Those big PRs have been rejected or stalled, so we already know there's a limit to this slope. This method enables several other Based on quick grep of this repo, outside of tests, there are 524 uses of I think |
feec617
to
a8f03a0
Compare
This comment has been minimized.
This comment has been minimized.
a8f03a0
to
d47806e
Compare
This comment has been minimized.
This comment has been minimized.
d47806e
to
913c12d
Compare
This comment has been minimized.
This comment has been minimized.
Code changes in raw_vec require blessing UI tests every time
55b85cc
to
784e6a1
Compare
@bors r+ |
…nieu Vec::try_with_capacity Related to rust-lang#91913 Implements try_with_capacity for `Vec`, `VecDeque`, and `String`. I can follow it up with more collections if desired. `Vec::try_with_capacity()` is functionally equivalent to the current stable: ```rust let mut v = Vec::new(); v.try_reserve_exact(n)? ``` However, `try_reserve` calls non-inlined `finish_grow`, which requires old and new `Layout`, and is designed to reallocate memory. There is benefit to using `try_with_capacity`, besides syntax convenience, because it generates much smaller code at the call site with a direct call to the allocator. There's codegen test included. It's also a very desirable functionality for users of `no_global_oom_handling` (Rust-for-Linux), since it makes a very commonly used function available in that environment (`with_capacity` is used much more frequently than all `(try_)reserve(_exact)`).
…nieu Vec::try_with_capacity Related to rust-lang#91913 Implements try_with_capacity for `Vec`, `VecDeque`, and `String`. I can follow it up with more collections if desired. `Vec::try_with_capacity()` is functionally equivalent to the current stable: ```rust let mut v = Vec::new(); v.try_reserve_exact(n)? ``` However, `try_reserve` calls non-inlined `finish_grow`, which requires old and new `Layout`, and is designed to reallocate memory. There is benefit to using `try_with_capacity`, besides syntax convenience, because it generates much smaller code at the call site with a direct call to the allocator. There's codegen test included. It's also a very desirable functionality for users of `no_global_oom_handling` (Rust-for-Linux), since it makes a very commonly used function available in that environment (`with_capacity` is used much more frequently than all `(try_)reserve(_exact)`).
…kingjubilee Rollup of 13 pull requests Successful merges: - rust-lang#113525 (Dynamically size sigaltstk in std) - rust-lang#114009 (compiler: allow transmute of ZST arrays with generics) - rust-lang#116793 (Allow targets to override default codegen backend) - rust-lang#118623 (Improve std::fs::read_to_string example) - rust-lang#120504 (Vec::try_with_capacity) - rust-lang#121089 (Remove `feed_local_def_id`) - rust-lang#121280 (Implement MaybeUninit::fill{,_with,_from}) - rust-lang#122087 (Add missing background color for top-level rust documentation page and increase contrast by setting text color to black) - rust-lang#122104 (Rust is a proper name: rust → Rust) - rust-lang#122110 (Make `x t miri` respect `MIRI_TEMP`) - rust-lang#122114 (Make not finding core a fatal error) - rust-lang#122115 (Cancel parsing ever made during recovery) - rust-lang#122126 (Fix `tidy --bless` on ̶X̶e̶n̶i̶x̶ Windows) r? `@ghost` `@rustbot` modify labels: rollup
…nieu Vec::try_with_capacity Related to rust-lang#91913 Implements try_with_capacity for `Vec`, `VecDeque`, and `String`. I can follow it up with more collections if desired. `Vec::try_with_capacity()` is functionally equivalent to the current stable: ```rust let mut v = Vec::new(); v.try_reserve_exact(n)? ``` However, `try_reserve` calls non-inlined `finish_grow`, which requires old and new `Layout`, and is designed to reallocate memory. There is benefit to using `try_with_capacity`, besides syntax convenience, because it generates much smaller code at the call site with a direct call to the allocator. There's codegen test included. It's also a very desirable functionality for users of `no_global_oom_handling` (Rust-for-Linux), since it makes a very commonly used function available in that environment (`with_capacity` is used much more frequently than all `(try_)reserve(_exact)`).
Rollup of 12 pull requests Successful merges: - rust-lang#99153 (Add Read Impl for &Stdin) - rust-lang#112136 (Add std::ffi::c_str module) - rust-lang#120504 (Vec::try_with_capacity) - rust-lang#121280 (Implement MaybeUninit::fill{,_with,_from}) - rust-lang#121813 (Misc improvements to non local defs lint implementation) - rust-lang#121833 (Suggest correct path in include_bytes!) - rust-lang#121860 (Add a tidy check that checks whether the fluent slugs only appear once) - rust-lang#122160 (Eagerly translate `HelpUseLatestEdition` in parser diagnostics) - rust-lang#122178 (ci: add a runner for vanilla LLVM 18) - rust-lang#122186 (Remove a workaround for a bug) - rust-lang#122215 (Some tweaks to the parallel query cycle handler) - rust-lang#122223 (Fix typo in `VisitorResult`) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 8 pull requests Successful merges: - rust-lang#99153 (Add Read Impl for &Stdin) - rust-lang#114655 (Make `impl<Fd: AsFd>` impl take `?Sized`) - rust-lang#120504 (Vec::try_with_capacity) - rust-lang#121280 (Implement MaybeUninit::fill{,_with,_from}) - rust-lang#121403 (impl From<TryReserveError> for io::Error) - rust-lang#121526 (on the fly type casting for `build.rustc` and `build.cargo`) - rust-lang#121584 (bump itertools to 0.12) - rust-lang#121711 (Implement junction_point) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#120504 - kornelski:try_with_capacity, r=Amanieu Vec::try_with_capacity Related to rust-lang#91913 Implements try_with_capacity for `Vec`, `VecDeque`, and `String`. I can follow it up with more collections if desired. `Vec::try_with_capacity()` is functionally equivalent to the current stable: ```rust let mut v = Vec::new(); v.try_reserve_exact(n)? ``` However, `try_reserve` calls non-inlined `finish_grow`, which requires old and new `Layout`, and is designed to reallocate memory. There is benefit to using `try_with_capacity`, besides syntax convenience, because it generates much smaller code at the call site with a direct call to the allocator. There's codegen test included. It's also a very desirable functionality for users of `no_global_oom_handling` (Rust-for-Linux), since it makes a very commonly used function available in that environment (`with_capacity` is used much more frequently than all `(try_)reserve(_exact)`).
This had a small perf impact |
@kornelski Could you look into the perf regressions? This is most likely due to |
Less generic code for Vec allocations Follow up to rust-lang#120504 (comment) which hopefully makes compilation faster.
Less generic code for Vec allocations Follow up to rust-lang#120504 (comment) which hopefully makes compilation faster.
Less generic code for Vec allocations Follow up to rust-lang#120504 (comment) which hopefully makes compilation faster.
Less generic code for Vec allocations Follow up to rust-lang#120504 (comment) which hopefully makes compilation faster.
Related to #91913
Implements try_with_capacity for
Vec
,VecDeque
, andString
. I can follow it up with more collections if desired.Vec::try_with_capacity()
is functionally equivalent to the current stable:However,
try_reserve
calls non-inlinedfinish_grow
, which requires old and newLayout
, and is designed to reallocate memory. There is benefit to usingtry_with_capacity
, besides syntax convenience, because it generates much smaller code at the call site with a direct call to the allocator. There's codegen test included.It's also a very desirable functionality for users of
no_global_oom_handling
(Rust-for-Linux), since it makes a very commonly used function available in that environment (with_capacity
is used much more frequently than all(try_)reserve(_exact)
).