summaryrefslogtreecommitdiff
path: root/bench/src/clock.rs
blob: b86b06dcd5d91961c2610b8bf0775ce3a5ee5204 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::cell::RefCell;
use std::path::Path;
use std::rc::Rc;

use criterion::{criterion_group, criterion_main, Criterion};

use typst::diag::TypResult;
use typst::eval::{eval, Module};
use typst::exec::exec;
use typst::export::pdf;
use typst::layout::{layout, Frame, LayoutTree};
use typst::loading::FsLoader;
use typst::parse::parse;
use typst::source::SourceId;
use typst::syntax::SyntaxTree;
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 loader = FsLoader::new().with_path(FONT_DIR).wrap();
    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 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 $(,)?) => {
                c.bench_function(&format!("{}-{}", $step, name), |b| {
                    b.iter_batched(
                        || {
                            let mut $ctx = ctx.borrow_mut();
                            $setup
                        },
                        |_| $code,
                        criterion::BatchSize::PerIteration,
                    )
                });
            };
            ($step:literal, $code:expr) => {
                c.bench_function(&format!("{}-{}", $step, name), |b| b.iter(|| $code));
            };
        }

        bench!("parse", case.parse());
        bench!("eval", case.eval());
        bench!("exec", case.exec());

        #[cfg(not(feature = "layout-cache"))]
        {
            bench!("layout", case.layout());
            bench!("typeset", case.typeset());
        }

        #[cfg(feature = "layout-cache")]
        {
            bench!(
                "layout",
                setup = |ctx| ctx.layouts.clear(),
                code = case.layout(),
            );
            bench!(
                "typeset",
                setup = |ctx| ctx.layouts.clear(),
                code = case.typeset(),
            );
            bench!("layout-cached", case.layout());
            bench!("typeset-cached", case.typeset());
        }

        bench!("pdf", case.pdf());
    }
}

/// A test case with prepared intermediate results.
struct Case {
    ctx: Rc<RefCell<Context>>,
    id: SourceId,
    ast: Rc<SyntaxTree>,
    module: Module,
    tree: LayoutTree,
    frames: Vec<Rc<Frame>>,
}

impl Case {
    fn new(ctx: Rc<RefCell<Context>>, id: SourceId) -> Self {
        let mut borrowed = ctx.borrow_mut();
        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, id, ast, module, tree, frames }
    }

    fn parse(&self) -> SyntaxTree {
        parse(self.ctx.borrow().sources.get(self.id)).unwrap()
    }

    fn eval(&self) -> TypResult<Module> {
        eval(&mut self.ctx.borrow_mut(), self.id, Rc::clone(&self.ast))
    }

    fn exec(&self) -> LayoutTree {
        exec(&mut self.ctx.borrow_mut(), &self.module.template)
    }

    fn layout(&self) -> Vec<Rc<Frame>> {
        layout(&mut self.ctx.borrow_mut(), &self.tree)
    }

    fn typeset(&self) -> TypResult<Vec<Rc<Frame>>> {
        self.ctx.borrow_mut().typeset(self.id)
    }

    fn pdf(&self) -> Vec<u8> {
        pdf(&self.ctx.borrow(), &self.frames)
    }
}

criterion_group!(benches, benchmarks);
criterion_main!(benches);