Skip to content

Commit

Permalink
perf(base): Remove an indirection in aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jan 27, 2019
1 parent e20a1db commit 8327a03
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
13 changes: 8 additions & 5 deletions base/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<Id, T> Clone for Seed<Id, T> {
pub fn deserialize_group<'de, Id, T, D>(
seed: &mut Seed<Id, T>,
deserializer: D,
) -> Result<Arc<Vec<AliasData<Id, T>>>, D::Error>
) -> Result<Arc<[AliasData<Id, T>]>, D::Error>
where
D: crate::serde::Deserializer<'de>,
T: Clone + From<Type<Id, T>> + ::std::any::Any + DeserializeState<'de, Seed<Id, T>>,
Expand All @@ -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 {
Expand All @@ -110,7 +110,10 @@ impl Shared for crate::kind::ArcKind {
}
}

impl<T> Shared for Arc<T> {
impl<T> Shared for Arc<T>
where
T: ?Sized,
{
fn unique(&self) -> bool {
Arc::strong_count(self) == 1
}
Expand Down Expand Up @@ -255,7 +258,7 @@ enum Lookup {

fn node_to_id<T>(map: &NodeToId, node: &T) -> Lookup
where
T: Shared,
T: ?Sized + Shared,
{
if Shared::unique(node) {
return Lookup::Unique;
Expand Down Expand Up @@ -326,7 +329,7 @@ pub mod shared {
pub fn serialize<S, T, Seed>(self_: &T, serializer: S, seed: &Seed) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Shared + Deref,
T: ?Sized + Shared + Deref,
T::Target: SerializeState<Seed>,
Seed: AsRef<NodeToId>,
{
Expand Down
18 changes: 9 additions & 9 deletions base/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ where
}

pub fn group(group: Vec<AliasData<Id, T>>) -> Vec<Alias<Id, T>> {
let group = Arc::new(group);
let group = Arc::<[_]>::from(group);
(0..group.len())
.map(|index| Alias {
_typ: T::from(Type::Alias(AliasRef {
Expand Down Expand Up @@ -500,7 +500,7 @@ pub struct AliasRef<Id, T> {
serde(serialize_state_with = "crate::serialization::shared::serialize")
)]
/// The other aliases defined in this group
pub group: Arc<Vec<AliasData<Id, T>>>,
pub group: Arc<[AliasData<Id, T>]>,
}

impl<Id, T> Eq for AliasRef<Id, T> where AliasData<Id, T>: Eq {}
Expand Down Expand Up @@ -535,7 +535,7 @@ impl<Id, T> AliasRef<Id, T> {
}

#[doc(hidden)]
pub fn new(index: usize, group: Arc<Vec<AliasData<Id, T>>>) -> Self {
pub fn new(index: usize, group: Arc<[AliasData<Id, T>]>) -> Self {
AliasRef { index, group }
}

Expand Down Expand Up @@ -985,7 +985,7 @@ where
pub fn alias(name: Id, args: Vec<Generic<Id>>, 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 }]),
}))
}

Expand Down Expand Up @@ -2932,7 +2932,7 @@ pub trait TypeInterner<Id, T> {
fn alias(&mut self, name: Id, args: Vec<Generic<Id>>, 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 }]),
}))
}

Expand Down Expand Up @@ -2995,14 +2995,14 @@ pub trait TypeInterner<Id, T> {
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<AliasData<Id, T>>) -> Vec<Alias<Id, T>> {
let group = Arc::new(group);
let group = Arc::<[_]>::from(group);
(0..group.len())
.map(|index| Alias {
_typ: self.intern(Type::Alias(AliasRef {
Expand Down Expand Up @@ -3533,15 +3533,15 @@ 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)))
.collect();

interner.intern(Type::Alias(AliasRef {
index: alias.index,
group: Arc::new(group),
group: Arc::from(group),
}))
}
Type::EmptyRow => interner.empty_row(),
Expand Down
23 changes: 16 additions & 7 deletions check/src/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,14 @@ where

#[doc(hidden)]
pub trait Ptr {
type Target;
type Target: ?Sized;
fn as_ptr(&self) -> &Self::Target;
}

impl<T> Ptr for Arc<T> {
impl<T> Ptr for Arc<T>
where
T: ?Sized,
{
type Target = T;
fn as_ptr(&self) -> &Self::Target {
self
Expand Down Expand Up @@ -264,9 +267,12 @@ impl<Id, T> Ptr for Type<Id, T> {
}

#[repr(transparent)]
pub struct PtrEq<T>(pub T);
pub struct PtrEq<T: ?Sized>(pub T);

impl<T> PtrEq<T> {
impl<T> PtrEq<T>
where
T: ?Sized,
{
pub fn new(t: &T) -> &Self {
unsafe { mem::transmute(t) }
}
Expand Down Expand Up @@ -297,7 +303,10 @@ where
}
}

impl<T> Borrow<PtrEq<T>> for PtrEq<Arc<T>> {
impl<T> Borrow<PtrEq<T>> for PtrEq<Arc<T>>
where
T: ?Sized,
{
#[inline(always)]
fn borrow(&self) -> &PtrEq<T> {
PtrEq::new(self.0.as_ptr())
Expand Down Expand Up @@ -335,7 +344,7 @@ impl TypeInternerAlloc for RcType {

pub struct TranslateInterner<T, U> {
pub type_map: FnvMap<PtrEq<T>, U>,
pub alias_map: FnvMap<PtrEq<Arc<Vec<AliasData<Symbol, T>>>>, Arc<Vec<AliasData<Symbol, U>>>>,
pub alias_map: FnvMap<PtrEq<Arc<[AliasData<Symbol, T>]>>, Arc<[AliasData<Symbol, U>]>>,
}

impl<T, U> Default for TranslateInterner<T, U>
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 8327a03

Please sign in to comment.