Put in a bunch of #[inline] attributes on fns.
Somewhere along the path I didn’t mark some functions as `#[inline]` which they should probably be. Small but visible benchmark improvements, but within ε so low confidence.
This commit is contained in:
parent
ec57ec49be
commit
b549457d62
10
src/any.rs
10
src/any.rs
|
@ -22,18 +22,22 @@ pub trait CloneToAny {
|
|||
}
|
||||
|
||||
impl<T: Any + Clone> CloneToAny for T {
|
||||
#[inline]
|
||||
fn clone_to_any(&self) -> Box<CloneAny> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn clone_to_any_send(&self) -> Box<CloneAny + Send> where Self: Send {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn clone_to_any_sync(&self) -> Box<CloneAny + Sync> where Self: Sync {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn clone_to_any_send_sync(&self) -> Box<CloneAny + Send + Sync> where Self: Send + Sync {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
@ -80,6 +84,7 @@ macro_rules! define {
|
|||
macro_rules! impl_clone {
|
||||
($t:ty, $method:ident) => {
|
||||
impl Clone for Box<$t> {
|
||||
#[inline]
|
||||
fn clone(&self) -> Box<$t> {
|
||||
(**self).$method()
|
||||
}
|
||||
|
@ -104,26 +109,31 @@ pub trait IntoBox<A: ?Sized + UncheckedAnyExt>: Any {
|
|||
macro_rules! implement {
|
||||
($base:ident, $(+ $bounds:ident)*) => {
|
||||
impl fmt::Debug for $base $(+ $bounds)* {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.pad(stringify!($base $(+ $bounds)*))
|
||||
}
|
||||
}
|
||||
|
||||
impl UncheckedAnyExt for $base $(+ $bounds)* {
|
||||
#[inline]
|
||||
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
|
||||
&*(self as *const Self as *const T)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
|
||||
&mut *(self as *mut Self as *mut T)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn downcast_unchecked<T: 'static>(self: Box<Self>) -> Box<T> {
|
||||
Box::from_raw(Box::into_raw(self) as *mut T)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: $base $(+ $bounds)*> IntoBox<$base $(+ $bounds)*> for T {
|
||||
#[inline]
|
||||
fn into_box(self) -> Box<$base $(+ $bounds)*> {
|
||||
Box::new(self)
|
||||
}
|
||||
|
|
17
src/lib.rs
17
src/lib.rs
|
@ -123,6 +123,7 @@ pub struct Map<A: ?Sized + UncheckedAnyExt = Any> {
|
|||
|
||||
// #[derive(Clone)] would want A to implement Clone, but in reality it’s only Box<A> that can.
|
||||
impl<A: ?Sized + UncheckedAnyExt> Clone for Map<A> where Box<A>: Clone {
|
||||
#[inline]
|
||||
fn clone(&self) -> Map<A> {
|
||||
Map {
|
||||
raw: self.raw.clone(),
|
||||
|
@ -145,6 +146,7 @@ impl_common_methods! {
|
|||
|
||||
impl<A: ?Sized + UncheckedAnyExt> Map<A> {
|
||||
/// Returns a reference to the value stored in the collection for the type `T`, if it exists.
|
||||
#[inline]
|
||||
pub fn get<T: IntoBox<A>>(&self) -> Option<&T> {
|
||||
self.raw.get(&TypeId::of::<T>())
|
||||
.map(|any| unsafe { any.downcast_ref_unchecked::<T>() })
|
||||
|
@ -152,6 +154,7 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
|
|||
|
||||
/// Returns a mutable reference to the value stored in the collection for the type `T`,
|
||||
/// if it exists.
|
||||
#[inline]
|
||||
pub fn get_mut<T: IntoBox<A>>(&mut self) -> Option<&mut T> {
|
||||
self.raw.get_mut(&TypeId::of::<T>())
|
||||
.map(|any| unsafe { any.downcast_mut_unchecked::<T>() })
|
||||
|
@ -160,6 +163,7 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
|
|||
/// Sets the value stored in the collection for the type `T`.
|
||||
/// If the collection already had a value of type `T`, that value is returned.
|
||||
/// Otherwise, `None` is returned.
|
||||
#[inline]
|
||||
pub fn insert<T: IntoBox<A>>(&mut self, value: T) -> Option<T> {
|
||||
unsafe {
|
||||
self.raw.insert(TypeId::of::<T>(), value.into_box())
|
||||
|
@ -169,6 +173,7 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
|
|||
|
||||
/// Removes the `T` value from the collection,
|
||||
/// returning it if there was one or `None` if there was not.
|
||||
#[inline]
|
||||
pub fn remove<T: IntoBox<A>>(&mut self) -> Option<T> {
|
||||
self.raw.remove(&TypeId::of::<T>())
|
||||
.map(|any| *unsafe { any.downcast_unchecked::<T>() })
|
||||
|
@ -181,6 +186,7 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
|
|||
}
|
||||
|
||||
/// Gets the entry for the given type in the collection for in-place manipulation
|
||||
#[inline]
|
||||
pub fn entry<T: IntoBox<A>>(&mut self) -> Entry<A, T> {
|
||||
match self.raw.entry(TypeId::of::<T>()) {
|
||||
raw::Entry::Occupied(e) => Entry::Occupied(OccupiedEntry {
|
||||
|
@ -196,18 +202,21 @@ impl<A: ?Sized + UncheckedAnyExt> Map<A> {
|
|||
}
|
||||
|
||||
impl<A: ?Sized + UncheckedAnyExt> AsRef<RawMap<A>> for Map<A> {
|
||||
#[inline]
|
||||
fn as_ref(&self) -> &RawMap<A> {
|
||||
&self.raw
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: ?Sized + UncheckedAnyExt> AsMut<RawMap<A>> for Map<A> {
|
||||
#[inline]
|
||||
fn as_mut(&mut self) -> &mut RawMap<A> {
|
||||
&mut self.raw
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: ?Sized + UncheckedAnyExt> Into<RawMap<A>> for Map<A> {
|
||||
#[inline]
|
||||
fn into(self) -> RawMap<A> {
|
||||
self.raw
|
||||
}
|
||||
|
@ -236,6 +245,7 @@ pub enum Entry<'a, A: ?Sized + UncheckedAnyExt, V: 'a> {
|
|||
impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> Entry<'a, A, V> {
|
||||
/// Ensures a value is in the entry by inserting the default if empty, and returns
|
||||
/// a mutable reference to the value in the entry.
|
||||
#[inline]
|
||||
pub fn or_insert(self, default: V) -> &'a mut V {
|
||||
match self {
|
||||
Entry::Occupied(inner) => inner.into_mut(),
|
||||
|
@ -245,6 +255,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> Entry<'a, A, V> {
|
|||
|
||||
/// Ensures a value is in the entry by inserting the result of the default function if empty,
|
||||
/// and returns a mutable reference to the value in the entry.
|
||||
#[inline]
|
||||
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
|
||||
match self {
|
||||
Entry::Occupied(inner) => inner.into_mut(),
|
||||
|
@ -255,27 +266,32 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> Entry<'a, A, V> {
|
|||
|
||||
impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> OccupiedEntry<'a, A, V> {
|
||||
/// Gets a reference to the value in the entry
|
||||
#[inline]
|
||||
pub fn get(&self) -> &V {
|
||||
unsafe { self.inner.get().downcast_ref_unchecked() }
|
||||
}
|
||||
|
||||
/// Gets a mutable reference to the value in the entry
|
||||
#[inline]
|
||||
pub fn get_mut(&mut self) -> &mut V {
|
||||
unsafe { self.inner.get_mut().downcast_mut_unchecked() }
|
||||
}
|
||||
|
||||
/// Converts the OccupiedEntry into a mutable reference to the value in the entry
|
||||
/// with a lifetime bound to the collection itself
|
||||
#[inline]
|
||||
pub fn into_mut(self) -> &'a mut V {
|
||||
unsafe { self.inner.into_mut().downcast_mut_unchecked() }
|
||||
}
|
||||
|
||||
/// Sets the value of the entry, and returns the entry's old value
|
||||
#[inline]
|
||||
pub fn insert(&mut self, value: V) -> V {
|
||||
unsafe { *self.inner.insert(value.into_box()).downcast_unchecked() }
|
||||
}
|
||||
|
||||
/// Takes the value out of the entry, and returns it
|
||||
#[inline]
|
||||
pub fn remove(self) -> V {
|
||||
unsafe { *self.inner.remove().downcast_unchecked() }
|
||||
}
|
||||
|
@ -284,6 +300,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> OccupiedEntry<'a, A, V> {
|
|||
impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> VacantEntry<'a, A, V> {
|
||||
/// Sets the value of the entry with the VacantEntry's key,
|
||||
/// and returns a mutable reference to it
|
||||
#[inline]
|
||||
pub fn insert(self, value: V) -> &'a mut V {
|
||||
unsafe { self.inner.insert(value.into_box()).downcast_mut_unchecked() }
|
||||
}
|
||||
|
|
19
src/raw.rs
19
src/raw.rs
|
@ -61,6 +61,7 @@ pub struct RawMap<A: ?Sized + UncheckedAnyExt = Any> {
|
|||
|
||||
// #[derive(Clone)] would want A to implement Clone, but in reality it’s only Box<A> that can.
|
||||
impl<A: ?Sized + UncheckedAnyExt> Clone for RawMap<A> where Box<A>: Clone {
|
||||
#[inline]
|
||||
fn clone(&self) -> RawMap<A> {
|
||||
RawMap {
|
||||
inner: self.inner.clone(),
|
||||
|
@ -69,6 +70,7 @@ impl<A: ?Sized + UncheckedAnyExt> Clone for RawMap<A> where Box<A>: Clone {
|
|||
}
|
||||
|
||||
impl<A: ?Sized + UncheckedAnyExt> Default for RawMap<A> {
|
||||
#[inline]
|
||||
fn default() -> RawMap<A> {
|
||||
RawMap::new()
|
||||
}
|
||||
|
@ -167,6 +169,7 @@ impl<A: ?Sized + UncheckedAnyExt> RawMap<A> {
|
|||
}
|
||||
|
||||
/// Gets the entry for the given type in the collection for in-place manipulation.
|
||||
#[inline]
|
||||
pub fn entry(&mut self, key: TypeId) -> Entry<A> {
|
||||
match self.inner.entry(key) {
|
||||
hash_map::Entry::Occupied(e) => Entry::Occupied(OccupiedEntry {
|
||||
|
@ -182,6 +185,7 @@ impl<A: ?Sized + UncheckedAnyExt> RawMap<A> {
|
|||
///
|
||||
/// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
|
||||
/// form *must* match those for the key type.
|
||||
#[inline]
|
||||
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&A>
|
||||
where TypeId: Borrow<Q>, Q: Hash + Eq {
|
||||
self.inner.get(k).map(|x| &**x)
|
||||
|
@ -191,6 +195,7 @@ impl<A: ?Sized + UncheckedAnyExt> RawMap<A> {
|
|||
///
|
||||
/// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
|
||||
/// form *must* match those for the key type.
|
||||
#[inline]
|
||||
pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
|
||||
where TypeId: Borrow<Q>, Q: Hash + Eq {
|
||||
self.inner.contains_key(k)
|
||||
|
@ -200,6 +205,7 @@ impl<A: ?Sized + UncheckedAnyExt> RawMap<A> {
|
|||
///
|
||||
/// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
|
||||
/// form *must* match those for the key type.
|
||||
#[inline]
|
||||
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut A>
|
||||
where TypeId: Borrow<Q>, Q: Hash + Eq {
|
||||
self.inner.get_mut(k).map(|x| &mut **x)
|
||||
|
@ -210,6 +216,7 @@ impl<A: ?Sized + UncheckedAnyExt> RawMap<A> {
|
|||
///
|
||||
/// It is the caller’s responsibility to ensure that the key corresponds with the type ID of
|
||||
/// the value. If they do not, memory safety may be violated.
|
||||
#[inline]
|
||||
pub unsafe fn insert(&mut self, key: TypeId, value: Box<A>) -> Option<Box<A>> {
|
||||
self.inner.insert(key, value)
|
||||
}
|
||||
|
@ -219,6 +226,7 @@ impl<A: ?Sized + UncheckedAnyExt> RawMap<A> {
|
|||
///
|
||||
/// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
|
||||
/// form *must* match those for the key type.
|
||||
#[inline]
|
||||
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<Box<A>>
|
||||
where TypeId: Borrow<Q>, Q: Hash + Eq {
|
||||
self.inner.remove(k)
|
||||
|
@ -229,12 +237,14 @@ impl<A: ?Sized + UncheckedAnyExt> RawMap<A> {
|
|||
impl<A: ?Sized + UncheckedAnyExt, Q> Index<Q> for RawMap<A> where TypeId: Borrow<Q>, Q: Eq + Hash {
|
||||
type Output = A;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: Q) -> &A {
|
||||
self.get(&index).expect("no entry found for key")
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: ?Sized + UncheckedAnyExt, Q> IndexMut<Q> for RawMap<A> where TypeId: Borrow<Q>, Q: Eq + Hash {
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: Q) -> &mut A {
|
||||
self.get_mut(&index).expect("no entry found for key")
|
||||
}
|
||||
|
@ -244,6 +254,7 @@ impl<A: ?Sized + UncheckedAnyExt> IntoIterator for RawMap<A> {
|
|||
type Item = Box<A>;
|
||||
type IntoIter = IntoIter<A>;
|
||||
|
||||
#[inline]
|
||||
fn into_iter(self) -> IntoIter<A> {
|
||||
IntoIter {
|
||||
inner: self.inner.into_iter(),
|
||||
|
@ -275,6 +286,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt> Entry<'a, A> {
|
|||
///
|
||||
/// It is the caller’s responsibility to ensure that the key of the entry corresponds with
|
||||
/// the type ID of `value`. If they do not, memory safety may be violated.
|
||||
#[inline]
|
||||
pub unsafe fn or_insert(self, default: Box<A>) -> &'a mut A {
|
||||
match self {
|
||||
Entry::Occupied(inner) => inner.into_mut(),
|
||||
|
@ -287,6 +299,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt> Entry<'a, A> {
|
|||
///
|
||||
/// It is the caller’s responsibility to ensure that the key of the entry corresponds with
|
||||
/// the type ID of `value`. If they do not, memory safety may be violated.
|
||||
#[inline]
|
||||
pub unsafe fn or_insert_with<F: FnOnce() -> Box<A>>(self, default: F) -> &'a mut A {
|
||||
match self {
|
||||
Entry::Occupied(inner) => inner.into_mut(),
|
||||
|
@ -297,17 +310,20 @@ impl<'a, A: ?Sized + UncheckedAnyExt> Entry<'a, A> {
|
|||
|
||||
impl<'a, A: ?Sized + UncheckedAnyExt> OccupiedEntry<'a, A> {
|
||||
/// Gets a reference to the value in the entry.
|
||||
#[inline]
|
||||
pub fn get(&self) -> &A {
|
||||
&**self.inner.get()
|
||||
}
|
||||
|
||||
/// Gets a mutable reference to the value in the entry.
|
||||
#[inline]
|
||||
pub fn get_mut(&mut self) -> &mut A {
|
||||
&mut **self.inner.get_mut()
|
||||
}
|
||||
|
||||
/// Converts the OccupiedEntry into a mutable reference to the value in the entry
|
||||
/// with a lifetime bound to the collection itself.
|
||||
#[inline]
|
||||
pub fn into_mut(self) -> &'a mut A {
|
||||
&mut **self.inner.into_mut()
|
||||
}
|
||||
|
@ -316,11 +332,13 @@ impl<'a, A: ?Sized + UncheckedAnyExt> OccupiedEntry<'a, A> {
|
|||
///
|
||||
/// It is the caller’s responsibility to ensure that the key of the entry corresponds with
|
||||
/// the type ID of `value`. If they do not, memory safety may be violated.
|
||||
#[inline]
|
||||
pub unsafe fn insert(&mut self, value: Box<A>) -> Box<A> {
|
||||
self.inner.insert(value)
|
||||
}
|
||||
|
||||
/// Takes the value out of the entry, and returns it.
|
||||
#[inline]
|
||||
pub fn remove(self) -> Box<A> {
|
||||
self.inner.remove()
|
||||
}
|
||||
|
@ -332,6 +350,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt> VacantEntry<'a, A> {
|
|||
///
|
||||
/// It is the caller’s responsibility to ensure that the key of the entry corresponds with
|
||||
/// the type ID of `value`. If they do not, memory safety may be violated.
|
||||
#[inline]
|
||||
pub unsafe fn insert(self, value: Box<A>) -> &'a mut A {
|
||||
&mut **self.inner.insert(value)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue