From 8327a035f0b72d2662f53a294680928809408ea9 Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Fri, 25 Jan 2019 20:40:49 +0100 Subject: [PATCH] perf(base): Remove an indirection in aliases --- base/src/serialization.rs | 13 ++++++++----- base/src/types/mod.rs | 18 +++++++++--------- check/src/typ.rs | 23 ++++++++++++++++------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/base/src/serialization.rs b/base/src/serialization.rs index aa01ebebe3..2bb71d7e3e 100644 --- a/base/src/serialization.rs +++ b/base/src/serialization.rs @@ -76,7 +76,7 @@ impl Clone for Seed { pub fn deserialize_group<'de, Id, T, D>( seed: &mut Seed, deserializer: D, -) -> Result>>, D::Error> +) -> Result]>, D::Error> where D: crate::serde::Deserializer<'de>, T: Clone + From> + ::std::any::Any + DeserializeState<'de, Seed>, @@ -87,7 +87,7 @@ where { use crate::serialization::SharedSeed; let seed = SharedSeed::new(seed); - DeserializeSeed::deserialize(seed, deserializer) + DeserializeSeed::deserialize(seed, deserializer).map(|vec: Vec<_>| Arc::from(vec)) } impl<'a, T> Shared for &'a T { @@ -110,7 +110,10 @@ impl Shared for crate::kind::ArcKind { } } -impl Shared for Arc { +impl Shared for Arc +where + T: ?Sized, +{ fn unique(&self) -> bool { Arc::strong_count(self) == 1 } @@ -255,7 +258,7 @@ enum Lookup { fn node_to_id(map: &NodeToId, node: &T) -> Lookup where - T: Shared, + T: ?Sized + Shared, { if Shared::unique(node) { return Lookup::Unique; @@ -326,7 +329,7 @@ pub mod shared { pub fn serialize(self_: &T, serializer: S, seed: &Seed) -> Result where S: Serializer, - T: Shared + Deref, + T: ?Sized + Shared + Deref, T::Target: SerializeState, Seed: AsRef, { diff --git a/base/src/types/mod.rs b/base/src/types/mod.rs index 990a4df474..0b49d1af91 100644 --- a/base/src/types/mod.rs +++ b/base/src/types/mod.rs @@ -423,7 +423,7 @@ where } pub fn group(group: Vec>) -> Vec> { - let group = Arc::new(group); + let group = Arc::<[_]>::from(group); (0..group.len()) .map(|index| Alias { _typ: T::from(Type::Alias(AliasRef { @@ -500,7 +500,7 @@ pub struct AliasRef { serde(serialize_state_with = "crate::serialization::shared::serialize") )] /// The other aliases defined in this group - pub group: Arc>>, + pub group: Arc<[AliasData]>, } impl Eq for AliasRef where AliasData: Eq {} @@ -535,7 +535,7 @@ impl AliasRef { } #[doc(hidden)] - pub fn new(index: usize, group: Arc>>) -> Self { + pub fn new(index: usize, group: Arc<[AliasData]>) -> Self { AliasRef { index, group } } @@ -985,7 +985,7 @@ where pub fn alias(name: Id, args: Vec>, typ: T) -> T { T::from(Type::Alias(AliasRef { index: 0, - group: Arc::new(vec![AliasData { name, args, typ }]), + group: Arc::from(vec![AliasData { name, args, typ }]), })) } @@ -2932,7 +2932,7 @@ pub trait TypeInterner { fn alias(&mut self, name: Id, args: Vec>, typ: T) -> T { self.intern(Type::Alias(AliasRef { index: 0, - group: Arc::new(vec![AliasData { name, args, typ }]), + group: Arc::from(vec![AliasData { name, args, typ }]), })) } @@ -2995,14 +2995,14 @@ pub trait TypeInterner { Alias { _typ: self.intern(Type::Alias(AliasRef { index: 0, - group: Arc::new(vec![data]), + group: Arc::from(vec![data]), })), _marker: PhantomData, } } fn alias_group(&mut self, group: Vec>) -> Vec> { - let group = Arc::new(group); + let group = Arc::<[_]>::from(group); (0..group.len()) .map(|index| Alias { _typ: self.intern(Type::Alias(AliasRef { @@ -3533,7 +3533,7 @@ where Type::Ident(ref id) => interner.ident(id.clone()), Type::Projection(ref ids) => interner.projection(ids.clone()), Type::Alias(ref alias) => { - let group = alias + let group: Vec<_> = alias .group .iter() .map(|alias_data| translate_alias(alias_data, |a| translate(interner, a))) @@ -3541,7 +3541,7 @@ where interner.intern(Type::Alias(AliasRef { index: alias.index, - group: Arc::new(group), + group: Arc::from(group), })) } Type::EmptyRow => interner.empty_row(), diff --git a/check/src/typ.rs b/check/src/typ.rs index 75a52390d0..9dd9b863a5 100644 --- a/check/src/typ.rs +++ b/check/src/typ.rs @@ -231,11 +231,14 @@ where #[doc(hidden)] pub trait Ptr { - type Target; + type Target: ?Sized; fn as_ptr(&self) -> &Self::Target; } -impl Ptr for Arc { +impl Ptr for Arc +where + T: ?Sized, +{ type Target = T; fn as_ptr(&self) -> &Self::Target { self @@ -264,9 +267,12 @@ impl Ptr for Type { } #[repr(transparent)] -pub struct PtrEq(pub T); +pub struct PtrEq(pub T); -impl PtrEq { +impl PtrEq +where + T: ?Sized, +{ pub fn new(t: &T) -> &Self { unsafe { mem::transmute(t) } } @@ -297,7 +303,10 @@ where } } -impl Borrow> for PtrEq> { +impl Borrow> for PtrEq> +where + T: ?Sized, +{ #[inline(always)] fn borrow(&self) -> &PtrEq { PtrEq::new(self.0.as_ptr()) @@ -335,7 +344,7 @@ impl TypeInternerAlloc for RcType { pub struct TranslateInterner { pub type_map: FnvMap, U>, - pub alias_map: FnvMap>>>, Arc>>>, + pub alias_map: FnvMap]>>, Arc<[AliasData]>>, } impl Default for TranslateInterner @@ -390,7 +399,7 @@ where let group = match type_interner.alias_map.get(PtrEq::new(&alias.group)) { Some(group) => group.clone(), None => { - let group = Arc::new( + let group: Arc<[_]> = Arc::from( alias .group .iter()