Skip to content

Commit

Permalink
Add benchmark test for the loader
Browse files Browse the repository at this point in the history
I'm making some changes that affect the performance
of the parser and the loader, and would like to
benchmark them together.

Unfortunately this required making a bunch of things
public (#[cfg(test)] doesn't seem to apply for
benchmarks), which isn't great.
  • Loading branch information
Colecf committed Dec 31, 2023
1 parent d5cec94 commit 46a6792
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
19 changes: 17 additions & 2 deletions benches/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use std::io::Write;
use n2::parse::Loader;
use std::{io::Write, path::PathBuf, str::FromStr};

pub fn bench_canon(c: &mut Criterion) {
// TODO switch to canon_path_fast
Expand Down Expand Up @@ -31,6 +32,7 @@ impl n2::parse::Loader for NoOpLoader {

fn generate_build_ninja() -> Vec<u8> {
let mut buf: Vec<u8> = Vec::new();
write!(buf, "rule cc\n command = touch $out",).unwrap();
for i in 0..1000 {
write!(
buf,
Expand Down Expand Up @@ -85,5 +87,18 @@ pub fn bench_parse(c: &mut Criterion) {
};
}

criterion_group!(benches, bench_canon, bench_parse);
fn bench_load_synthetic(c: &mut Criterion) {
let mut input = generate_build_ninja();
input.push(0);
c.bench_function("load synthetic build.ninja", |b| {
b.iter(|| {
let mut loader = n2::load::Loader::new();
loader
.parse(PathBuf::from_str("build.ninja").unwrap(), &input)
.unwrap();
})
});
}

criterion_group!(benches, bench_canon, bench_parse, bench_load_synthetic);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod depfile;
mod eval;
mod graph;
mod hash;
mod load;
pub mod load;
pub mod parse;
mod process;
#[cfg(unix)]
Expand Down
6 changes: 3 additions & 3 deletions src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'a> eval::Env for BuildImplicitVars<'a> {

/// Internal state used while loading.
#[derive(Default)]
struct Loader {
pub struct Loader {
graph: graph::Graph,
default: Vec<FileId>,
/// rule name -> list of (key, val)
Expand All @@ -64,7 +64,7 @@ impl parse::Loader for Loader {
}

impl Loader {
fn new() -> Self {
pub fn new() -> Self {
let mut loader = Loader::default();

loader.rules.insert("phony".to_owned(), SmallMap::default());
Expand Down Expand Up @@ -167,7 +167,7 @@ impl Loader {
self.parse(path, &bytes)
}

fn parse(&mut self, path: PathBuf, bytes: &[u8]) -> anyhow::Result<()> {
pub fn parse(&mut self, path: PathBuf, bytes: &[u8]) -> anyhow::Result<()> {
let filename = std::rc::Rc::new(path);

let mut parser = parse::Parser::new(&bytes);
Expand Down

0 comments on commit 46a6792

Please sign in to comment.