Skip to content

Commit 8d31644

Browse files
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)`).
2 parents 06f20f6 + 784e6a1 commit 8d31644

File tree

14 files changed

+189
-31
lines changed

14 files changed

+189
-31
lines changed

library/alloc/src/collections/vec_deque/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,30 @@ impl<T> VecDeque<T> {
559559
pub fn with_capacity(capacity: usize) -> VecDeque<T> {
560560
Self::with_capacity_in(capacity, Global)
561561
}
562+
563+
/// Creates an empty deque with space for at least `capacity` elements.
564+
///
565+
/// # Errors
566+
///
567+
/// Returns an error if the capacity exceeds `isize::MAX` _bytes_,
568+
/// or if the allocator reports allocation failure.
569+
///
570+
/// # Examples
571+
///
572+
/// ```
573+
/// # #![feature(try_with_capacity)]
574+
/// # #[allow(unused)]
575+
/// # fn example() -> Result<(), std::collections::TryReserveError> {
576+
/// use std::collections::VecDeque;
577+
///
578+
/// let deque: VecDeque<u32> = VecDeque::try_with_capacity(10)?;
579+
/// # Ok(()) }
580+
/// ```
581+
#[inline]
582+
#[unstable(feature = "try_with_capacity", issue = "91913")]
583+
pub fn try_with_capacity(capacity: usize) -> Result<VecDeque<T>, TryReserveError> {
584+
Ok(VecDeque { head: 0, len: 0, buf: RawVec::try_with_capacity_in(capacity, Global)? })
585+
}
562586
}
563587

564588
impl<T, A: Allocator> VecDeque<T, A> {

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
#![feature(trusted_len)]
164164
#![feature(trusted_random_access)]
165165
#![feature(try_trait_v2)]
166+
#![feature(try_with_capacity)]
166167
#![feature(tuple_trait)]
167168
#![feature(unchecked_math)]
168169
#![feature(unicode_internals)]

library/alloc/src/raw_vec.rs

+38-23
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@ use crate::collections::TryReserveErrorKind::*;
1717
#[cfg(test)]
1818
mod tests;
1919

20+
// One central function responsible for reporting capacity overflows. This'll
21+
// ensure that the code generation related to these panics is minimal as there's
22+
// only one location which panics rather than a bunch throughout the module.
2023
#[cfg(not(no_global_oom_handling))]
24+
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
25+
fn capacity_overflow() -> ! {
26+
panic!("capacity overflow");
27+
}
28+
2129
enum AllocInit {
2230
/// The contents of the new memory are uninitialized.
2331
Uninitialized,
32+
#[cfg(not(no_global_oom_handling))]
2433
/// The new memory is guaranteed to be zeroed.
2534
Zeroed,
2635
}
@@ -93,6 +102,8 @@ impl<T> RawVec<T, Global> {
93102
/// zero-sized. Note that if `T` is zero-sized this means you will
94103
/// *not* get a `RawVec` with the requested capacity.
95104
///
105+
/// Non-fallible version of `try_with_capacity`
106+
///
96107
/// # Panics
97108
///
98109
/// Panics if the requested capacity exceeds `isize::MAX` bytes.
@@ -104,7 +115,7 @@ impl<T> RawVec<T, Global> {
104115
#[must_use]
105116
#[inline]
106117
pub fn with_capacity(capacity: usize) -> Self {
107-
Self::with_capacity_in(capacity, Global)
118+
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global))
108119
}
109120

110121
/// Like `with_capacity`, but guarantees the buffer is zeroed.
@@ -142,15 +153,22 @@ impl<T, A: Allocator> RawVec<T, A> {
142153
#[cfg(not(no_global_oom_handling))]
143154
#[inline]
144155
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
145-
Self::allocate_in(capacity, AllocInit::Uninitialized, alloc)
156+
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc))
157+
}
158+
159+
/// Like `try_with_capacity`, but parameterized over the choice of
160+
/// allocator for the returned `RawVec`.
161+
#[inline]
162+
pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
163+
Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc)
146164
}
147165

148166
/// Like `with_capacity_zeroed`, but parameterized over the choice
149167
/// of allocator for the returned `RawVec`.
150168
#[cfg(not(no_global_oom_handling))]
151169
#[inline]
152170
pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self {
153-
Self::allocate_in(capacity, AllocInit::Zeroed, alloc)
171+
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc))
154172
}
155173

156174
/// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
@@ -179,35 +197,41 @@ impl<T, A: Allocator> RawVec<T, A> {
179197
}
180198
}
181199

182-
#[cfg(not(no_global_oom_handling))]
183-
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
200+
fn try_allocate_in(
201+
capacity: usize,
202+
init: AllocInit,
203+
alloc: A,
204+
) -> Result<Self, TryReserveError> {
184205
// Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
206+
185207
if T::IS_ZST || capacity == 0 {
186-
Self::new_in(alloc)
208+
Ok(Self::new_in(alloc))
187209
} else {
188210
// We avoid `unwrap_or_else` here because it bloats the amount of
189211
// LLVM IR generated.
190212
let layout = match Layout::array::<T>(capacity) {
191213
Ok(layout) => layout,
192-
Err(_) => capacity_overflow(),
214+
Err(_) => return Err(CapacityOverflow.into()),
193215
};
194-
match alloc_guard(layout.size()) {
195-
Ok(_) => {}
196-
Err(_) => capacity_overflow(),
216+
217+
if let Err(err) = alloc_guard(layout.size()) {
218+
return Err(err);
197219
}
220+
198221
let result = match init {
199222
AllocInit::Uninitialized => alloc.allocate(layout),
223+
#[cfg(not(no_global_oom_handling))]
200224
AllocInit::Zeroed => alloc.allocate_zeroed(layout),
201225
};
202226
let ptr = match result {
203227
Ok(ptr) => ptr,
204-
Err(_) => handle_alloc_error(layout),
228+
Err(_) => return Err(AllocError { layout, non_exhaustive: () }.into()),
205229
};
206230

207231
// Allocators currently return a `NonNull<[u8]>` whose length
208232
// matches the size requested. If that ever changes, the capacity
209233
// here should change to `ptr.len() / mem::size_of::<T>()`.
210-
Self { ptr: Unique::from(ptr.cast()), cap: unsafe { Cap(capacity) }, alloc }
234+
Ok(Self { ptr: Unique::from(ptr.cast()), cap: unsafe { Cap(capacity) }, alloc })
211235
}
212236
}
213237

@@ -537,11 +561,11 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
537561
// Central function for reserve error handling.
538562
#[cfg(not(no_global_oom_handling))]
539563
#[inline]
540-
fn handle_reserve(result: Result<(), TryReserveError>) {
564+
fn handle_reserve<T>(result: Result<T, TryReserveError>) -> T {
541565
match result.map_err(|e| e.kind()) {
566+
Ok(res) => res,
542567
Err(CapacityOverflow) => capacity_overflow(),
543568
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
544-
Ok(()) => { /* yay */ }
545569
}
546570
}
547571

@@ -561,12 +585,3 @@ fn alloc_guard(alloc_size: usize) -> Result<(), TryReserveError> {
561585
Ok(())
562586
}
563587
}
564-
565-
// One central function responsible for reporting capacity overflows. This'll
566-
// ensure that the code generation related to these panics is minimal as there's
567-
// only one location which panics rather than a bunch throughout the module.
568-
#[cfg(not(no_global_oom_handling))]
569-
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
570-
fn capacity_overflow() -> ! {
571-
panic!("capacity overflow");
572-
}

library/alloc/src/raw_vec/tests.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,14 @@ fn zst() {
105105
let v: RawVec<ZST> = RawVec::with_capacity_in(100, Global);
106106
zst_sanity(&v);
107107

108-
let v: RawVec<ZST> = RawVec::allocate_in(0, AllocInit::Uninitialized, Global);
108+
let v: RawVec<ZST> = RawVec::try_allocate_in(0, AllocInit::Uninitialized, Global).unwrap();
109109
zst_sanity(&v);
110110

111-
let v: RawVec<ZST> = RawVec::allocate_in(100, AllocInit::Uninitialized, Global);
111+
let v: RawVec<ZST> = RawVec::try_allocate_in(100, AllocInit::Uninitialized, Global).unwrap();
112112
zst_sanity(&v);
113113

114-
let mut v: RawVec<ZST> = RawVec::allocate_in(usize::MAX, AllocInit::Uninitialized, Global);
114+
let mut v: RawVec<ZST> =
115+
RawVec::try_allocate_in(usize::MAX, AllocInit::Uninitialized, Global).unwrap();
115116
zst_sanity(&v);
116117

117118
// Check all these operations work as expected with zero-sized elements.

library/alloc/src/string.rs

+13
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,19 @@ impl String {
492492
String { vec: Vec::with_capacity(capacity) }
493493
}
494494

495+
/// Creates a new empty `String` with at least the specified capacity.
496+
///
497+
/// # Errors
498+
///
499+
/// Returns [`Err`] if the capacity exceeds `isize::MAX` bytes,
500+
/// or if the memory allocator reports failure.
501+
///
502+
#[inline]
503+
#[unstable(feature = "try_with_capacity", issue = "91913")]
504+
pub fn try_with_capacity(capacity: usize) -> Result<String, TryReserveError> {
505+
Ok(String { vec: Vec::try_with_capacity(capacity)? })
506+
}
507+
495508
// HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
496509
// required for this method definition, is not available. Since we don't
497510
// require this method for testing purposes, I'll just stub it

library/alloc/src/vec/mod.rs

+34
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,22 @@ impl<T> Vec<T> {
481481
Self::with_capacity_in(capacity, Global)
482482
}
483483

484+
/// Constructs a new, empty `Vec<T>` with at least the specified capacity.
485+
///
486+
/// The vector will be able to hold at least `capacity` elements without
487+
/// reallocating. This method is allowed to allocate for more elements than
488+
/// `capacity`. If `capacity` is 0, the vector will not allocate.
489+
///
490+
/// # Errors
491+
///
492+
/// Returns an error if the capacity exceeds `isize::MAX` _bytes_,
493+
/// or if the allocator reports allocation failure.
494+
#[inline]
495+
#[unstable(feature = "try_with_capacity", issue = "91913")]
496+
pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
497+
Self::try_with_capacity_in(capacity, Global)
498+
}
499+
484500
/// Creates a `Vec<T>` directly from a pointer, a length, and a capacity.
485501
///
486502
/// # Safety
@@ -672,6 +688,24 @@ impl<T, A: Allocator> Vec<T, A> {
672688
Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 }
673689
}
674690

691+
/// Constructs a new, empty `Vec<T, A>` with at least the specified capacity
692+
/// with the provided allocator.
693+
///
694+
/// The vector will be able to hold at least `capacity` elements without
695+
/// reallocating. This method is allowed to allocate for more elements than
696+
/// `capacity`. If `capacity` is 0, the vector will not allocate.
697+
///
698+
/// # Errors
699+
///
700+
/// Returns an error if the capacity exceeds `isize::MAX` _bytes_,
701+
/// or if the allocator reports allocation failure.
702+
#[inline]
703+
#[unstable(feature = "allocator_api", issue = "32838")]
704+
// #[unstable(feature = "try_with_capacity", issue = "91913")]
705+
pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
706+
Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 })
707+
}
708+
675709
/// Creates a `Vec<T, A>` directly from a pointer, a length, a capacity,
676710
/// and an allocator.
677711
///

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![feature(pattern)]
2121
#![feature(trusted_len)]
2222
#![feature(try_reserve_kind)]
23+
#![feature(try_with_capacity)]
2324
#![feature(unboxed_closures)]
2425
#![feature(associated_type_bounds)]
2526
#![feature(binary_heap_into_iter_sorted)]

library/alloc/tests/string.rs

+11
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,17 @@ fn test_reserve_exact() {
723723
assert!(s.capacity() >= 33)
724724
}
725725

726+
#[test]
727+
#[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
728+
#[cfg_attr(target_os = "android", ignore)] // Android used in CI has a broken dlmalloc
729+
fn test_try_with_capacity() {
730+
let string = String::try_with_capacity(1000).unwrap();
731+
assert_eq!(0, string.len());
732+
assert!(string.capacity() >= 1000 && string.capacity() <= isize::MAX as usize);
733+
734+
assert!(String::try_with_capacity(usize::MAX).is_err());
735+
}
736+
726737
#[test]
727738
#[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
728739
#[cfg_attr(target_os = "android", ignore)] // Android used in CI has a broken dlmalloc

library/alloc/tests/vec.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,18 @@ fn test_reserve_exact() {
16941694
assert!(v.capacity() >= 33)
16951695
}
16961696

1697+
#[test]
1698+
#[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
1699+
#[cfg_attr(target_os = "android", ignore)] // Android used in CI has a broken dlmalloc
1700+
fn test_try_with_capacity() {
1701+
let mut vec: Vec<u32> = Vec::try_with_capacity(5).unwrap();
1702+
assert_eq!(0, vec.len());
1703+
assert!(vec.capacity() >= 5 && vec.capacity() <= isize::MAX as usize / 4);
1704+
assert!(vec.spare_capacity_mut().len() >= 5);
1705+
1706+
assert!(Vec::<u16>::try_with_capacity(isize::MAX as usize + 1).is_err());
1707+
}
1708+
16971709
#[test]
16981710
#[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
16991711
#[cfg_attr(target_os = "android", ignore)] // Android used in CI has a broken dlmalloc

library/alloc/tests/vec_deque.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,17 @@ fn test_reserve_exact_2() {
11821182
assert!(v.capacity() >= 33)
11831183
}
11841184

1185+
#[test]
1186+
#[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
1187+
#[cfg_attr(target_os = "android", ignore)] // Android used in CI has a broken dlmalloc
1188+
fn test_try_with_capacity() {
1189+
let vec: VecDeque<u32> = VecDeque::try_with_capacity(5).unwrap();
1190+
assert_eq!(0, vec.len());
1191+
assert!(vec.capacity() >= 5 && vec.capacity() <= isize::MAX as usize / 4);
1192+
1193+
assert!(VecDeque::<u16>::try_with_capacity(isize::MAX as usize + 1).is_err());
1194+
}
1195+
11851196
#[test]
11861197
#[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
11871198
#[cfg_attr(target_os = "android", ignore)] // Android used in CI has a broken dlmalloc

tests/codegen/vec-with-capacity.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//@ compile-flags: -O
2+
//@ ignore-debug
3+
// (with debug assertions turned on, `assert_unchecked` generates a real assertion)
4+
5+
#![crate_type = "lib"]
6+
#![feature(try_with_capacity)]
7+
8+
// CHECK-LABEL: @with_capacity_does_not_grow1
9+
#[no_mangle]
10+
pub fn with_capacity_does_not_grow1() -> Vec<u32> {
11+
let v = Vec::with_capacity(1234);
12+
// CHECK: call {{.*}}__rust_alloc(
13+
// CHECK-NOT: call {{.*}}__rust_realloc
14+
// CHECK-NOT: call {{.*}}capacity_overflow
15+
// CHECK-NOT: call {{.*}}finish_grow
16+
// CHECK-NOT: call {{.*}}reserve
17+
// CHECK-NOT: memcpy
18+
// CHECK-NOT: memset
19+
v
20+
}
21+
22+
// CHECK-LABEL: @try_with_capacity_does_not_grow2
23+
#[no_mangle]
24+
pub fn try_with_capacity_does_not_grow2() -> Option<Vec<Vec<u8>>> {
25+
let v = Vec::try_with_capacity(1234).ok()?;
26+
// CHECK: call {{.*}}__rust_alloc(
27+
// CHECK-NOT: call {{.*}}__rust_realloc
28+
// CHECK-NOT: call {{.*}}capacity_overflow
29+
// CHECK-NOT: call {{.*}}finish_grow
30+
// CHECK-NOT: call {{.*}}handle_alloc_error
31+
// CHECK-NOT: call {{.*}}reserve
32+
// CHECK-NOT: memcpy
33+
// CHECK-NOT: memset
34+
Some(v)
35+
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
thread 'main' panicked at library/alloc/src/raw_vec.rs:571:5:
1+
thread 'main' panicked at library/alloc/src/raw_vec.rs:26:5:
22
capacity overflow
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

tests/ui/suggestions/deref-path-method.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LL | Vec::contains(&vec, &0);
77
note: if you're trying to build a new `Vec<_, _>` consider using one of the following associated functions:
88
Vec::<T>::new
99
Vec::<T>::with_capacity
10+
Vec::<T>::try_with_capacity
1011
Vec::<T>::from_raw_parts
11-
Vec::<T, A>::new_in
12-
and 2 others
12+
and 4 others
1313
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1414
help: the function `contains` is implemented on `[_]`
1515
|

tests/ui/ufcs/bad-builder.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ LL | Vec::<Q>::mew()
77
note: if you're trying to build a new `Vec<Q>` consider using one of the following associated functions:
88
Vec::<T>::new
99
Vec::<T>::with_capacity
10+
Vec::<T>::try_with_capacity
1011
Vec::<T>::from_raw_parts
11-
Vec::<T, A>::new_in
12-
and 2 others
12+
and 4 others
1313
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1414
help: there is an associated function `new` with a similar name
1515
|

0 commit comments

Comments
 (0)