Skip to content

Commit dd17eb4

Browse files
Dunqingoverlookmotel
authored andcommitted
refactor(allocator/vec2): rename parameters and method name to align with std
1 parent 6a8c2fd commit dd17eb4

File tree

1 file changed

+34
-46
lines changed

1 file changed

+34
-46
lines changed

crates/oxc_allocator/src/vec2/raw_vec.rs

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -315,24 +315,24 @@ impl<'a, T> RawVec<'a, T> {
315315
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
316316
pub fn try_reserve_exact(
317317
&mut self,
318-
used_cap: usize,
319-
needed_extra_cap: usize,
318+
len: usize,
319+
additional: usize,
320320
) -> Result<(), CollectionAllocErr> {
321-
if self.needs_to_grow(used_cap, needed_extra_cap) {
322-
self.reserve_exact_internal(used_cap, needed_extra_cap)?
321+
if self.needs_to_grow(len, additional) {
322+
self.grow_exact(len, additional)?
323323
}
324324

325325
Ok(())
326326
}
327327

328328
/// Ensures that the buffer contains at least enough space to hold
329-
/// `used_cap + needed_extra_cap` elements. If it doesn't already,
329+
/// `len + additional` elements. If it doesn't already,
330330
/// will reallocate the minimum possible amount of memory necessary.
331331
/// Generally this will be exactly the amount of memory necessary,
332332
/// but in principle the allocator is free to give back more than
333333
/// we asked for.
334334
///
335-
/// If `used_cap` exceeds `self.cap()`, this may fail to actually allocate
335+
/// If `len` exceeds `self.cap()`, this may fail to actually allocate
336336
/// the requested space. This is not really unsafe, but the unsafe
337337
/// code *you* write that relies on the behavior of this function may break.
338338
///
@@ -345,48 +345,44 @@ impl<'a, T> RawVec<'a, T> {
345345
/// # Aborts
346346
///
347347
/// Aborts on OOM
348-
pub fn reserve_exact(&mut self, used_cap: usize, needed_extra_cap: usize) {
349-
if let Err(err) = self.try_reserve_exact(used_cap, needed_extra_cap) {
348+
pub fn reserve_exact(&mut self, len: usize, additional: usize) {
349+
if let Err(err) = self.try_reserve_exact(len, additional) {
350350
handle_error(err)
351351
}
352352
}
353353

354-
/// Calculates the buffer's new size given that it'll hold `used_cap +
355-
/// needed_extra_cap` elements. This logic is used in amortized reserve methods.
354+
/// Calculates the buffer's new size given that it'll hold `len +
355+
/// additional` elements. This logic is used in amortized reserve methods.
356356
/// Returns `(new_capacity, new_alloc_size)`.
357357
fn amortized_new_size(
358358
&self,
359-
used_cap: usize,
360-
needed_extra_cap: usize,
359+
len: usize,
360+
additional: usize,
361361
) -> Result<usize, CollectionAllocErr> {
362362
// Nothing we can really do about these checks :(
363-
let required_cap = used_cap.checked_add(needed_extra_cap).ok_or(CapacityOverflow)?;
363+
let required_cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
364364
// Cannot overflow, because `cap <= isize::MAX`, and type of `cap` is `usize`.
365365
let double_cap = self.cap * 2;
366366
// `double_cap` guarantees exponential growth.
367367
Ok(cmp::max(double_cap, required_cap))
368368
}
369369

370370
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
371-
pub fn try_reserve(
372-
&mut self,
373-
used_cap: usize,
374-
needed_extra_cap: usize,
375-
) -> Result<(), CollectionAllocErr> {
376-
if self.needs_to_grow(used_cap, needed_extra_cap) {
377-
self.reserve_amortized_internal(used_cap, needed_extra_cap)?;
371+
pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), CollectionAllocErr> {
372+
if self.needs_to_grow(len, additional) {
373+
self.grow_amortized(len, additional)?;
378374
}
379375

380376
Ok(())
381377
}
382378

383379
/// Ensures that the buffer contains at least enough space to hold
384-
/// `used_cap + needed_extra_cap` elements. If it doesn't already have
380+
/// `len + additional` elements. If it doesn't already have
385381
/// enough capacity, will reallocate enough space plus comfortable slack
386382
/// space to get amortized `O(1)` behavior. Will limit this behavior
387383
/// if it would needlessly cause itself to panic.
388384
///
389-
/// If `used_cap` exceeds `self.cap()`, this may fail to actually allocate
385+
/// If `len` exceeds `self.cap()`, this may fail to actually allocate
390386
/// the requested space. This is not really unsafe, but the unsafe
391387
/// code *you* write that relies on the behavior of this function may break.
392388
///
@@ -433,20 +429,20 @@ impl<'a, T> RawVec<'a, T> {
433429
/// # }
434430
/// ```
435431
#[inline]
436-
pub fn reserve(&mut self, used_cap: usize, need_extra_cap: usize) {
437-
if let Err(err) = self.try_reserve(used_cap, need_extra_cap) {
432+
pub fn reserve(&mut self, len: usize, additional: usize) {
433+
if let Err(err) = self.try_reserve(len, additional) {
438434
handle_error(err)
439435
}
440436
}
441437

442438
/*
443439
/// Attempts to ensure that the buffer contains at least enough space to hold
444-
/// `used_cap + needed_extra_cap` elements. If it doesn't already have
440+
/// `len + additional` elements. If it doesn't already have
445441
/// enough capacity, will reallocate in place enough space plus comfortable slack
446442
/// space to get amortized `O(1)` behavior. Will limit this behaviour
447443
/// if it would needlessly cause itself to panic.
448444
///
449-
/// If `used_cap` exceeds `self.cap()`, this may fail to actually allocate
445+
/// If `len` exceeds `self.cap()`, this may fail to actually allocate
450446
/// the requested space. This is not really unsafe, but the unsafe
451447
/// code *you* write that relies on the behavior of this function may break.
452448
///
@@ -457,7 +453,7 @@ impl<'a, T> RawVec<'a, T> {
457453
/// * Panics if the requested capacity exceeds `usize::MAX` bytes.
458454
/// * Panics on 32-bit platforms if the requested capacity exceeds
459455
/// `isize::MAX` bytes.
460-
pub fn reserve_in_place(&mut self, used_cap: usize, needed_extra_cap: usize) -> bool {
456+
pub fn reserve_in_place(&mut self, len: usize, additional: usize) -> bool {
461457
unsafe {
462458
// NOTE: we don't early branch on ZSTs here because we want this
463459
// to actually catch "asking for more than usize::MAX" in that case.
@@ -466,21 +462,21 @@ impl<'a, T> RawVec<'a, T> {
466462
467463
// Don't actually need any more capacity. If the current `cap` is 0, we can't
468464
// reallocate in place.
469-
// Wrapping in case they give a bad `used_cap`
465+
// Wrapping in case they give a bad `len`
470466
let old_layout = match self.current_layout() {
471467
Some(layout) => layout,
472468
None => return false,
473469
};
474-
if self.cap().wrapping_sub(used_cap) >= needed_extra_cap {
470+
if self.cap().wrapping_sub(len) >= additional {
475471
return false;
476472
}
477473
478474
let new_cap = self
479-
.amortized_new_size(used_cap, needed_extra_cap)
475+
.amortized_new_size(len, additional)
480476
.unwrap_or_else(|_| capacity_overflow());
481477
482-
// Here, `cap < used_cap + needed_extra_cap <= new_cap`
483-
// (regardless of whether `self.cap - used_cap` wrapped).
478+
// Here, `cap < len + additional <= new_cap`
479+
// (regardless of whether `self.cap - len` wrapped).
484480
// Therefore we can safely call grow_in_place.
485481
486482
let new_layout = Layout::new::<T>().repeat(new_cap).unwrap().0;
@@ -584,24 +580,20 @@ impl<'a, T> RawVec<'a, T> {
584580

585581
impl<'a, T> RawVec<'a, T> {
586582
#[inline]
587-
fn needs_to_grow(&self, used_cap: usize, needed_extra_cap: usize) -> bool {
588-
needed_extra_cap > self.cap().wrapping_sub(used_cap)
583+
fn needs_to_grow(&self, len: usize, additional: usize) -> bool {
584+
additional > self.cap().wrapping_sub(len)
589585
}
590586

591587
/// Helper method to reserve additional space, reallocating the backing memory.
592588
/// The caller is responsible for confirming that there is not already enough space available.
593-
fn reserve_exact_internal(
594-
&mut self,
595-
used_cap: usize,
596-
needed_extra_cap: usize,
597-
) -> Result<(), CollectionAllocErr> {
589+
fn grow_exact(&mut self, len: usize, additional: usize) -> Result<(), CollectionAllocErr> {
598590
unsafe {
599591
// NOTE: we don't early branch on ZSTs here because we want this
600592
// to actually catch "asking for more than usize::MAX" in that case.
601593
// If we make it past the first branch then we are guaranteed to
602594
// panic.
603595

604-
let new_cap = used_cap.checked_add(needed_extra_cap).ok_or(CapacityOverflow)?;
596+
let new_cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
605597
let new_layout = Layout::array::<T>(new_cap).map_err(|_| CapacityOverflow)?;
606598

607599
self.ptr = self.finish_grow(new_layout)?.cast();
@@ -614,18 +606,14 @@ impl<'a, T> RawVec<'a, T> {
614606

615607
/// Helper method to reserve additional space, reallocating the backing memory.
616608
/// The caller is responsible for confirming that there is not already enough space available.
617-
fn reserve_amortized_internal(
618-
&mut self,
619-
used_cap: usize,
620-
needed_extra_cap: usize,
621-
) -> Result<(), CollectionAllocErr> {
609+
fn grow_amortized(&mut self, len: usize, additional: usize) -> Result<(), CollectionAllocErr> {
622610
unsafe {
623611
// NOTE: we don't early branch on ZSTs here because we want this
624612
// to actually catch "asking for more than usize::MAX" in that case.
625613
// If we make it past the first branch then we are guaranteed to
626614
// panic.
627615

628-
let new_cap = self.amortized_new_size(used_cap, needed_extra_cap)?;
616+
let new_cap = self.amortized_new_size(len, additional)?;
629617
let new_layout = Layout::array::<T>(new_cap).map_err(|_| CapacityOverflow)?;
630618

631619
self.ptr = self.finish_grow(new_layout)?.cast();

0 commit comments

Comments
 (0)