mirror of
https://github.com/typst/typst
synced 2025-05-16 10:05:28 +08:00
99 lines
2.6 KiB
Rust
99 lines
2.6 KiB
Rust
use std::path::Path;
|
|
|
|
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
|
|
|
use typst::eval::eval;
|
|
use typst::layout::layout;
|
|
use typst::loading::MemLoader;
|
|
use typst::parse::{parse, Scanner, TokenMode, Tokens};
|
|
use typst::source::SourceId;
|
|
use typst::Context;
|
|
|
|
const SRC: &str = include_str!("bench.typ");
|
|
const FONT: &[u8] = include_bytes!("../fonts/IBMPlexSans-Regular.ttf");
|
|
|
|
fn context() -> (Context, SourceId) {
|
|
let loader = MemLoader::new().with(Path::new("font.ttf"), FONT).wrap();
|
|
let mut ctx = Context::new(loader);
|
|
let id = ctx.sources.provide(Path::new("src.typ"), SRC.to_string());
|
|
(ctx, id)
|
|
}
|
|
|
|
fn bench_decode(c: &mut Criterion) {
|
|
c.bench_function("decode", |b| {
|
|
b.iter(|| {
|
|
// We don't use chars().count() because that has a special
|
|
// superfast implementation.
|
|
let mut count = 0;
|
|
let mut chars = black_box(SRC).chars();
|
|
while let Some(_) = chars.next() {
|
|
count += 1;
|
|
}
|
|
count
|
|
})
|
|
});
|
|
}
|
|
|
|
fn bench_scan(c: &mut Criterion) {
|
|
c.bench_function("scan", |b| {
|
|
b.iter(|| {
|
|
let mut count = 0;
|
|
let mut scanner = Scanner::new(black_box(SRC));
|
|
while let Some(_) = scanner.eat() {
|
|
count += 1;
|
|
}
|
|
count
|
|
})
|
|
});
|
|
}
|
|
|
|
fn bench_tokenize(c: &mut Criterion) {
|
|
c.bench_function("tokenize", |b| {
|
|
b.iter(|| Tokens::new(black_box(SRC), black_box(TokenMode::Markup)).count())
|
|
});
|
|
}
|
|
|
|
fn bench_parse(c: &mut Criterion) {
|
|
c.bench_function("parse", |b| b.iter(|| parse(SRC)));
|
|
}
|
|
|
|
fn bench_edit(c: &mut Criterion) {
|
|
let (mut ctx, id) = context();
|
|
c.bench_function("edit", |b| {
|
|
b.iter(|| black_box(ctx.sources.edit(id, 1168 .. 1171, "_Uhr_")))
|
|
});
|
|
}
|
|
|
|
fn bench_eval(c: &mut Criterion) {
|
|
let (mut ctx, id) = context();
|
|
let ast = ctx.sources.get(id).ast().unwrap();
|
|
c.bench_function("eval", |b| b.iter(|| eval(&mut ctx, id, &ast).unwrap()));
|
|
}
|
|
|
|
fn bench_to_tree(c: &mut Criterion) {
|
|
let (mut ctx, id) = context();
|
|
let module = ctx.evaluate(id).unwrap();
|
|
c.bench_function("to_tree", |b| {
|
|
b.iter(|| module.template.to_pages(ctx.style()))
|
|
});
|
|
}
|
|
|
|
fn bench_layout(c: &mut Criterion) {
|
|
let (mut ctx, id) = context();
|
|
let tree = ctx.execute(id).unwrap();
|
|
c.bench_function("layout", |b| b.iter(|| layout(&mut ctx, &tree)));
|
|
}
|
|
|
|
criterion_group!(
|
|
benches,
|
|
bench_decode,
|
|
bench_scan,
|
|
bench_tokenize,
|
|
bench_parse,
|
|
bench_edit,
|
|
bench_eval,
|
|
bench_to_tree,
|
|
bench_layout
|
|
);
|
|
criterion_main!(benches);
|