diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-07-20 20:21:56 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-07-21 11:28:29 +0200 |
| commit | 9488b1b850152eb564dbfefc898c962bdac73eb4 (patch) | |
| tree | 0a99487cddfee1a46b5802dc6b64c81b70e549da /bench/src/clock.rs | |
| parent | 8000783f95ee007d9dda6f1dcc1c42c8e607b122 (diff) | |
Main context struct
Diffstat (limited to 'bench/src/clock.rs')
| -rw-r--r-- | bench/src/clock.rs | 72 |
1 files changed, 23 insertions, 49 deletions
diff --git a/bench/src/clock.rs b/bench/src/clock.rs index c3dcaeb7..e33c6c13 100644 --- a/bench/src/clock.rs +++ b/bench/src/clock.rs @@ -4,37 +4,41 @@ use std::rc::Rc; use criterion::{criterion_group, criterion_main, Criterion}; -use typst::cache::Cache; -use typst::eval::{eval, Module, Scope}; -use typst::exec::{exec, State}; +use typst::eval::{eval, Module}; +use typst::exec::exec; use typst::export::pdf; use typst::layout::{layout, Frame, LayoutTree}; use typst::loading::{FileId, FsLoader}; use typst::parse::parse; use typst::syntax::SyntaxTree; -use typst::typeset; +use typst::Context; const FONT_DIR: &str = "../fonts"; const TYP_DIR: &str = "../tests/typ"; const CASES: &[&str] = &["coma.typ", "text/basic.typ"]; fn benchmarks(c: &mut Criterion) { - let ctx = Context::new(); + let loader = { + let mut loader = FsLoader::new(); + loader.search_path(FONT_DIR); + Rc::new(loader) + }; + + let ctx = Rc::new(RefCell::new(Context::new(loader.clone()))); for case in CASES { let path = Path::new(TYP_DIR).join(case); let name = path.file_stem().unwrap().to_string_lossy(); - let src_id = ctx.borrow_mut().loader.resolve_path(&path).unwrap(); + let src_id = loader.resolve_path(&path).unwrap(); let src = std::fs::read_to_string(&path).unwrap(); let case = Case::new(src_id, src, ctx.clone()); macro_rules! bench { - ($step:literal, setup = |$cache:ident| $setup:expr, code = $code:expr $(,)?) => { + ($step:literal, setup = |$ctx:ident| $setup:expr, code = $code:expr $(,)?) => { c.bench_function(&format!("{}-{}", $step, name), |b| { b.iter_batched( || { - let mut borrowed = ctx.borrow_mut(); - let $cache = &mut borrowed.cache; + let mut $ctx = ctx.borrow_mut(); $setup }, |_| $code, @@ -61,12 +65,12 @@ fn benchmarks(c: &mut Criterion) { { bench!( "layout", - setup = |cache| cache.layout.clear(), + setup = |ctx| ctx.layouts.clear(), code = case.layout(), ); bench!( "typeset", - setup = |cache| cache.layout.clear(), + setup = |ctx| ctx.layouts.clear(), code = case.typeset(), ); bench!("layout-cached", case.layout()); @@ -77,28 +81,11 @@ fn benchmarks(c: &mut Criterion) { } } -/// The context required for benchmarking a case. -struct Context { - loader: FsLoader, - cache: Cache, -} - -impl Context { - fn new() -> Rc<RefCell<Self>> { - let mut loader = FsLoader::new(); - loader.search_path(FONT_DIR); - let cache = Cache::new(&loader); - Rc::new(RefCell::new(Self { loader, cache })) - } -} - /// A test case with prepared intermediate results. struct Case { ctx: Rc<RefCell<Context>>, src_id: FileId, src: String, - scope: Scope, - state: State, ast: Rc<SyntaxTree>, module: Module, tree: LayoutTree, @@ -108,20 +95,15 @@ struct Case { impl Case { fn new(src_id: FileId, src: String, ctx: Rc<RefCell<Context>>) -> Self { let mut borrowed = ctx.borrow_mut(); - let Context { loader, cache } = &mut *borrowed; - let scope = typst::library::new(); - let state = typst::exec::State::default(); let ast = Rc::new(parse(&src).output); - let module = eval(loader, cache, src_id, Rc::clone(&ast), &scope).output; - let tree = exec(&module.template, state.clone()).output; - let frames = layout(loader, cache, &tree); + let module = eval(&mut borrowed, src_id, Rc::clone(&ast)).output; + let tree = exec(&mut borrowed, &module.template).output; + let frames = layout(&mut borrowed, &tree); drop(borrowed); Self { ctx, src_id, src, - scope, - state, ast, module, tree, @@ -135,31 +117,23 @@ impl Case { fn eval(&self) -> Module { let mut borrowed = self.ctx.borrow_mut(); - let Context { loader, cache } = &mut *borrowed; - let ast = Rc::clone(&self.ast); - eval(loader, cache, self.src_id, ast, &self.scope).output + eval(&mut borrowed, self.src_id, Rc::clone(&self.ast)).output } fn exec(&self) -> LayoutTree { - exec(&self.module.template, self.state.clone()).output + exec(&mut self.ctx.borrow_mut(), &self.module.template).output } fn layout(&self) -> Vec<Rc<Frame>> { - let mut borrowed = self.ctx.borrow_mut(); - let Context { loader, cache } = &mut *borrowed; - layout(loader, cache, &self.tree) + layout(&mut self.ctx.borrow_mut(), &self.tree) } fn typeset(&self) -> Vec<Rc<Frame>> { - let mut borrowed = self.ctx.borrow_mut(); - let Context { loader, cache } = &mut *borrowed; - let state = self.state.clone(); - typeset(loader, cache, self.src_id, &self.src, &self.scope, state).output + self.ctx.borrow_mut().typeset(self.src_id, &self.src).output } fn pdf(&self) -> Vec<u8> { - let ctx = self.ctx.borrow(); - pdf(&ctx.cache, &self.frames) + pdf(&self.ctx.borrow(), &self.frames) } } |
