X-Git-Url: https://git.chrismorgan.info/anymap/blobdiff_plain/0c3026f7dec56e013478d896e1a088d10bdc0411..1374cacb410abd763f5ff6d4644b8f01b2330f3c:/src/lib.rs?ds=inline diff --git a/src/lib.rs b/src/lib.rs index 15a21bc..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() } } @@ -293,6 +310,7 @@ impl<'a, A: ?Sized + UncheckedAnyExt, V: IntoBox> 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