Skip to content

Commit

Permalink
Compilation time benches (#381)
Browse files Browse the repository at this point in the history
* compilation benches

* clean

* better expect msg

* typo

* typo

* format
  • Loading branch information
edg-l authored Dec 13, 2023
1 parent 7f043b4 commit 6c5e4e2
Show file tree
Hide file tree
Showing 9 changed files with 819 additions and 13 deletions.
154 changes: 142 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ cairo-lang-starknet = "2.3.1"

[dev-dependencies]
cairo-lang-runner = "2.3.1"
criterion = { version = "0.5.1", features = ["html_reports"] }
lambdaworks-math = "0.1"
num-traits = "0.2"
pretty_assertions_sorted = "1.2.3"
Expand All @@ -88,5 +89,9 @@ opt-level = 3
[profile.dev.package."*"]
opt-level = 1

[[bench]]
name = "compile_time"
harness = false

[workspace]
members = ["runtime"]
80 changes: 80 additions & 0 deletions benches/compile_time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use cairo_native::{context::NativeContext, module_to_object};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use util::prepare_programs;

mod util;

pub fn bench_compile_time(c: &mut Criterion) {
{
let mut c = c.benchmark_group("Compilation With Independent Context");

let programs = prepare_programs();

for (program, filename) in programs {
c.bench_with_input(BenchmarkId::new(filename, 1), &program, |b, program| {
b.iter(|| {
let native_context = NativeContext::new();
native_context.compile(program).unwrap();
// pass manager internally verifies the MLIR output is correct.
})
});
}
}

{
let mut c = c.benchmark_group("Compilation With Shared Context");

let programs = prepare_programs();

let native_context = NativeContext::new();

for (program, filename) in programs {
c.bench_with_input(BenchmarkId::new(filename, 1), &program, |b, program| {
b.iter(|| {
native_context.compile(program).unwrap();
// pass manager internally verifies the MLIR output is correct.
})
});
}
}

{
let mut c = c.benchmark_group("Compilation With Independent Context To Object Code");

let programs = prepare_programs();

for (program, filename) in programs {
c.bench_with_input(BenchmarkId::new(filename, 1), &program, |b, program| {
b.iter(|| {
let native_context = NativeContext::new();
let module = native_context.compile(black_box(program)).unwrap();
let object = module_to_object(module.module())
.expect("to compile correctly to a object file");
black_box(object)
})
});
}
}

{
let mut c = c.benchmark_group("Compilation With Shared Context To Object Code");

let programs = prepare_programs();

let native_context = NativeContext::new();

for (program, filename) in programs {
c.bench_with_input(BenchmarkId::new(filename, 1), &program, |b, program| {
b.iter(|| {
let module = native_context.compile(black_box(program)).unwrap();
let object = module_to_object(module.module())
.expect("to compile correctly to a object file");
black_box(object)
})
});
}
}
}

criterion_group!(benches, bench_compile_time);
criterion_main!(benches);
22 changes: 22 additions & 0 deletions benches/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::{path::Path, sync::Arc};

use cairo_lang_sierra::program::Program;

pub fn prepare_programs() -> impl Iterator<Item = (Arc<Program>, String)> {
let programs = Path::new("programs/compile_benches")
.read_dir()
.unwrap()
.filter_map(|entry| {
let path = entry.unwrap().path();
match path.extension().map(|x| x.to_str().unwrap()) {
Some("cairo") => Some((
cairo_native::utils::cairo_to_sierra(&path),
path.file_name().unwrap().to_str().unwrap().to_string(),
)),
_ => None,
}
})
.collect::<Vec<_>>(); // collect so iter is not lazy evaluated on bench

programs.into_iter()
}
Loading

0 comments on commit 6c5e4e2

Please sign in to comment.