Commit d78fc511 authored by Jeff Smits's avatar Jeff Smits

Removed string cloning for names in scopes, nice little speed boost from that

parent 96fee995
......@@ -82,7 +82,7 @@ impl<'d, 'f : 'd> MutContext<'d, 'f> {
})
}
pub fn match_term(&self, term_name: &str, current: &ATermRef) -> Result<()> {
pub fn match_term(&self, term_name: &'d str, current: &ATermRef) -> Result<()> {
Scopes::match_term(&mut self.scopes.borrow_mut(), term_name, current)
}
......@@ -208,16 +208,16 @@ impl fmt::Display for StackTracer {
#[derive(Debug, Default, Clone)]
pub struct Scope<'a, ATerm> {
pub term: FnvHashMap<String, Option<ATerm>>,
pub strategy: FnvHashMap<String, StrategyDef<'a>>,
pub term: FnvHashMap<&'a str, Option<ATerm>>,
pub strategy: FnvHashMap<&'a str, StrategyDef<'a>>,
is_overlay: bool,
}
impl<'a, ATerm> Scope<'a, ATerm> {
pub fn new<IA, IS>(terms: IA, defs: IS) -> Self
where
IA: IntoIterator<Item = (String, ATerm)>,
IS: IntoIterator<Item = (String, StrategyDef<'a>)>,
IA: IntoIterator<Item = (&'a str, ATerm)>,
IS: IntoIterator<Item = (&'a str, StrategyDef<'a>)>,
{
Scope {
term: terms.into_iter().map(|(n, a)| (n, Some(a))).collect(),
......@@ -233,7 +233,7 @@ impl<'a, ATerm> Scope<'a, ATerm> {
Scope {
term: FnvHashMap::default(),
strategy: defs.into_iter()
.map(|def| (def.name.clone(), StrategyDef::from_def(def)))
.map(|def| (def.name.as_str(), StrategyDef::from_def(def)))
.collect(),
is_overlay: false,
}
......@@ -246,7 +246,7 @@ impl<'a, ATerm> Scope<'a, ATerm> {
Scope {
term: FnvHashMap::default(),
strategy: defs.into_iter()
.map(|def| (def.name.clone(), StrategyDef::from_let_def(def, no_of_scopes)))
.map(|def| (def.name.as_str(), StrategyDef::from_let_def(def, no_of_scopes)))
.collect(),
is_overlay: false,
}
......@@ -254,18 +254,10 @@ impl<'a, ATerm> Scope<'a, ATerm> {
pub fn from_fresh_variables<I>(fresh_vars: I) -> Self
where
I: IntoIterator<Item = String>,
I: IntoIterator<Item = &'a str>,
{
Scope {
term: {
let fresh_vars = fresh_vars.into_iter();
let mut result = FnvHashMap::with_capacity_and_hasher(fresh_vars.size_hint().0, ::fnv::FnvBuildHasher::default());
// trying to use zip + iter::repeat will require ATerm: Clone, which is unnecessary
for fresh_var in fresh_vars {
result.insert(fresh_var, None);
}
result
},
term: fresh_vars.into_iter().map(|fresh_var| (fresh_var, None)).collect(),
strategy: FnvHashMap::default(),
is_overlay: false,
}
......@@ -288,9 +280,9 @@ pub mod Scopes {
.ok_or_else(|| Error::UndefinedVariable(term_name.to_owned()))
}
pub fn match_term(
scopes: &mut Vec<Scope<ATermRef>>,
term_name: &str,
pub fn match_term<'a>(
scopes: &mut Vec<Scope<'a, ATermRef>>,
term_name: &'a str,
current: &ATermRef,
) -> Result<()> {
let term = get_term_option(scopes, term_name)?;
......@@ -306,7 +298,7 @@ pub mod Scopes {
for mut scope in scopes.iter_mut().rev() {
if scope.is_overlay || scope.term.contains_key(term_name) {
if let Some(Some(t)) = scope.term.insert(
term_name.to_owned(),
term_name,
Some(current.clone()),
)
{
......
......@@ -193,7 +193,7 @@ impl<'d, 'f : 'd> Eval<'d, 'f> for preprocess::Strategy {
}
Build(ref term) => term.build(context),
Scope(ref fresh_names, ref body) => {
context.push_scope(context::Scope::from_fresh_variables(fresh_names.clone()));
context.push_scope(context::Scope::from_fresh_variables(fresh_names.iter().map(|s| s.as_str())));
let result = body.eval(context, current);
context.pop_scope();
result
......@@ -268,7 +268,7 @@ impl<'d, 'f : 'd> Eval<'d, 'f> for preprocess::Strategy {
fn eval_match<'d, 'f : 'd>(
context: &MutContext<'d, 'f>,
match_term: &preprocess::MatchTerm,
match_term: &'d preprocess::MatchTerm,
current: &ATermRef,
) -> Result<()> {
use preprocess::MatchTerm::*;
......
......@@ -74,8 +74,8 @@ impl<'a> StrategyDef<'a> {
match *self {
StrategyDef::TopLevel(ref def) => {
Scope::new(
def.targs.iter().cloned().zip(actual_targs),
def.sargs.iter().cloned().zip(actual_sargs),
def.targs.iter().map(String::as_str).zip(actual_targs),
def.sargs.iter().map(String::as_str).zip(actual_sargs),
)
}
StrategyDef::Predefined {
......@@ -83,8 +83,8 @@ impl<'a> StrategyDef<'a> {
..
} => {
Scope::new(
def.targs.iter().cloned().zip(actual_targs),
def.sargs.iter().cloned().zip(actual_sargs),
def.targs.iter().map(String::as_str).zip(actual_targs),
def.sargs.iter().map(String::as_str).zip(actual_sargs),
)
}
StrategyDef::Anonymous { .. } => Scope::new(Vec::new(), Vec::new()),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment