Skip to content

Commit 04e0390

Browse files
Dunqingoverlookmotel
authored andcommitted
perf(allocator/vec2): replace self.reserve(1) calls with self.grow_one() for better efficiency (oxc-project#9856)
The places calling `self.reserve(1)` already checked `len == self.buf.cap()`, in order to avoid checking again in `reserve`, we should call `grow_one` instead which is a shortcut of `grow_amortized(len, 1)`.
1 parent 7f2f247 commit 04e0390

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

crates/oxc_allocator/src/vec2/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ impl<'bump, T: 'bump> Vec<'bump, T> {
12371237

12381238
// space for the new element
12391239
if len == self.buf.cap() {
1240-
self.reserve(1);
1240+
self.buf.grow_one();
12411241
}
12421242

12431243
unsafe {
@@ -1565,7 +1565,7 @@ impl<'bump, T: 'bump> Vec<'bump, T> {
15651565
// This will panic or abort if we would allocate > isize::MAX bytes
15661566
// or if the length increment would overflow for zero-sized types.
15671567
if self.len == self.buf.cap() {
1568-
self.reserve(1);
1568+
self.buf.grow_one();
15691569
}
15701570
unsafe {
15711571
let end = self.buf.ptr().add(self.len);

crates/oxc_allocator/src/vec2/raw_vec.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,10 @@ impl<'a, T> RawVec<'a, T> {
437437

438438
/// A specialized version of `self.reserve(len, 1)` which requires the
439439
/// caller to ensure `len == self.capacity()`.
440-
#[inline(never)]
440+
//
441+
// Unlike standard library implementation marked as `#[inline(never)]`, we need to
442+
// mark as `#[inline]` because this function is common case in the oxc_parser.
443+
#[inline]
441444
pub fn grow_one(&mut self) {
442445
if let Err(err) = self.grow_amortized(self.cap, 1) {
443446
handle_error(err);

0 commit comments

Comments
 (0)