From 46a67927d85dd593ec479e9633ca80471faae10f Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Sat, 30 Dec 2023 18:33:42 -0800 Subject: [PATCH] Add benchmark test for the loader 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. --- benches/parse.rs | 19 +++++++++++++++++-- src/lib.rs | 2 +- src/load.rs | 6 +++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/benches/parse.rs b/benches/parse.rs index aa29db6..a96873c 100644 --- a/benches/parse.rs +++ b/benches/parse.rs @@ -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 @@ -31,6 +32,7 @@ impl n2::parse::Loader for NoOpLoader { fn generate_build_ninja() -> Vec { let mut buf: Vec = Vec::new(); + write!(buf, "rule cc\n command = touch $out",).unwrap(); for i in 0..1000 { write!( buf, @@ -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); diff --git a/src/lib.rs b/src/lib.rs index 50e258f..fea575a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ mod depfile; mod eval; mod graph; mod hash; -mod load; +pub mod load; pub mod parse; mod process; #[cfg(unix)] diff --git a/src/load.rs b/src/load.rs index 51c4c6f..bf23f5f 100644 --- a/src/load.rs +++ b/src/load.rs @@ -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, /// rule name -> list of (key, val) @@ -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()); @@ -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);