Skip to content

Commit

Permalink
Merge pull request #47 from lykia-rs/feature/optimizations
Browse files Browse the repository at this point in the history
feature/optimizations
  • Loading branch information
can-keklik authored Dec 15, 2024
2 parents 39d682a + e7454f5 commit 32caa09
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 128 deletions.
18 changes: 13 additions & 5 deletions lykiadb-lang/benches/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use lykiadb_lang::{parser::{program::Program, resolver::Resolver, Parser}, tokenizer::scanner::Scanner, Locals, Scopes};
use lykiadb_lang::{
parser::{program::Program, resolver::Resolver, Parser},
tokenizer::scanner::Scanner,
Locals, Scopes,
};
use rustc_hash::FxHashMap;

pub struct ParserBenchmark {
scopes: Scopes,
locals: Locals,
}

impl Default for ParserBenchmark {
fn default() -> Self {
Self::new()
}
}

impl ParserBenchmark {
pub fn new() -> ParserBenchmark {
Expand Down Expand Up @@ -49,16 +58,15 @@ fn runtime() {
EXCEPT
SELECT * FROM books;
".to_string();
"
.to_string();
let mut parser = black_box(ParserBenchmark::new());
black_box(parser.process(black_box(&content)));
}

fn bench(c: &mut Criterion) {
let mut group = c.benchmark_group("sample-size-example");
group.bench_function("2-way join", |b| {
b.iter(|| runtime())
});
group.bench_function("2-way join", |b| b.iter(runtime));
group.finish();
}

Expand Down
9 changes: 1 addition & 8 deletions lykiadb-lang/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,10 @@ pub mod test {
});
assert_eq!(visited, vec![3, 1, 2]);
}

}

#[test]
fn test_expr_get_id() {

// Test Variable
let var_expr = Expr::Variable {
name: Identifier::new("test_var", false),
Expand Down Expand Up @@ -822,7 +820,6 @@ pub mod test {
fn test_expr_get_span() {
let test_span = Span::default();


// Test Variable
let var_expr = Expr::Variable {
name: Identifier::new("test_var", false),
Expand Down Expand Up @@ -1073,10 +1070,7 @@ pub mod test {
fn test_function_display() {
let func = Expr::Function {
name: Some(Identifier::new("test_func", false)),
parameters: vec![
Identifier::new("a", false),
Identifier::new("b", false),
],
parameters: vec![Identifier::new("a", false), Identifier::new("b", false)],
body: Arc::new(vec![]),
span: Span::default(),
id: 1,
Expand Down Expand Up @@ -1139,7 +1133,6 @@ pub mod test {
assert_eq!(call.to_string(), "test_func(Num(1.0), Num(2.0))");
}


pub fn create_simple_add_expr(id: usize, left: f64, right: f64) -> Expr {
Expr::Binary {
left: Box::new(Expr::Literal {
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-lang/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ macro_rules! optional_with_expected {
};
}

impl<'a> Parser<'a> {
impl Parser<'_> {
fn sql_insert(&mut self) -> ParseResult<Box<Expr>> {
if !self.match_next(&skw!(Insert)) {
return self.sql_update();
Expand Down
8 changes: 2 additions & 6 deletions lykiadb-lang/src/parser/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ impl<'a> Resolver<'a> {
) -> Resolver<'a> {
Resolver {
scopes,
locals: if let Some(previous_locals) = previous_locals {
previous_locals
} else {
FxHashMap::default()
},
locals: previous_locals.unwrap_or_default(),
program,
}
}
Expand Down Expand Up @@ -91,7 +87,7 @@ impl<'a> Resolver<'a> {
}
}

impl<'a> VisitorMut<(), ResolveError> for Resolver<'a> {
impl VisitorMut<(), ResolveError> for Resolver<'_> {
fn visit_expr(&mut self, e: &Expr) -> Result<(), ResolveError> {
match e {
Expr::Literal { value, .. } => match value {
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-lang/src/tokenizer/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum ScanError {
MalformedNumberLiteral { span: Span },
}

impl<'a> Scanner<'a> {
impl Scanner<'_> {
pub fn scan(source: &str) -> Result<Vec<Token>, ScanError> {
let mut scanner = Scanner {
chars: source.chars().enumerate().peekable(),
Expand Down
3 changes: 2 additions & 1 deletion lykiadb-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ tokio-stream = { version = "~0.1.6", features = ["net"] }
tracing = "0.1"
tracing-subscriber = "0.3"
pretty_assertions = "1.4.1"
string-interner = "0.18.0"

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
test_each_file = "0.3.4"

[[bench]]
name = "interpreter"
harness = false
harness = false
27 changes: 16 additions & 11 deletions lykiadb-server/src/engine/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ impl From<ScanError> for ExecutionError {
}
}

pub fn report_error(source_name: &str, source: &str, error: ExecutionError, mut writer: impl std::io::Write) {
pub fn report_error(
source_name: &str,
source: &str,
error: ExecutionError,
mut writer: impl std::io::Write,
) {
use ariadne::{Color, Label, Report, ReportKind, Source};

// Generate & choose some colours for each of our elements
Expand All @@ -54,7 +59,8 @@ pub fn report_error(source_name: &str, source: &str, error: ExecutionError, mut
.with_color(out),
)
.finish()
.write((source_name, Source::from(&source)), &mut writer).unwrap();
.write((source_name, Source::from(&source)), &mut writer)
.unwrap();
};

match error {
Expand Down Expand Up @@ -87,11 +93,7 @@ pub fn report_error(source_name: &str, source: &str, error: ExecutionError, mut
);
}
ExecutionError::Parse(ParseError::NoTokens) => {
print(
"There is nothing to parse",
"",
Span::default(),
);
print("There is nothing to parse", "", Span::default());
}
ExecutionError::Parse(ParseError::InvalidAssignmentTarget { left }) => {
print(
Expand Down Expand Up @@ -167,18 +169,21 @@ pub fn report_error(source_name: &str, source: &str, error: ExecutionError, mut
}
#[cfg(test)]
mod tests {
use core::panic;


use super::*;
use lykiadb_lang::{kw, sym, tokenizer::token::{Keyword, Symbol, Token, TokenType}, Identifier, Literal};
use lykiadb_lang::{
kw, sym,
tokenizer::token::{Keyword, Symbol, Token, TokenType},
Identifier, Literal,
};

fn capture_error_output(filename: &str, source: &str, error: ExecutionError) -> String {
let mut output = Vec::new();
report_error(filename, source, error, &mut output);
String::from_utf8(output).unwrap()
}


// Scanner Error Tests
#[test]
fn test_scanner_unterminated_string() {
Expand Down Expand Up @@ -230,7 +235,7 @@ mod tests {
},
literal: None,
},
expected: TokenType::Identifier { dollar: true }
expected: TokenType::Identifier { dollar: true },
});

let output = capture_error_output("test.txt", source, error);
Expand Down
Loading

0 comments on commit 32caa09

Please sign in to comment.