From 75174cdf2bd6535419f8f270179780c8082dd883 Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Tue, 22 Jan 2019 21:51:28 +0100 Subject: [PATCH] perf: Compare and hash types by the minimum necessary --- base/src/types/mod.rs | 35 +++++++++++++++++++++++++++++++++-- check/src/kindcheck.rs | 1 + 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/base/src/types/mod.rs b/base/src/types/mod.rs index 23b9c242d3..7ae9163cc4 100644 --- a/base/src/types/mod.rs +++ b/base/src/types/mod.rs @@ -40,6 +40,31 @@ pub use self::pretty_print::{Filter, TypeFormatter}; pub mod pretty_print; +macro_rules! forward_eq_hash { + (<$($param: ident),*> for $typ: ty { $field: ident }) => { + impl<$($param),*> Eq for $typ where $($param : Eq),* {} + + impl<$($param),*> PartialEq for $typ + where $($param : PartialEq),* + { + fn eq(&self, other: &Self) -> bool { + self.$field == other.$field + } + } + + impl<$($param),*> Hash for $typ + where $($param : Hash),* + { + fn hash(&self, state: &mut H) + where + H: std::hash::Hasher, + { + self.$field.hash(state) + } + } + } +} + /// Trait for values which contains typed values which can be refered by name pub trait TypeEnv: KindEnv { type Type; @@ -237,7 +262,7 @@ impl BuiltinType { } } -#[derive(Clone, Debug, Eq, PartialEq, Hash)] +#[derive(Clone, Debug)] #[cfg_attr(feature = "serde_derive", derive(DeserializeState, SerializeState))] #[cfg_attr(feature = "serde_derive", serde(serialize_state = "SeSeed"))] #[cfg_attr(feature = "serde_derive", serde(deserialize_state = "Seed"))] @@ -248,7 +273,9 @@ pub struct TypeVariable { pub id: u32, } -#[derive(Clone, Debug, Eq, PartialEq, Hash)] +forward_eq_hash! { <> for TypeVariable { id } } + +#[derive(Clone, Debug)] #[cfg_attr(feature = "serde_derive", derive(DeserializeState, SerializeState))] #[cfg_attr(feature = "serde_derive", serde(deserialize_state = "Seed"))] #[cfg_attr( @@ -270,6 +297,10 @@ pub struct Skolem { pub kind: ArcKind, } +forward_eq_hash! { for Skolem { id } } + +/// FIXME Distinguish generic id's so we only need to compare them by `id` (currently they will get +/// the wrong kind assigned to them otherwise) #[derive(Clone, Debug, Eq, PartialEq, Hash)] #[cfg_attr(feature = "serde_derive", derive(DeserializeState, SerializeState))] #[cfg_attr(feature = "serde_derive", serde(deserialize_state = "Seed"))] diff --git a/check/src/kindcheck.rs b/check/src/kindcheck.rs index b65ce6b815..8443730b0e 100644 --- a/check/src/kindcheck.rs +++ b/check/src/kindcheck.rs @@ -178,6 +178,7 @@ impl<'a> KindCheck<'a> { let kind = self.kindcheck(typ)?; let kind = self.unify(typ.span(), expected, kind)?; self.finalize_type(typ); + info!("Kindchecked {:#?}", typ); Ok(kind) }