Skip to content

Commit

Permalink
perf(metadata): Use Arc to do shallow clones of Metadata (-4%)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Jan 27, 2019
1 parent 9c9d4b4 commit 4faf41b
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 151 deletions.
45 changes: 37 additions & 8 deletions base/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use std::collections::BTreeMap;
use std::{collections::BTreeMap, sync::Arc};

use crate::ast::Argument;
use crate::symbol::{Symbol, SymbolRef};
use crate::{
ast::Argument,
symbol::{Symbol, SymbolRef},
};

pub trait MetadataEnv {
fn get_metadata(&self, id: &SymbolRef) -> Option<&Metadata>;
fn get_metadata(&self, id: &SymbolRef) -> Option<&Arc<Metadata>>;
}

impl<'a, T: ?Sized + MetadataEnv> MetadataEnv for &'a T {
fn get_metadata(&self, id: &SymbolRef) -> Option<&Metadata> {
fn get_metadata(&self, id: &SymbolRef) -> Option<&Arc<Metadata>> {
(**self).get_metadata(id)
}
}

impl MetadataEnv for () {
fn get_metadata(&self, _id: &SymbolRef) -> Option<&Metadata> {
fn get_metadata(&self, _id: &SymbolRef) -> Option<&Arc<Metadata>> {
None
}
}
Expand Down Expand Up @@ -47,7 +49,7 @@ pub struct Metadata {
pub comment: Option<Comment>,
pub attributes: Vec<Attribute>,
pub args: Vec<Argument<Symbol>>,
pub module: BTreeMap<String, Metadata>,
pub module: BTreeMap<String, Arc<Metadata>>,
}

impl Metadata {
Expand Down Expand Up @@ -76,7 +78,7 @@ impl Metadata {
Entry::Vacant(entry) => {
entry.insert(value);
}
Entry::Occupied(entry) => entry.into_mut().merge_with(value),
Entry::Occupied(entry) => Arc::make_mut(entry.into_mut()).merge_with_ref(&value),
}
}
self.attributes.extend(other.attributes);
Expand All @@ -85,6 +87,33 @@ impl Metadata {
}
}

pub fn merge_ref(mut self, other: &Metadata) -> Self {
self.merge_with_ref(other);
self
}

pub fn merge_with_ref(&mut self, other: &Metadata) {
if other.definition.is_some() {
self.definition = other.definition.clone();
}
if self.comment.is_none() {
self.comment = other.comment.clone();
}
for (key, value) in &other.module {
use std::collections::btree_map::Entry;
match self.module.entry(key.clone()) {
Entry::Vacant(entry) => {
entry.insert(value.clone());
}
Entry::Occupied(entry) => Arc::make_mut(entry.into_mut()).merge_with_ref(&value),
}
}
self.attributes.extend(other.attributes.iter().cloned());
if self.args.is_empty() {
self.args = other.args.clone();
}
}

pub fn get_attribute(&self, name: &str) -> Option<&str> {
self.attributes()
.find(|attribute| attribute.name == name)
Expand Down
11 changes: 6 additions & 5 deletions check/src/implicits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
fmt,
hash::{Hash, Hasher},
rc::Rc,
sync::Arc,
};

use itertools::{Either, Itertools};
Expand Down Expand Up @@ -692,7 +693,7 @@ impl<'a, 'b, 'c> MutVisitor<'c> for ResolveImplicitsVisitor<'a, 'b> {
}

pub struct ImplicitResolver<'a> {
pub(crate) metadata: &'a mut FnvMap<Symbol, Metadata>,
pub(crate) metadata: &'a mut FnvMap<Symbol, Arc<Metadata>>,
environment: &'a TypecheckEnv<Type = RcType>,
pub(crate) implicit_bindings: Vec<ImplicitBindings>,
implicit_vars: ScopedMap<Symbol, ImplicitBindings>,
Expand All @@ -702,7 +703,7 @@ pub struct ImplicitResolver<'a> {
impl<'a> ImplicitResolver<'a> {
pub fn new(
environment: &'a TypecheckEnv<Type = RcType>,
metadata: &'a mut FnvMap<Symbol, Metadata>,
metadata: &'a mut FnvMap<Symbol, Arc<Metadata>>,
) -> ImplicitResolver<'a> {
ImplicitResolver {
metadata,
Expand All @@ -719,7 +720,7 @@ impl<'a> ImplicitResolver<'a> {
}
let metadata = self.metadata.get(id);

let opt = self.try_create_implicit(&id, metadata, &typ, &mut Vec::new());
let opt = self.try_create_implicit(&id, metadata.map(|m| &**m), &typ, &mut Vec::new());

if let Some((definition, path, implicit_type)) = opt {
self.implicit_bindings.last_mut().unwrap().insert(
Expand Down Expand Up @@ -769,7 +770,7 @@ impl<'a> ImplicitResolver<'a> {

let opt = self.try_create_implicit(
&field.name,
field_metadata,
field_metadata.map(|m| &**m),
&field.typ,
&mut path,
);
Expand Down Expand Up @@ -811,7 +812,7 @@ impl<'a> ImplicitResolver<'a> {
self.metadata
.get(typename)
.or_else(|| self.environment.get_metadata(typename))
.map(has_implicit_attribute)
.map(|m| has_implicit_attribute(m))
})
.unwrap_or(false);
}
Expand Down
Loading

0 comments on commit 4faf41b

Please sign in to comment.