Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ecovec![] macro #29

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ use crate::sync::atomic::{self, AtomicUsize, Ordering::*};
macro_rules! eco_vec {
() => { $crate::EcoVec::new() };
($elem:expr; $n:expr) => { $crate::EcoVec::from_elem($elem, $n) };
($($value:expr),+ $(,)?) => {{
let capacity = 0 $(+ $crate::eco_vec!(@count $value))*;
let mut vec = $crate::EcoVec::with_capacity(capacity);
$(vec.push($value);)*
vec
}};
(@count $value:expr) => { 1 };
($($value:expr),+ $(,)?) => { $crate::EcoVec::from([$($value),+]) };
}

/// An economical vector with clone-on-write semantics.
Expand Down Expand Up @@ -214,7 +208,8 @@ impl<T: Clone> EcoVec<T> {
pub fn from_elem(value: T, n: usize) -> Self {
let mut vec = Self::with_capacity(n);
for _ in 0..n {
vec.push(value.clone());
// Safety: we just called `EcoVec::with_capacity()`
unsafe { vec.push_unchecked(value.clone()) }
}
vec
}
Expand All @@ -241,12 +236,27 @@ impl<T: Clone> EcoVec<T> {
// Ensure unique ownership and grow the vector if necessary.
self.reserve((self.len == self.capacity()) as usize);

// Safety: we just called `EcoVec::reserve()`
unsafe { self.push_unchecked(value) }
}

/// Add a value at the end of the vector, without reallocating.
///
/// # Safety
/// Caller must ensure that `self.len < self.capacity()` and
/// `self.is_unique()` hold, such as by calling `EcoVec::reserve()`, or
/// `EcoVec::with_capacity()`.
#[inline]
unsafe fn push_unchecked(&mut self, value: T) {
debug_assert!(self.len < self.capacity());
debug_assert!(self.is_unique());

unsafe {
// Safety:
// - The reference count is `1` because of `reserve`.
// - Caller must ensure that reference count is `1`.
// - The pointer returned by `data_mut()` is valid for `capacity`
// writes.
// - Due to the `reserve` call, `len < capacity`.
// - Caller must ensure that `len < capacity`.
// - Thus, `data_mut() + len` is valid for one write.
ptr::write(self.data_mut().add(self.len), value);

Expand Down