From 3932bb2cb93be95d67fc56998423eb9ce047fdfa Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 9 Aug 2021 11:06:37 +0200 Subject: New source loading architecture --- bench/src/clock.rs | 31 +++++++++++++------------------ bench/src/parsing.rs | 4 +--- 2 files changed, 14 insertions(+), 21 deletions(-) (limited to 'bench/src') diff --git a/bench/src/clock.rs b/bench/src/clock.rs index 0f684f85..b86b06dc 100644 --- a/bench/src/clock.rs +++ b/bench/src/clock.rs @@ -11,7 +11,7 @@ use typst::export::pdf; use typst::layout::{layout, Frame, LayoutTree}; use typst::loading::FsLoader; use typst::parse::parse; -use typst::source::SourceFile; +use typst::source::SourceId; use typst::syntax::SyntaxTree; use typst::Context; @@ -21,15 +21,13 @@ const CASES: &[&str] = &["coma.typ", "text/basic.typ"]; fn benchmarks(c: &mut Criterion) { let loader = FsLoader::new().with_path(FONT_DIR).wrap(); - let ctx = Rc::new(RefCell::new(Context::new(loader.clone()))); + let ctx = Rc::new(RefCell::new(Context::new(loader))); for case in CASES { let path = Path::new(TYP_DIR).join(case); let name = path.file_stem().unwrap().to_string_lossy(); - let file = loader.resolve(&path).unwrap(); - let src = std::fs::read_to_string(&path).unwrap(); - let source = SourceFile::new(file, src); - let case = Case::new(ctx.clone(), source); + let id = ctx.borrow_mut().sources.load(&path).unwrap(); + let case = Case::new(ctx.clone(), id); macro_rules! bench { ($step:literal, setup = |$ctx:ident| $setup:expr, code = $code:expr $(,)?) => { @@ -82,7 +80,7 @@ fn benchmarks(c: &mut Criterion) { /// A test case with prepared intermediate results. struct Case { ctx: Rc>, - source: SourceFile, + id: SourceId, ast: Rc, module: Module, tree: LayoutTree, @@ -90,26 +88,23 @@ struct Case { } impl Case { - fn new(ctx: Rc>, source: SourceFile) -> Self { + fn new(ctx: Rc>, id: SourceId) -> Self { let mut borrowed = ctx.borrow_mut(); - let ast = Rc::new(parse(&source).unwrap()); - let module = eval(&mut borrowed, source.file(), Rc::clone(&ast)).unwrap(); + let source = borrowed.sources.get(id); + let ast = Rc::new(parse(source).unwrap()); + let module = eval(&mut borrowed, id, Rc::clone(&ast)).unwrap(); let tree = exec(&mut borrowed, &module.template); let frames = layout(&mut borrowed, &tree); drop(borrowed); - Self { ctx, source, ast, module, tree, frames } + Self { ctx, id, ast, module, tree, frames } } fn parse(&self) -> SyntaxTree { - parse(&self.source).unwrap() + parse(self.ctx.borrow().sources.get(self.id)).unwrap() } fn eval(&self) -> TypResult { - eval( - &mut self.ctx.borrow_mut(), - self.source.file(), - Rc::clone(&self.ast), - ) + eval(&mut self.ctx.borrow_mut(), self.id, Rc::clone(&self.ast)) } fn exec(&self) -> LayoutTree { @@ -121,7 +116,7 @@ impl Case { } fn typeset(&self) -> TypResult>> { - self.ctx.borrow_mut().typeset(&self.source) + self.ctx.borrow_mut().typeset(self.id) } fn pdf(&self) -> Vec { diff --git a/bench/src/parsing.rs b/bench/src/parsing.rs index dd5e1279..f95dfe75 100644 --- a/bench/src/parsing.rs +++ b/bench/src/parsing.rs @@ -1,7 +1,6 @@ use iai::{black_box, main}; use typst::diag::TypResult; -use typst::loading::FileId; use typst::parse::{parse, Scanner, TokenMode, Tokens}; use typst::source::SourceFile; use typst::syntax::SyntaxTree; @@ -33,8 +32,7 @@ fn bench_tokenize() -> usize { } fn bench_parse() -> TypResult { - let source = SourceFile::new(FileId::from_raw(0), black_box(SRC).into()); - parse(&source) + parse(&SourceFile::detached(black_box(SRC))) } main!(bench_decode, bench_scan, bench_tokenize, bench_parse); -- cgit v1.2.3