Skip to content

Try 1.5x as a vec growth factor #108718

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,12 @@ impl<T, A: Allocator> RawVec<T, A> {
// Nothing we can really do about these checks, sadly.
let required_cap = len.checked_add(additional).ok_or(CapacityOverflow)?;

// This guarantees exponential growth. The doubling cannot overflow
// because `cap <= isize::MAX` and the type of `cap` is `usize`.
let cap = cmp::max(self.cap * 2, required_cap);
// Increase the capacity by 1.5x each time (exponential growth). The
// `(self.cap <= 1) as usize` is to ensure in the case that self.cap is
// 1, we still grow by 1 (without introducing an additional branch on
// most processors).
let next_cap = self.cap + (self.cap / 2) + (self.cap <= 1) as usize;
let cap = cmp::max(next_cap, required_cap);
let cap = cmp::max(Self::MIN_NON_ZERO_CAP, cap);

let new_layout = Layout::array::<T>(cap);
Expand Down
1 change: 1 addition & 0 deletions library/alloc/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,7 @@ fn test_iter_zero_sized() {
}

#[test]
#[cfg(any())] // FIXME(thom) update this test if the perf run says to.
fn test_shrink_to_fit() {
let mut xs = vec![0, 1, 2, 3];
for i in 4..100 {
Expand Down
1 change: 1 addition & 0 deletions library/alloc/tests/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,7 @@ fn vec_macro_repeating_null_raw_fat_pointer() {
// This test will likely fail if you change the capacities used in
// `RawVec::grow_amortized`.
#[test]
#[cfg(any())] // FIXME(thom) update this test if the perf run says it's good
fn test_push_growth_strategy() {
// If the element size is 1, we jump from 0 to 8, then double.
{
Expand Down