Skip to content

Commit

Permalink
perf(optimize): Allocate core syntax directly into arenas
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Oct 1, 2019
1 parent a29d0c6 commit 723ec4d
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 74 deletions.
2 changes: 1 addition & 1 deletion base/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<Id> AsMut<SpannedAstType<Id>> for AstType<Id> {
}
}

#[derive(Clone, Eq, PartialEq, Debug)]
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct TypedIdent<Id = Symbol, T = ArcType<Id>> {
pub typ: T,
pub name: Id,
Expand Down
21 changes: 16 additions & 5 deletions base/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ where
T: Clone + 'a,
R: std::iter::FromIterator<T>,
{
merge_iter(types, |(l, r)| f(l, r), |(l, _)| l.clone())
merge_collect(types, |(l, r)| f(l, r), |(l, _)| l.clone())
}

struct MergeIter<I, F, G, T> {
pub struct MergeIter<I, F, G, T> {
types: I,
clone_types_iter: I,
action: F,
Expand Down Expand Up @@ -123,18 +123,29 @@ where
}
}

pub fn merge_iter<I, F, G, U, R>(types: I, action: F, converter: G) -> Option<R>
impl<I, F, G, U> ExactSizeIterator for MergeIter<I, F, G, U>
where
I: ExactSizeIterator,
F: FnMut(I::Item) -> Option<U>,
G: FnMut(I::Item) -> U,
{
fn len(&self) -> usize {
self.clone_types_iter.len()
}
}

pub fn merge_collect<I, F, G, U, R>(types: I, action: F, converter: G) -> Option<R>
where
I: IntoIterator,
I::IntoIter: FusedIterator + Clone,
F: FnMut(I::Item) -> Option<U>,
G: FnMut(I::Item) -> U,
R: std::iter::FromIterator<U>,
{
merge_iter_(types, action, converter).map(|iter| iter.collect())
merge_iter(types, action, converter).map(|iter| iter.collect())
}

fn merge_iter_<I, F, G, U>(
pub fn merge_iter<I, F, G, U>(
types: I,
mut action: F,
converter: G,
Expand Down
6 changes: 3 additions & 3 deletions base/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::ast::{DisplayEnv, IdentEnv};
// FIXME Don't have a double indirection (Arc + String)
/// A symbol uniquely identifies something regardless of its name and which module it originated
/// from
#[derive(Clone, Eq)]
#[derive(Clone, Eq, Default)]
pub struct Symbol(Arc<SymbolData>);

#[derive(Debug, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -297,7 +297,7 @@ impl SymbolRef {
}
}

#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[cfg_attr(feature = "serde_derive", derive(DeserializeState))]
#[cfg_attr(feature = "serde_derive", serde(deserialize_state = "S"))]
#[cfg_attr(feature = "serde_derive", serde(de_parameters = "S"))]
Expand Down Expand Up @@ -504,7 +504,7 @@ impl Symbols {
location: None,
name,
})
}
}

/// Looks up the symbol for `name` or creates a new symbol if it does not exist
pub fn symbol<N>(&mut self, name: SymbolData<N>) -> Symbol
Expand Down
4 changes: 2 additions & 2 deletions base/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
ast::{Commented, EmptyEnv, IdentEnv},
fnv::FnvMap,
kind::{ArcKind, Kind, KindCache, KindEnv},
merge::{merge, merge_iter},
merge::{merge, merge_collect},
metadata::Comment,
pos::{BytePos, HasSpan, Span},
source::Source,
Expand Down Expand Up @@ -3864,7 +3864,7 @@ where
T: Clone + 'a,
R: std::iter::FromIterator<T>,
{
merge_iter(types, f, Clone::clone)
merge_collect(types, f, Clone::clone)
}

pub fn translate_alias<Id, T, U, F>(alias: &AliasData<Id, T>, mut translate: F) -> AliasData<Id, U>
Expand Down
4 changes: 2 additions & 2 deletions vm/src/core/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::base::{
ast::TypedIdent,
fnv::FnvSet,
kind::{ArcKind, KindEnv},
merge::merge_iter,
merge::merge_collect,
scoped_map::ScopedMap,
symbol::{Symbol, SymbolRef},
types::{Alias, ArcType, TypeEnv, TypeExt},
Expand Down Expand Up @@ -486,7 +486,7 @@ impl<'a, 'e> Compiler<'a, 'e> {
);
}
let mut result = Ok(());
let new_closures = merge_iter(
let new_closures = merge_collect(
closures,
|closure| {
function.stack.enter_scope();
Expand Down
55 changes: 54 additions & 1 deletion vm/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ pub enum Named<'a> {
Expr(&'a Expr<'a>),
}

#[derive(Clone, Debug, PartialEq)]
impl<'a> Default for Named<'a> {
fn default() -> Self {
Named::Recursive(Vec::new())
}
}

#[derive(Clone, Debug, PartialEq, Default)]
pub struct LetBinding<'a> {
pub name: TypedIdent<Symbol>,
pub expr: Named<'a>,
Expand Down Expand Up @@ -329,6 +335,53 @@ impl<'a> Allocator<'a> {
}
}

pub trait ArenaAllocatable<'a>: Sized {
fn alloc_into(self, allocator: &'a Allocator<'a>) -> &'a Self;
fn alloc_iter_into(
iter: impl IntoIterator<Item = Self>,
allocator: &'a Allocator<'a>,
) -> &'a [Self];
}

impl<'a> ArenaAllocatable<'a> for Expr<'a> {
fn alloc_into(self, allocator: &'a Allocator<'a>) -> &'a Self {
allocator.arena.alloc(self)
}

fn alloc_iter_into(
iter: impl IntoIterator<Item = Self>,
allocator: &'a Allocator<'a>,
) -> &'a [Self] {
allocator.arena.alloc_fixed(iter)
}
}

impl<'a> ArenaAllocatable<'a> for Alternative<'a> {
fn alloc_into(self, allocator: &'a Allocator<'a>) -> &'a Self {
allocator.alternative_arena.alloc(self)
}

fn alloc_iter_into(
iter: impl IntoIterator<Item = Self>,
allocator: &'a Allocator<'a>,
) -> &'a [Self] {
allocator.alternative_arena.alloc_fixed(iter)
}
}

impl<'a> ArenaAllocatable<'a> for LetBinding<'a> {
fn alloc_into(self, allocator: &'a Allocator<'a>) -> &'a Self {
allocator.let_binding_arena.alloc(self)
}

fn alloc_iter_into(
iter: impl IntoIterator<Item = Self>,
allocator: &'a Allocator<'a>,
) -> &'a [Self] {
allocator.let_binding_arena.alloc_fixed(iter)
}
}

pub(crate) trait ArenaExt<T> {
fn alloc_fixed<'a, I>(&'a self, iter: I) -> &'a mut [T]
where
Expand Down
Loading

0 comments on commit 723ec4d

Please sign in to comment.