Skip to content

Commit 3cd3d23

Browse files
Dunqingoverlookmotel
authored andcommitted
feat(allocator/vec2): align RawVec::reserve with standard library implementation (oxc-project#10701)
Align with https://doc.rust-lang.org/src/alloc/raw_vec.rs.html#542-560, I saw performance shows a `0.1%` performance improvement.
1 parent 04e0390 commit 3cd3d23

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

crates/oxc_allocator/src/vec2/raw_vec.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,19 @@ impl<'a, T> RawVec<'a, T> {
430430
/// ```
431431
#[inline]
432432
pub fn reserve(&mut self, len: usize, additional: usize) {
433-
if let Err(err) = self.try_reserve(len, additional) {
434-
handle_error(err)
433+
// Callers expect this function to be very cheap when there is already sufficient capacity.
434+
// Therefore, we move all the resizing and error-handling logic from grow_amortized and
435+
// handle_reserve behind a call, while making sure that this function is likely to be
436+
// inlined as just a comparison and a call if the comparison fails.
437+
#[cold]
438+
fn do_reserve_and_handle<T>(slf: &mut RawVec<T>, len: usize, additional: usize) {
439+
if let Err(err) = slf.grow_amortized(len, additional) {
440+
handle_error(err);
441+
}
442+
}
443+
444+
if self.needs_to_grow(len, additional) {
445+
do_reserve_and_handle(self, len, additional);
435446
}
436447
}
437448

0 commit comments

Comments
 (0)