Skip to content

Commit

Permalink
perf(parser): Reuse the same Kind allocation in the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Apr 21, 2018
1 parent c1c061a commit ba6e981
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 18 deletions.
3 changes: 2 additions & 1 deletion base/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ pub fn walk_mut_expr<'a, V: ?Sized + MutVisitor<'a>>(v: &mut V, e: &'a mut Spann
}
Expr::Ident(ref mut id) => v.visit_ident(id),
Expr::MacroExpansion {
ref mut replacement, ..
ref mut replacement,
..
} => v.visit_expr(replacement),
Expr::Literal(..) | Expr::Error(..) => (),
}
Expand Down
3 changes: 1 addition & 2 deletions base/src/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl KindEnv for EmptyEnv<Symbol> {
}
}


/// Kind representation
///
/// All types in gluon has a kind. Most types encountered are of the `Type` kind which
Expand Down Expand Up @@ -179,7 +178,7 @@ impl fmt::Display for ArcKind {
}
}

type_cache! { KindCache() { ArcKind, Kind } row hole typ }
type_cache! { KindCache() () { ArcKind, Kind } row hole typ }

impl<'a, F: ?Sized> Walker<'a, ArcKind> for F
where
Expand Down
4 changes: 3 additions & 1 deletion base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ extern crate serde_derive_state;
extern crate serde_state as serde;

macro_rules! type_cache {
($name: ident ($($args: ident),*) { $typ: ty, $inner_type: ident } $( $id: ident )+) => {
($name: ident ($($args: ident),*) ($($arg: ident : $arg_type: ty),*) { $typ: ty, $inner_type: ident } $( $id: ident )+) => {

#[derive(Debug, Clone)]
pub struct $name<$($args),*> {
$(pub $arg : $arg_type,)*
$(pub $id : $typ,)+
_marker: ::std::marker::PhantomData<( $($args),* )>,
}
Expand All @@ -47,6 +48,7 @@ macro_rules! type_cache {
{
pub fn new() -> Self {
$name {
$($arg: <$arg_type>::default(),)*
$(
$id : $inner_type::$id(),
)+
Expand Down
6 changes: 4 additions & 2 deletions base/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ impl TypeEnv for EmptyEnv<Symbol> {
}
}


/// Trait which is a `TypeEnv` which also provides access to the type representation of some
/// primitive types
pub trait PrimitiveEnv: TypeEnv {
Expand All @@ -71,7 +70,10 @@ impl<'a, T: ?Sized + PrimitiveEnv> PrimitiveEnv for &'a T {
}
}

type_cache! { TypeCache(Id, T) { T, Type }
type_cache! {
TypeCache(Id, T)
(kind_cache: ::kind::KindCache)
{ T, Type }
hole opaque int byte float string char
function_builtin array_builtin unit empty_row
}
Expand Down
2 changes: 1 addition & 1 deletion benches/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn prelude(b: &mut Bencher) {
b.iter(|| {
let mut symbols = Symbols::new();
let mut symbols = SymbolModule::new("".into(), &mut symbols);
let expr = parser::parse_expr(&mut symbols, &TypeCache::new(), &text)
let expr = parser::parse_expr(&mut symbols, &TypeCache::default(), &text)
.unwrap_or_else(|err| panic!("{:?}", err));
black_box(expr)
})
Expand Down
12 changes: 6 additions & 6 deletions parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ AtomicKind: ArcKind = {
use lalrpop_util::ParseError;

match id {
"_" => Ok(Kind::hole()),
"Type" => Ok(Kind::typ()),
"Row" => Ok(Kind::row()),
"_" => Ok(type_cache.kind_cache.hole()),
"Type" => Ok(type_cache.kind_cache.typ()),
"Row" => Ok(type_cache.kind_cache.row()),
id => Err(ParseError::User {
error: pos::spanned2(
l.into(),
Expand All @@ -150,7 +150,7 @@ Kind: ArcKind = {

TypeParam: Generic<Id> = {
<id : Ident> =>
Generic::new(id, Kind::hole()),
Generic::new(id, type_cache.kind_cache.hole()),

"(" <id: Ident> ":" <kind: Kind> ")" =>
Generic::new(id, kind),
Expand Down Expand Up @@ -219,7 +219,7 @@ AtomicType_: Type<Id, AstType<Id>> = {
Type::Ident(env.from_str(last))
}
Err(_) => {
Type::Generic(Generic::new(env.from_str(last), Kind::hole()))
Type::Generic(Generic::new(env.from_str(last), type_cache.kind_cache.hole()))
}
}
}
Expand Down Expand Up @@ -268,7 +268,7 @@ Type_ = {
AppType_,
"forall" <args: Ident+> "." <ty: Type> =>
Type::Forall(args.into_iter()
.map(|id| Generic::new(id, Kind::variable(0)))
.map(|id| Generic::new(id, type_cache.kind_cache.hole()))
.collect(),
ty,
None),
Expand Down
4 changes: 2 additions & 2 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ where

let mut parse_errors = Errors::new();

let type_cache = TypeCache::new();
let type_cache = TypeCache::default();

let result =
grammar::LetOrExprParser::new().parse(&type_cache, symbols, &mut parse_errors, layout);
Expand Down Expand Up @@ -442,7 +442,7 @@ pub fn parse_string<'env, 'input>(
symbols: &'env mut IdentEnv<Ident = String>,
input: &'input str,
) -> Result<SpannedExpr<String>, (Option<SpannedExpr<String>>, ParseErrors)> {
parse_partial_expr(symbols, &TypeCache::new(), input)
parse_partial_expr(symbols, &TypeCache::default(), input)
}

pub fn reparse_infix<Id>(
Expand Down
4 changes: 2 additions & 2 deletions vm/src/api/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use base::symbol::{Symbol, Symbols};

use {Error as VmError, Result};
use api::VmType;
use thread::Thread;
use thread::{Thread, ThreadInternal};

use serde::de::{self, DeserializeOwned, DeserializeSeed, EnumAccess, Error, IntoDeserializer,
MapAccess, SeqAccess, VariantAccess, Visitor};
Expand Down Expand Up @@ -37,7 +37,7 @@ fn from_rust_<T>(symbols: &mut Symbols, thread: &Thread) -> Result<(String, ArcT
where
T: DeserializeOwned,
{
let type_cache = TypeCache::new();
let type_cache = thread.global_env().type_cache();
let mut deserializer = Deserializer::from_value(&type_cache, thread, symbols);
T::deserialize(&mut deserializer)?;
let mut variants = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl GlobalVmStateBuilder {
interner: RwLock::new(Interner::new()),
gc: Mutex::new(Gc::new(Generation::default(), usize::MAX)),
macros: MacroEnv::new(),
type_cache: TypeCache::new(),
type_cache: TypeCache::default(),
generation_0_threads: RwLock::new(Vec::new()),

#[cfg(not(target_arch = "wasm32"))]
Expand Down

0 comments on commit ba6e981

Please sign in to comment.