Skip to content

Commit c37c666

Browse files
committed
Add an optimizer hint for the capacity that with_capacity_in returns
1 parent c8cbd5c commit c37c666

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Diff for: alloc/src/raw_vec.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,13 @@ impl<A: Allocator> RawVecInner<A> {
423423
#[inline]
424424
fn with_capacity_in(capacity: usize, alloc: A, elem_layout: Layout) -> Self {
425425
match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc, elem_layout) {
426-
Ok(res) => res,
426+
Ok(this) => {
427+
unsafe {
428+
// Make it more obvious that a subsquent Vec::reserve(capacity) will not allocate.
429+
hint::assert_unchecked(!this.needs_to_grow(0, capacity, elem_layout));
430+
}
431+
this
432+
}
427433
Err(err) => handle_error(err),
428434
}
429435
}

0 commit comments

Comments
 (0)