Use SmallVec for patterns

This commit is contained in:
Richard Feldman 2019-05-29 17:44:20 -04:00
parent b034e8a486
commit 6c0e34f5fc
8 changed files with 26 additions and 6 deletions

7
Cargo.lock generated
View file

@ -117,6 +117,7 @@ dependencies = [
"maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"petgraph 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)",
"pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -148,6 +149,11 @@ dependencies = [
"typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "smallvec"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "typenum"
version = "1.10.0"
@ -206,6 +212,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
"checksum sized-chunks 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a2a2eb3fe454976eefb479f78f9b394d34d661b647c6326a3a6e66f68bb12c26"
"checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be"
"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"

View file

@ -9,6 +9,7 @@ log = "0.4"
petgraph = { version = "0.4.5", optional = true }
combine = "3.8.1"
im-rc = "13.0.0"
smallvec = "0.6.9"
[dev-dependencies]
pretty_assertions = "0.5.1"

7
cli/Cargo.lock generated
View file

@ -62,6 +62,7 @@ dependencies = [
"combine 3.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"im-rc 13.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -100,6 +101,11 @@ dependencies = [
"typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "smallvec"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "typenum"
version = "1.10.0"
@ -131,6 +137,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
"checksum sized-chunks 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a2a2eb3fe454976eefb479f78f9b394d34d661b647c6326a3a6e66f68bb12c26"
"checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be"
"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"

View file

@ -7,6 +7,7 @@ use expr::Operator::*;
use std::rc::Rc;
use im_rc::hashmap::HashMap;
use self::Evaluated::*;
use smallvec::SmallVec;
pub fn eval(expr: Expr) -> Evaluated {
scoped_eval(expr, &HashMap::new())
@ -133,7 +134,7 @@ fn eval_apply(expr: Evaluated, args: Vec<Expr>, vars: &Scope) -> Evaluated {
}
#[inline(always)]
fn eval_closure(args: Vec<Evaluated>, arg_patterns: Vec<Pattern>, vars: &Scope)
fn eval_closure(args: Vec<Evaluated>, arg_patterns: SmallVec<[Pattern; 4]>, vars: &Scope)
-> Result<Scope, expr::Problem>
{
if arg_patterns.len() == args.len() {
@ -224,7 +225,7 @@ fn eval_operator(Evaluated(left_expr): &Evaluated, op: Operator, Evaluated(right
}
#[inline(always)]
fn eval_match (condition: Evaluated, branches: Vec<(Pattern, Box<Expr>)>, vars: &Scope) -> Evaluated {
fn eval_match (condition: Evaluated, branches: SmallVec<[(Pattern, Box<Expr>); 4]>, vars: &Scope) -> Evaluated {
let Evaluated(ref evaluated_expr) = condition;
for (pattern, definition) in branches {

View file

@ -1,5 +1,6 @@
use std::fmt;
use self::Expr::*;
use smallvec::SmallVec;
#[derive(Clone, Debug, PartialEq)]
pub enum Expr {
@ -17,14 +18,14 @@ pub enum Expr {
Func(String, Vec<Expr>),
Apply(Box<Expr>, Vec<Expr>),
Operator(Box<Expr>, Operator, Box<Expr>),
Closure(Vec<Pattern>, Box<Expr>),
Closure(SmallVec<[Pattern; 4]>, Box<Expr>),
// Sum Types
ApplyVariant(String, Option<Vec<Expr>>),
// Conditionals
If(Box<Expr>, Box<Expr>, Box<Expr>),
Match(Box<Expr>, Vec<(Pattern, Box<Expr>)>),
Match(Box<Expr>, SmallVec<[(Pattern, Box<Expr>); 4]>),
// Error
Error(Problem),

View file

@ -12,4 +12,5 @@ extern crate dogged;
extern crate im_rc;
extern crate smallvec;
#[macro_use] extern crate combine;

View file

@ -3,6 +3,7 @@ use expr::{Expr, Pattern};
use std::char;
use parse_state::{IndentablePosition};
use smallvec::SmallVec;
use combine::parser::char::{char, string, spaces, digit, hex_digit, HexDigit, alpha_num};
use combine::parser::repeat::{many, count_min_max, sep_by1, skip_many, skip_many1};
@ -206,7 +207,7 @@ where I: Stream<Item = char, Position = IndentablePosition>,
string("match").skip(indented_whitespaces1(min_indent))
.with(expr_body(min_indent)).skip(indented_whitespaces1(min_indent))
.and(
many::<Vec<_>, _>(
many::<SmallVec<_>, _>(
string("when").skip(indented_whitespaces1(min_indent))
.with(pattern(min_indent)).skip(indented_whitespaces1(min_indent))
.skip(string("then")).skip(indented_whitespaces1(min_indent))

View file

@ -1,4 +1,5 @@
#[macro_use] extern crate pretty_assertions;
#[macro_use] extern crate smallvec;
extern crate combine;
extern crate roc;