0.12.1
[anymap] / src / lib.rs
index 15a21bce7f9100dd9253213af9408233af88fc48..d41c335d1b73fe822f442ac71bff0317856b6b51 100644 (file)
@@ -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() }
     }
@@ -293,6 +310,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox<A>> VacantEntry<'a, A, V> {
 mod bench {
     use AnyMap;
     use test::Bencher;
+    use test::black_box;
 
     #[bench]
     fn insertion(b: &mut Bencher) {
@@ -331,43 +349,25 @@ mod bench {
             #[bench]
             fn $name(b: &mut Bencher) {
                 $(
-                    #[derive(Debug, PartialEq)]
                     struct $T(&'static str);
-
-                    impl Default for $T {
-                        fn default() -> $T {
-                            $T(stringify!($T))
-                        }
-                    }
                 )*
 
                 b.iter(|| {
                     let mut data = AnyMap::new();
                     $(
-                        assert_eq!(data.insert($T::default()), None::<$T>);
+                        let _ = black_box(data.insert($T(stringify!($T))));
                     )*
                     $(
-                        assert_eq!(data.get(), Some(&$T::default()));
+                        let _ = black_box(data.get::<$T>());
                     )*
                 })
             }
         );
     }
 
-    // Pathalogical rustc/llvm case here. This takes *absurdly* long to compile (it adds something
-    // like 100 seconds), peaking at over 1GB of RAM (though -Z time-passes doesn’t pick up on
-    // that, as it’s in the middle of the “llvm modules passes [0]” that it happens and it’s all
-    // freed at the end of that pass) and adding over 700KB to the final build. (3KB per type is
-    // more than I expected, reasonably or unreasonably.) TODO determine why and get it fixed.
-    //
-    // Selected rustc -Z time-passes output, with and without this block:
-    //
-    // - item-bodies checking:   0.4 seconds without,  5.2 seconds with;
-    // - borrow checking:        0.1 seconds without, 11   seconds with;
-    // - llvm module passes [0]: 2.5 seconds without, 49   seconds with;
-    // - codegen passes [0]:     0.6 seconds without, 11.2 seconds with.
-    //
-    // Very not good.
+    // Caution: if the macro does too much (e.g. assertions) this goes from being slow to being
+    // *really* slow (like add a minute for each assertion on it) and memory-hungry (like, adding
+    // several hundred megabytes to the peak for each assertion).
     big_benchmarks! {
         insert_and_get_on_260_types,
         A0 B0 C0 D0 E0 F0 G0 H0 I0 J0 K0 L0 M0 N0 O0 P0 Q0 R0 S0 T0 U0 V0 W0 X0 Y0 Z0