From c5830686dea641b6efaec98607412c4ce204aff2 Mon Sep 17 00:00:00 2001 From: Raz Luvaton <16746759+rluvaton@users.noreply.github.com> Date: Sat, 20 Sep 2025 22:05:38 +0300 Subject: [PATCH] perf: change `RawVec` `grow_one` from `#[inline(never)]` to `#[inline]` I noticed that this was not inlined when trying to `push` and probably the reason why the compiler lose the information about the capacity (fixes https://github.com/rust-lang/rust/issues/82801) BTW The original PR that added it (https://github.com/rust-lang/rust/pull/91352) wrote: > [...] I tried lots of minor variations on this, e.g. different inlining attributes. This was the best one I could find. [...] I never contributed to Rust so I did not know how to test that it actually fixes the problem, but I'm fairly certain it is. Consider the very basic example (Godbolt rustc 1.90.0 `-C opt-level=3 -C target-feature=+avx2 -C codegen-units=1`) ```rust #[no_mangle] fn extend_offsets(offsets: &[usize]) -> Vec:: { let mut intermediate = Vec::::with_capacity(offsets.len()); for &offset in offsets { intermediate.push(offset) } intermediate } ``` it does not inline `grow_one` which make it not use SIMD. If however we are using [`push_within_capacity`](https://github.com/rust-lang/rust/issues/100486): ```rust #![feature(vec_push_within_capacity)] #[no_mangle] fn extend_offsets(offsets: &[usize]) -> Vec:: { let mut intermediate = Vec::::with_capacity(offsets.len()); for &offset in offsets { intermediate.push_within_capacity(offset).unwrap() } intermediate } ``` it will use SIMD --- library/alloc/src/raw_vec/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/raw_vec/mod.rs b/library/alloc/src/raw_vec/mod.rs index b0027e964e467..d42e27cc3b405 100644 --- a/library/alloc/src/raw_vec/mod.rs +++ b/library/alloc/src/raw_vec/mod.rs @@ -334,7 +334,7 @@ impl RawVec { /// A specialized version of `self.reserve(len, 1)` which requires the /// caller to ensure `len == self.capacity()`. #[cfg(not(no_global_oom_handling))] - #[inline(never)] + #[inline] #[track_caller] pub(crate) fn grow_one(&mut self) { self.inner.grow_one(T::LAYOUT)