From b549457d628fb178f6d700d85800f3ab63482d63 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Sat, 11 Jun 2016 13:30:33 +1000 Subject: [PATCH] Put in a bunch of #[inline] attributes on fns. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. --- src/any.rs | 10 ++++++++++ src/lib.rs | 17 +++++++++++++++++ src/raw.rs | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/any.rs b/src/any.rs index 4120e6a..976f768 100644 --- a/src/any.rs +++ b/src/any.rs @@ -22,18 +22,22 @@ pub trait CloneToAny { } impl CloneToAny for T { + #[inline] fn clone_to_any(&self) -> Box { Box::new(self.clone()) } + #[inline] fn clone_to_any_send(&self) -> Box where Self: Send { Box::new(self.clone()) } + #[inline] fn clone_to_any_sync(&self) -> Box where Self: Sync { Box::new(self.clone()) } + #[inline] fn clone_to_any_send_sync(&self) -> Box 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: 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(&self) -> &T { &*(self as *const Self as *const T) } + #[inline] unsafe fn downcast_mut_unchecked(&mut self) -> &mut T { &mut *(self as *mut Self as *mut T) } + #[inline] unsafe fn downcast_unchecked(self: Box) -> Box { Box::from_raw(Box::into_raw(self) as *mut T) } } impl IntoBox<$base $(+ $bounds)*> for T { + #[inline] fn into_box(self) -> Box<$base $(+ $bounds)*> { Box::new(self) } diff --git a/src/lib.rs b/src/lib.rs index d2b85aa..d41c335 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -123,6 +123,7 @@ pub struct Map { // #[derive(Clone)] would want A to implement Clone, but in reality it’s only Box that can. impl Clone for Map where Box: Clone { + #[inline] fn clone(&self) -> Map { Map { raw: self.raw.clone(), @@ -145,6 +146,7 @@ impl_common_methods! { impl Map { /// Returns a reference to the value stored in the collection for the type `T`, if it exists. + #[inline] pub fn get>(&self) -> Option<&T> { self.raw.get(&TypeId::of::()) .map(|any| unsafe { any.downcast_ref_unchecked::() }) @@ -152,6 +154,7 @@ impl Map { /// Returns a mutable reference to the value stored in the collection for the type `T`, /// if it exists. + #[inline] pub fn get_mut>(&mut self) -> Option<&mut T> { self.raw.get_mut(&TypeId::of::()) .map(|any| unsafe { any.downcast_mut_unchecked::() }) @@ -160,6 +163,7 @@ impl Map { /// 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>(&mut self, value: T) -> Option { unsafe { self.raw.insert(TypeId::of::(), value.into_box()) @@ -169,6 +173,7 @@ impl Map { /// Removes the `T` value from the collection, /// returning it if there was one or `None` if there was not. + #[inline] pub fn remove>(&mut self) -> Option { self.raw.remove(&TypeId::of::()) .map(|any| *unsafe { any.downcast_unchecked::() }) @@ -181,6 +186,7 @@ impl Map { } /// Gets the entry for the given type in the collection for in-place manipulation + #[inline] pub fn entry>(&mut self) -> Entry { match self.raw.entry(TypeId::of::()) { raw::Entry::Occupied(e) => Entry::Occupied(OccupiedEntry { @@ -196,18 +202,21 @@ impl Map { } impl AsRef> for Map { + #[inline] fn as_ref(&self) -> &RawMap { &self.raw } } impl AsMut> for Map { + #[inline] fn as_mut(&mut self) -> &mut RawMap { &mut self.raw } } impl Into> for Map { + #[inline] fn into(self) -> RawMap { self.raw } @@ -236,6 +245,7 @@ pub enum Entry<'a, A: ?Sized + UncheckedAnyExt, V: 'a> { impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox> 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> 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 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> Entry<'a, A, V> { impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox> 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> OccupiedEntry<'a, A, V> { impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox> 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() } } diff --git a/src/raw.rs b/src/raw.rs index 3645429..7d53783 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -61,6 +61,7 @@ pub struct RawMap { // #[derive(Clone)] would want A to implement Clone, but in reality it’s only Box that can. impl Clone for RawMap where Box: Clone { + #[inline] fn clone(&self) -> RawMap { RawMap { inner: self.inner.clone(), @@ -69,6 +70,7 @@ impl Clone for RawMap where Box: Clone { } impl Default for RawMap { + #[inline] fn default() -> RawMap { RawMap::new() } @@ -167,6 +169,7 @@ impl RawMap { } /// Gets the entry for the given type in the collection for in-place manipulation. + #[inline] pub fn entry(&mut self, key: TypeId) -> Entry { match self.inner.entry(key) { hash_map::Entry::Occupied(e) => Entry::Occupied(OccupiedEntry { @@ -182,6 +185,7 @@ impl RawMap { /// /// 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(&self, k: &Q) -> Option<&A> where TypeId: Borrow, Q: Hash + Eq { self.inner.get(k).map(|x| &**x) @@ -191,6 +195,7 @@ impl RawMap { /// /// 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(&self, k: &Q) -> bool where TypeId: Borrow, Q: Hash + Eq { self.inner.contains_key(k) @@ -200,6 +205,7 @@ impl RawMap { /// /// 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(&mut self, k: &Q) -> Option<&mut A> where TypeId: Borrow, Q: Hash + Eq { self.inner.get_mut(k).map(|x| &mut **x) @@ -210,6 +216,7 @@ impl RawMap { /// /// 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) -> Option> { self.inner.insert(key, value) } @@ -219,6 +226,7 @@ impl RawMap { /// /// 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(&mut self, k: &Q) -> Option> where TypeId: Borrow, Q: Hash + Eq { self.inner.remove(k) @@ -229,12 +237,14 @@ impl RawMap { impl Index for RawMap where TypeId: Borrow, Q: Eq + Hash { type Output = A; + #[inline] fn index(&self, index: Q) -> &A { self.get(&index).expect("no entry found for key") } } impl IndexMut for RawMap where TypeId: Borrow, 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 IntoIterator for RawMap { type Item = Box; type IntoIter = IntoIter; + #[inline] fn into_iter(self) -> IntoIter { 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 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 Box>(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) -> Box { self.inner.insert(value) } /// Takes the value out of the entry, and returns it. + #[inline] pub fn remove(self) -> Box { 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 mut A { &mut **self.inner.insert(value) } -- 2.42.0