Skip to content

Commit

Permalink
perf: use regular mul instead of checked_mul when doubling capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
bluurryy committed Oct 29, 2024
1 parent ae0c8eb commit 6cb6d1b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/bump_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,8 +1616,9 @@ where
return Ok(());
}

let new_cap = self.capacity().checked_mul(2).unwrap_or(required_cap).max(required_cap);
let new_cap = new_cap.max(min_non_zero_cap(T::SIZE));
// This guarantees exponential growth. The doubling cannot overflow
// because `capacity <= isize::MAX` and the type of `capacity` is usize;
let new_cap = (self.capacity() * 2).max(required_cap).max(min_non_zero_cap(T::SIZE));

self.generic_grow_to(new_cap)
}
Expand Down
5 changes: 3 additions & 2 deletions src/mut_bump_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,8 +1309,9 @@ where
return Ok(());
}

let new_cap = self.capacity().checked_mul(2).unwrap_or(required_cap).max(required_cap);
let new_cap = new_cap.max(min_non_zero_cap(T::SIZE));
// This guarantees exponential growth. The doubling cannot overflow
// because `capacity <= isize::MAX` and the type of `capacity` is usize;
let new_cap = (self.capacity() * 2).max(required_cap).max(min_non_zero_cap(T::SIZE));

unsafe { self.generic_grow_to(new_cap) }
}
Expand Down
5 changes: 3 additions & 2 deletions src/mut_bump_vec_rev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,8 +1334,9 @@ where
return Ok(());
}

let new_cap = self.capacity().checked_mul(2).unwrap_or(required_cap).max(required_cap);
let new_cap = new_cap.max(min_non_zero_cap(T::SIZE));
// This guarantees exponential growth. The doubling cannot overflow
// because `capacity <= isize::MAX` and the type of `capacity` is usize;
let new_cap = (self.capacity() * 2).max(required_cap).max(min_non_zero_cap(T::SIZE));

unsafe { self.generic_grow_to(new_cap) }
}
Expand Down

0 comments on commit 6cb6d1b

Please sign in to comment.