Skip to content

Commit

Permalink
vec: optimize for size a bit by preventing monomorphizing extend_from…
Browse files Browse the repository at this point in the history
…_slice.
  • Loading branch information
Dirbaio committed Jun 28, 2024
1 parent e9f8bac commit 400732b
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,27 @@ impl<T, S: Storage> VecInner<T, S> {
where
T: Clone,
{
if self.len + other.len() > self.capacity() {
// won't fit in the `Vec`; don't modify anything and return an error
Err(())
} else {
for elem in other {
unsafe {
self.push_unchecked(elem.clone());
pub fn extend_from_slice_inner<T>(
len: &mut usize,
buf: &mut [MaybeUninit<T>],
other: &[T],
) -> Result<(), ()>
where
T: Clone,
{
if *len + other.len() > buf.len() {
// won't fit in the `Vec`; don't modify anything and return an error
Err(())
} else {
for elem in other {
unsafe { *buf.get_unchecked_mut(*len) = MaybeUninit::new(elem.clone()) }
*len += 1;
}
Ok(())
}
Ok(())
}

extend_from_slice_inner(&mut self.len, self.buffer.borrow_mut(), other)
}

/// Removes the last element from a vector and returns it, or `None` if it's empty
Expand Down

0 comments on commit 400732b

Please sign in to comment.