Rename some identifiers in `RawVec` and `libarena`.
- Use `len` more consistently for the number of elements in a vector, because that's the usual name. - Use `additional` more consistently for the number of elements we want to add, because that's what `Vec::reserve()` uses. - Use `cap` consistently rather than `capacity`. - Plus a few other tweaks. This increases consistency and conciseness.
This commit is contained in:
parent
cb8bc8e05d
commit
c9cbe7e7eb
|
@ -235,13 +235,13 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ensures that the buffer contains at least enough space to hold
|
/// Ensures that the buffer contains at least enough space to hold `len +
|
||||||
/// `used_capacity + needed_extra_capacity` elements. If it doesn't already have
|
/// additional` elements. If it doesn't already have enough capacity, will
|
||||||
/// enough capacity, will reallocate enough space plus comfortable slack
|
/// reallocate enough space plus comfortable slack space to get amortized
|
||||||
/// space to get amortized `O(1)` behavior. Will limit this behavior
|
/// `O(1)` behavior. Will limit this behavior if it would needlessly cause
|
||||||
/// if it would needlessly cause itself to panic.
|
/// itself to panic.
|
||||||
///
|
///
|
||||||
/// If `used_capacity` exceeds `self.capacity()`, this may fail to actually allocate
|
/// If `len` exceeds `self.capacity()`, this may fail to actually allocate
|
||||||
/// the requested space. This is not really unsafe, but the unsafe
|
/// the requested space. This is not really unsafe, but the unsafe
|
||||||
/// code *you* write that relies on the behavior of this function may break.
|
/// code *you* write that relies on the behavior of this function may break.
|
||||||
///
|
///
|
||||||
|
@ -287,8 +287,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
/// # vector.push_all(&[1, 3, 5, 7, 9]);
|
/// # vector.push_all(&[1, 3, 5, 7, 9]);
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn reserve(&mut self, used_capacity: usize, needed_extra_capacity: usize) {
|
pub fn reserve(&mut self, len: usize, additional: usize) {
|
||||||
match self.try_reserve(used_capacity, needed_extra_capacity) {
|
match self.try_reserve(len, additional) {
|
||||||
Err(CapacityOverflow) => capacity_overflow(),
|
Err(CapacityOverflow) => capacity_overflow(),
|
||||||
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
|
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
|
||||||
Ok(()) => { /* yay */ }
|
Ok(()) => { /* yay */ }
|
||||||
|
@ -296,28 +296,23 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
|
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
|
||||||
pub fn try_reserve(
|
pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
|
||||||
&mut self,
|
if self.needs_to_grow(len, additional) {
|
||||||
used_capacity: usize,
|
self.grow_amortized(len, additional)
|
||||||
needed_extra_capacity: usize,
|
|
||||||
) -> Result<(), TryReserveError> {
|
|
||||||
if self.needs_to_grow(used_capacity, needed_extra_capacity) {
|
|
||||||
self.grow_amortized(used_capacity, needed_extra_capacity)
|
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ensures that the buffer contains at least enough space to hold
|
/// Ensures that the buffer contains at least enough space to hold `len +
|
||||||
/// `used_capacity + needed_extra_capacity` elements. If it doesn't already,
|
/// additional` elements. If it doesn't already, will reallocate the
|
||||||
/// will reallocate the minimum possible amount of memory necessary.
|
/// minimum possible amount of memory necessary. Generally this will be
|
||||||
/// Generally this will be exactly the amount of memory necessary,
|
/// exactly the amount of memory necessary, but in principle the allocator
|
||||||
/// but in principle the allocator is free to give back more than what
|
/// is free to give back more than we asked for.
|
||||||
/// we asked for.
|
|
||||||
///
|
///
|
||||||
/// If `used_capacity` exceeds `self.capacity()`, this may fail to actually allocate
|
/// If `len` exceeds `self.capacity()`, this may fail to actually allocate
|
||||||
/// the requested space. This is not really unsafe, but the unsafe
|
/// the requested space. This is not really unsafe, but the unsafe code
|
||||||
/// code *you* write that relies on the behavior of this function may break.
|
/// *you* write that relies on the behavior of this function may break.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
|
@ -328,8 +323,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
/// # Aborts
|
/// # Aborts
|
||||||
///
|
///
|
||||||
/// Aborts on OOM.
|
/// Aborts on OOM.
|
||||||
pub fn reserve_exact(&mut self, used_capacity: usize, needed_extra_capacity: usize) {
|
pub fn reserve_exact(&mut self, len: usize, additional: usize) {
|
||||||
match self.try_reserve_exact(used_capacity, needed_extra_capacity) {
|
match self.try_reserve_exact(len, additional) {
|
||||||
Err(CapacityOverflow) => capacity_overflow(),
|
Err(CapacityOverflow) => capacity_overflow(),
|
||||||
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
|
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
|
||||||
Ok(()) => { /* yay */ }
|
Ok(()) => { /* yay */ }
|
||||||
|
@ -339,14 +334,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
|
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
|
||||||
pub fn try_reserve_exact(
|
pub fn try_reserve_exact(
|
||||||
&mut self,
|
&mut self,
|
||||||
used_capacity: usize,
|
len: usize,
|
||||||
needed_extra_capacity: usize,
|
additional: usize,
|
||||||
) -> Result<(), TryReserveError> {
|
) -> Result<(), TryReserveError> {
|
||||||
if self.needs_to_grow(used_capacity, needed_extra_capacity) {
|
if self.needs_to_grow(len, additional) { self.grow_exact(len, additional) } else { Ok(()) }
|
||||||
self.grow_exact(used_capacity, needed_extra_capacity)
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shrinks the allocation down to the specified amount. If the given amount
|
/// Shrinks the allocation down to the specified amount. If the given amount
|
||||||
|
@ -371,8 +362,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
impl<T, A: AllocRef> RawVec<T, A> {
|
impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
|
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
|
||||||
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
|
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
|
||||||
fn needs_to_grow(&self, used_capacity: usize, needed_extra_capacity: usize) -> bool {
|
fn needs_to_grow(&self, len: usize, additional: usize) -> bool {
|
||||||
needed_extra_capacity > self.capacity().wrapping_sub(used_capacity)
|
additional > self.capacity().wrapping_sub(len)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn capacity_from_bytes(excess: usize) -> usize {
|
fn capacity_from_bytes(excess: usize) -> usize {
|
||||||
|
@ -392,13 +383,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
// so that all of the code that depends on `T` is within it, while as much
|
// so that all of the code that depends on `T` is within it, while as much
|
||||||
// of the code that doesn't depend on `T` as possible is in functions that
|
// of the code that doesn't depend on `T` as possible is in functions that
|
||||||
// are non-generic over `T`.
|
// are non-generic over `T`.
|
||||||
fn grow_amortized(
|
fn grow_amortized(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
|
||||||
&mut self,
|
|
||||||
used_capacity: usize,
|
|
||||||
needed_extra_capacity: usize,
|
|
||||||
) -> Result<(), TryReserveError> {
|
|
||||||
// This is ensured by the calling contexts.
|
// This is ensured by the calling contexts.
|
||||||
debug_assert!(needed_extra_capacity > 0);
|
debug_assert!(additional > 0);
|
||||||
|
|
||||||
if mem::size_of::<T>() == 0 {
|
if mem::size_of::<T>() == 0 {
|
||||||
// Since we return a capacity of `usize::MAX` when `elem_size` is
|
// Since we return a capacity of `usize::MAX` when `elem_size` is
|
||||||
// 0, getting to here necessarily means the `RawVec` is overfull.
|
// 0, getting to here necessarily means the `RawVec` is overfull.
|
||||||
|
@ -406,8 +394,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nothing we can really do about these checks, sadly.
|
// Nothing we can really do about these checks, sadly.
|
||||||
let required_cap =
|
let required_cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
|
||||||
used_capacity.checked_add(needed_extra_capacity).ok_or(CapacityOverflow)?;
|
|
||||||
|
|
||||||
// This guarantees exponential growth. The doubling cannot overflow
|
// This guarantees exponential growth. The doubling cannot overflow
|
||||||
// because `cap <= isize::MAX` and the type of `cap` is `usize`.
|
// because `cap <= isize::MAX` and the type of `cap` is `usize`.
|
||||||
|
@ -440,18 +427,14 @@ impl<T, A: AllocRef> RawVec<T, A> {
|
||||||
// The constraints on this method are much the same as those on
|
// The constraints on this method are much the same as those on
|
||||||
// `grow_amortized`, but this method is usually instantiated less often so
|
// `grow_amortized`, but this method is usually instantiated less often so
|
||||||
// it's less critical.
|
// it's less critical.
|
||||||
fn grow_exact(
|
fn grow_exact(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
|
||||||
&mut self,
|
|
||||||
used_capacity: usize,
|
|
||||||
needed_extra_capacity: usize,
|
|
||||||
) -> Result<(), TryReserveError> {
|
|
||||||
if mem::size_of::<T>() == 0 {
|
if mem::size_of::<T>() == 0 {
|
||||||
// Since we return a capacity of `usize::MAX` when the type size is
|
// Since we return a capacity of `usize::MAX` when the type size is
|
||||||
// 0, getting to here necessarily means the `RawVec` is overfull.
|
// 0, getting to here necessarily means the `RawVec` is overfull.
|
||||||
return Err(CapacityOverflow);
|
return Err(CapacityOverflow);
|
||||||
}
|
}
|
||||||
|
|
||||||
let cap = used_capacity.checked_add(needed_extra_capacity).ok_or(CapacityOverflow)?;
|
let cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
|
||||||
let new_layout = Layout::array::<T>(cap);
|
let new_layout = Layout::array::<T>(cap);
|
||||||
|
|
||||||
// `finish_grow` is non-generic over `T`.
|
// `finish_grow` is non-generic over `T`.
|
||||||
|
|
|
@ -2977,12 +2977,12 @@ impl<T> Drain<'_, T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Makes room for inserting more elements before the tail.
|
/// Makes room for inserting more elements before the tail.
|
||||||
unsafe fn move_tail(&mut self, extra_capacity: usize) {
|
unsafe fn move_tail(&mut self, additional: usize) {
|
||||||
let vec = self.vec.as_mut();
|
let vec = self.vec.as_mut();
|
||||||
let used_capacity = self.tail_start + self.tail_len;
|
let len = self.tail_start + self.tail_len;
|
||||||
vec.buf.reserve(used_capacity, extra_capacity);
|
vec.buf.reserve(len, additional);
|
||||||
|
|
||||||
let new_tail_start = self.tail_start + extra_capacity;
|
let new_tail_start = self.tail_start + additional;
|
||||||
let src = vec.as_ptr().add(self.tail_start);
|
let src = vec.as_ptr().add(self.tail_start);
|
||||||
let dst = vec.as_mut_ptr().add(new_tail_start);
|
let dst = vec.as_mut_ptr().add(new_tail_start);
|
||||||
ptr::copy(src, dst, self.tail_len);
|
ptr::copy(src, dst, self.tail_len);
|
||||||
|
|
|
@ -146,18 +146,18 @@ impl<T> TypedArena<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn can_allocate(&self, len: usize) -> bool {
|
fn can_allocate(&self, additional: usize) -> bool {
|
||||||
let available_capacity_bytes = self.end.get() as usize - self.ptr.get() as usize;
|
let available_bytes = self.end.get() as usize - self.ptr.get() as usize;
|
||||||
let at_least_bytes = len.checked_mul(mem::size_of::<T>()).unwrap();
|
let additional_bytes = additional.checked_mul(mem::size_of::<T>()).unwrap();
|
||||||
available_capacity_bytes >= at_least_bytes
|
available_bytes >= additional_bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ensures there's enough space in the current chunk to fit `len` objects.
|
/// Ensures there's enough space in the current chunk to fit `len` objects.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn ensure_capacity(&self, len: usize) {
|
fn ensure_capacity(&self, additional: usize) {
|
||||||
if !self.can_allocate(len) {
|
if !self.can_allocate(additional) {
|
||||||
self.grow(len);
|
self.grow(additional);
|
||||||
debug_assert!(self.can_allocate(len));
|
debug_assert!(self.can_allocate(additional));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,13 +214,13 @@ impl<T> TypedArena<T> {
|
||||||
/// Grows the arena.
|
/// Grows the arena.
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
#[cold]
|
#[cold]
|
||||||
fn grow(&self, n: usize) {
|
fn grow(&self, additional: usize) {
|
||||||
unsafe {
|
unsafe {
|
||||||
// We need the element size to convert chunk sizes (ranging from
|
// We need the element size to convert chunk sizes (ranging from
|
||||||
// PAGE to HUGE_PAGE bytes) to element counts.
|
// PAGE to HUGE_PAGE bytes) to element counts.
|
||||||
let elem_size = cmp::max(1, mem::size_of::<T>());
|
let elem_size = cmp::max(1, mem::size_of::<T>());
|
||||||
let mut chunks = self.chunks.borrow_mut();
|
let mut chunks = self.chunks.borrow_mut();
|
||||||
let mut new_capacity;
|
let mut new_cap;
|
||||||
if let Some(last_chunk) = chunks.last_mut() {
|
if let Some(last_chunk) = chunks.last_mut() {
|
||||||
let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
|
let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
|
||||||
last_chunk.entries = used_bytes / mem::size_of::<T>();
|
last_chunk.entries = used_bytes / mem::size_of::<T>();
|
||||||
|
@ -228,17 +228,17 @@ impl<T> TypedArena<T> {
|
||||||
// If the previous chunk's capacity is less than HUGE_PAGE
|
// If the previous chunk's capacity is less than HUGE_PAGE
|
||||||
// bytes, then this chunk will be least double the previous
|
// bytes, then this chunk will be least double the previous
|
||||||
// chunk's size.
|
// chunk's size.
|
||||||
new_capacity = last_chunk.storage.capacity();
|
new_cap = last_chunk.storage.capacity();
|
||||||
if new_capacity < HUGE_PAGE / elem_size {
|
if new_cap < HUGE_PAGE / elem_size {
|
||||||
new_capacity = new_capacity.checked_mul(2).unwrap();
|
new_cap = new_cap.checked_mul(2).unwrap();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
new_capacity = PAGE / elem_size;
|
new_cap = PAGE / elem_size;
|
||||||
}
|
}
|
||||||
// Also ensure that this chunk can fit `n`.
|
// Also ensure that this chunk can fit `additional`.
|
||||||
new_capacity = cmp::max(n, new_capacity);
|
new_cap = cmp::max(additional, new_cap);
|
||||||
|
|
||||||
let chunk = TypedArenaChunk::<T>::new(new_capacity);
|
let chunk = TypedArenaChunk::<T>::new(new_cap);
|
||||||
self.ptr.set(chunk.start());
|
self.ptr.set(chunk.start());
|
||||||
self.end.set(chunk.end());
|
self.end.set(chunk.end());
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
|
@ -342,10 +342,10 @@ impl DroplessArena {
|
||||||
|
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
#[cold]
|
#[cold]
|
||||||
fn grow(&self, needed_bytes: usize) {
|
fn grow(&self, additional: usize) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut chunks = self.chunks.borrow_mut();
|
let mut chunks = self.chunks.borrow_mut();
|
||||||
let mut new_capacity;
|
let mut new_cap;
|
||||||
if let Some(last_chunk) = chunks.last_mut() {
|
if let Some(last_chunk) = chunks.last_mut() {
|
||||||
// There is no need to update `last_chunk.entries` because that
|
// There is no need to update `last_chunk.entries` because that
|
||||||
// field isn't used by `DroplessArena`.
|
// field isn't used by `DroplessArena`.
|
||||||
|
@ -353,17 +353,17 @@ impl DroplessArena {
|
||||||
// If the previous chunk's capacity is less than HUGE_PAGE
|
// If the previous chunk's capacity is less than HUGE_PAGE
|
||||||
// bytes, then this chunk will be least double the previous
|
// bytes, then this chunk will be least double the previous
|
||||||
// chunk's size.
|
// chunk's size.
|
||||||
new_capacity = last_chunk.storage.capacity();
|
new_cap = last_chunk.storage.capacity();
|
||||||
if new_capacity < HUGE_PAGE {
|
if new_cap < HUGE_PAGE {
|
||||||
new_capacity = new_capacity.checked_mul(2).unwrap();
|
new_cap = new_cap.checked_mul(2).unwrap();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
new_capacity = PAGE;
|
new_cap = PAGE;
|
||||||
}
|
}
|
||||||
// Also ensure that this chunk can fit `needed_bytes`.
|
// Also ensure that this chunk can fit `additional`.
|
||||||
new_capacity = cmp::max(needed_bytes, new_capacity);
|
new_cap = cmp::max(additional, new_cap);
|
||||||
|
|
||||||
let chunk = TypedArenaChunk::<u8>::new(new_capacity);
|
let chunk = TypedArenaChunk::<u8>::new(new_cap);
|
||||||
self.ptr.set(chunk.start());
|
self.ptr.set(chunk.start());
|
||||||
self.end.set(chunk.end());
|
self.end.set(chunk.end());
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
|
|
Loading…
Reference in New Issue