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 more for the empty case #34

Merged
merged 5 commits into from
Aug 23, 2022
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
101 changes: 78 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,12 +935,6 @@ impl<T> ThinVec<T> {
}
}

unsafe fn deallocate(&mut self) {
if self.has_allocation() {
dealloc(self.ptr() as *mut u8, layout::<T>(self.capacity()))
}
}

/// Resize the buffer and update its capacity, without changing the length.
/// Unsafe because it can cause length to be greater than capacity.
unsafe fn reallocate(&mut self, new_cap: usize) {
Expand Down Expand Up @@ -1078,10 +1072,25 @@ impl<T: PartialEq> ThinVec<T> {
}

impl<T> Drop for ThinVec<T> {
#[inline]
fn drop(&mut self) {
unsafe {
ptr::drop_in_place(&mut self[..]);
self.deallocate();
#[cold]
#[inline(never)]
fn drop_non_singleton<T>(this: &mut ThinVec<T>) {
unsafe {
ptr::drop_in_place(&mut this[..]);

#[cfg(feature = "gecko-ffi")]
if this.ptr.as_ref().uses_stack_allocated_buffer() {
return;
}

dealloc(this.ptr() as *mut u8, layout::<T>(this.capacity()))
}
}

if !self.is_singleton() {
drop_non_singleton(self);
}
}
}
Expand Down Expand Up @@ -1125,7 +1134,10 @@ impl<T> Extend<T> for ThinVec<T> {
I: IntoIterator<Item = T>,
{
let iter = iter.into_iter();
self.reserve(iter.size_hint().0);
let hint = iter.size_hint().0;
if hint > 0 {
self.reserve(hint);
}
for x in iter {
self.push(x);
}
Expand Down Expand Up @@ -1318,20 +1330,33 @@ impl<T> Clone for ThinVec<T>
where
T: Clone,
{
#[inline]
fn clone(&self) -> ThinVec<T> {
let len = self.len();
let mut new_vec = ThinVec::<T>::with_capacity(len);
let mut data_raw = new_vec.data_raw();
for x in self.iter() {
#[cold]
#[inline(never)]
fn clone_non_singleton<T: Clone>(this: &ThinVec<T>) -> ThinVec<T> {
let len = this.len();
let mut new_vec = ThinVec::<T>::with_capacity(len);
let mut data_raw = new_vec.data_raw();
for x in this.iter() {
unsafe {
ptr::write(data_raw, x.clone());
data_raw = data_raw.add(1);
}
}
unsafe {
ptr::write(data_raw, x.clone());
data_raw = data_raw.add(1);
// `this` is not the singleton, but `new_vec` will be if
// `this` is empty.
new_vec.set_len(len); // could be the singleton
nnethercote marked this conversation as resolved.
Show resolved Hide resolved
}
new_vec
}
unsafe {
new_vec.set_len(len); // could be the singleton

if self.is_singleton() {
ThinVec::new()
} else {
clone_non_singleton(self)
}
new_vec
}
}

Expand Down Expand Up @@ -1394,11 +1419,20 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
}

impl<T> Drop for IntoIter<T> {
#[inline]
fn drop(&mut self) {
unsafe {
let mut vec = mem::replace(&mut self.vec, ThinVec::new());
ptr::drop_in_place(&mut vec[self.start..]);
vec.set_len(0) // could be the singleton
#[cold]
#[inline(never)]
fn drop_non_singleton<T>(this: &mut IntoIter<T>) {
unsafe {
let mut vec = mem::replace(&mut this.vec, ThinVec::new());
ptr::drop_in_place(&mut vec[this.start..]);
vec.set_len_non_singleton(0)
}
}

if !self.vec.is_singleton() {
drop_non_singleton(self);
}
}
}
Expand Down Expand Up @@ -1795,6 +1829,27 @@ mod tests {
assert_eq!(v.capacity(), 0);
assert_eq!(&v[..], &[]);
}

{
let v = ThinVec::<i32>::new();
let v = v.clone();

assert_eq!(v.len(), 0);
assert_eq!(v.capacity(), 0);
assert_eq!(&v[..], &[]);
}
}

#[test]
fn test_clone() {
let mut v = ThinVec::<i32>::new();
assert!(v.is_singleton());
v.push(0);
v.pop();
assert!(!v.is_singleton());

let v2 = v.clone();
assert!(v2.is_singleton());
}
}

Expand Down