-
Notifications
You must be signed in to change notification settings - Fork 146
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
Split out some cold paths #215
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,7 +205,7 @@ impl<T: Clone> ExtendFromSlice<T> for Vec<T> { | |
} | ||
|
||
/// Error type for APIs with fallible heap allocation | ||
#[derive(Debug)] | ||
#[derive(Clone, Copy, Debug)] | ||
pub enum CollectionAllocErr { | ||
/// Overflow `usize::MAX` or other error during size computation | ||
CapacityOverflow, | ||
|
@@ -221,23 +221,33 @@ impl From<LayoutErr> for CollectionAllocErr { | |
CollectionAllocErr::CapacityOverflow | ||
} | ||
} | ||
impl CollectionAllocErr { | ||
#[cold] | ||
#[inline(never)] | ||
fn bail(&self) -> ! { | ||
match self { | ||
CollectionAllocErr::CapacityOverflow => panic!("capacity overflow"), | ||
CollectionAllocErr::AllocErr { layout } => alloc::alloc::handle_alloc_error(*layout), | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
fn infallible<T>(result: Result<T, CollectionAllocErr>) -> T { | ||
match result { | ||
Ok(x) => x, | ||
Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"), | ||
Err(CollectionAllocErr::AllocErr { layout }) => alloc::alloc::handle_alloc_error(layout), | ||
Err(ref err) => err.bail(), | ||
} | ||
} | ||
|
||
/// FIXME: use `Layout::array` when we require a Rust version where it’s stable | ||
/// https://github.com/rust-lang/rust/issues/55724 | ||
fn layout_array<T>(n: usize) -> Result<Layout, CollectionAllocErr> { | ||
let size = mem::size_of::<T>().checked_mul(n) | ||
let size = mem::size_of::<T>() | ||
.checked_mul(n) | ||
.ok_or(CollectionAllocErr::CapacityOverflow)?; | ||
let align = mem::align_of::<T>(); | ||
Layout::from_size_align(size, align) | ||
.map_err(|_| CollectionAllocErr::CapacityOverflow) | ||
Layout::from_size_align(size, align).map_err(|_| CollectionAllocErr::CapacityOverflow) | ||
} | ||
|
||
unsafe fn deallocate<T>(ptr: *mut T, capacity: usize) { | ||
|
@@ -800,8 +810,19 @@ impl<A: Array> SmallVec<A> { | |
// from callers like insert() | ||
let (_, &mut len, cap) = self.triple_mut(); | ||
if cap - len >= additional { | ||
return Ok(()); | ||
Ok(()) | ||
} else { | ||
self.try_reserve_cold(len, additional) | ||
} | ||
} | ||
|
||
#[cold] | ||
#[inline(never)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if it's worth the |
||
fn try_reserve_cold( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not really sure about this being tagged as
For such cases I think only the caller knows whether the capacity is going to be likely-already enough. For example Maybe we should expose both, or something... |
||
&mut self, | ||
len: usize, | ||
additional: usize, | ||
) -> Result<(), CollectionAllocErr> { | ||
let new_cap = len | ||
.checked_add(additional) | ||
.and_then(usize::checked_next_power_of_two) | ||
|
@@ -817,11 +838,23 @@ impl<A: Array> SmallVec<A> { | |
} | ||
|
||
/// Reserve the minimum capacity for `additional` more elements to be inserted. | ||
#[inline] | ||
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { | ||
let (_, &mut len, cap) = self.triple_mut(); | ||
if cap - len >= additional { | ||
return Ok(()); | ||
Ok(()) | ||
} else { | ||
self.try_reserve_exact_cold(len, additional) | ||
} | ||
} | ||
|
||
#[cold] | ||
#[inline(never)] | ||
fn try_reserve_exact_cold( | ||
&mut self, | ||
len: usize, | ||
additional: usize, | ||
) -> Result<(), CollectionAllocErr> { | ||
let new_cap = len | ||
.checked_add(additional) | ||
.ok_or(CollectionAllocErr::CapacityOverflow)?; | ||
|
@@ -1782,7 +1815,9 @@ impl<'a> Drop for SetLenOnDrop<'a> { | |
#[cfg(feature = "const_generics")] | ||
unsafe impl<T, const N: usize> Array for [T; N] { | ||
type Item = T; | ||
fn size() -> usize { N } | ||
fn size() -> usize { | ||
N | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "const_generics"))] | ||
|
@@ -1805,13 +1840,15 @@ impl_array!( | |
); | ||
|
||
/// Convenience trait for constructing a `SmallVec` | ||
pub trait ToSmallVec<A:Array> { | ||
pub trait ToSmallVec<A: Array> { | ||
/// Construct a new `SmallVec` from a slice. | ||
fn to_smallvec(&self) -> SmallVec<A>; | ||
} | ||
|
||
impl<A:Array> ToSmallVec<A> for [A::Item] | ||
where A::Item: Copy { | ||
impl<A: Array> ToSmallVec<A> for [A::Item] | ||
where | ||
A::Item: Copy, | ||
{ | ||
#[inline] | ||
fn to_smallvec(&self) -> SmallVec<A> { | ||
SmallVec::from_slice(self) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[cold]
here looks uncontroversial to me, though maybe the#[inline(never)]
prevents stuff from getting potentially optimized away?