-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Criterion Benchmark Libfuncs and more (#385)
* bench libfuncs * bench cairo vm too * trigger * trigger * allow unused which is used * fix unique id issue * fix box and nullable from_jit * fixes * clippy
- Loading branch information
Showing
11 changed files
with
252 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use cairo_lang_runner::StarknetState; | ||
use cairo_native::{context::NativeContext, executor::NativeExecutor}; | ||
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
use util::{create_vm_runner, prepare_programs}; | ||
|
||
mod util; | ||
|
||
pub fn bench_libfuncs(c: &mut Criterion) { | ||
let programs = prepare_programs("tests/cases"); | ||
|
||
{ | ||
let mut c = c.benchmark_group("Libfunc Execution Time"); | ||
|
||
for (program, filename) in &programs { | ||
if filename == "tests/cases/felt_ops/div.cairo" { | ||
continue; // todo: enable when libfuncs felt252_div and felt252_div_const are implemented | ||
} | ||
|
||
let entry = program | ||
.funcs | ||
.iter() | ||
.find(|f| { | ||
if let Some(name) = &f.id.debug_name { | ||
name.ends_with("main") | ||
} else { | ||
false | ||
} | ||
}) | ||
.expect("failed to find entry point"); | ||
|
||
let vm_runner = create_vm_runner(program); | ||
|
||
c.bench_with_input( | ||
BenchmarkId::new(filename, "SierraCasmRunner"), | ||
&program, | ||
|b, _program| { | ||
b.iter(|| { | ||
let res = vm_runner | ||
.run_function_with_starknet_context( | ||
entry, | ||
&[], | ||
Some(usize::MAX), | ||
StarknetState::default(), | ||
) | ||
.expect("should run correctly"); | ||
black_box(res) | ||
}) | ||
}, | ||
); | ||
|
||
c.bench_with_input( | ||
BenchmarkId::new(filename, "jit-cold"), | ||
&program, | ||
|b, program| { | ||
let native_context = NativeContext::new(); | ||
b.iter(|| { | ||
let module = native_context.compile(program).unwrap(); | ||
// pass manager internally verifies the MLIR output is correct. | ||
let native_executor = NativeExecutor::new(module); | ||
|
||
// Execute the program. | ||
let result = native_executor | ||
.execute(&entry.id, &[], Some(u64::MAX as u128)) | ||
.unwrap(); | ||
black_box(result) | ||
}) | ||
}, | ||
); | ||
|
||
c.bench_with_input( | ||
BenchmarkId::new(filename, "jit-hot"), | ||
program, | ||
|b, program| { | ||
let native_context = NativeContext::new(); | ||
let module = native_context.compile(program).unwrap(); | ||
// pass manager internally verifies the MLIR output is correct. | ||
let native_executor = NativeExecutor::new(module); | ||
|
||
// warmup | ||
for _ in 0..5 { | ||
native_executor | ||
.execute(&entry.id, &[], Some(u64::MAX as u128)) | ||
.unwrap(); | ||
} | ||
|
||
b.iter(|| { | ||
// Execute the program. | ||
let result = native_executor | ||
.execute(&entry.id, &[], Some(u64::MAX as u128)) | ||
.unwrap(); | ||
black_box(result) | ||
}) | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
|
||
criterion_group!(benches, bench_libfuncs); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,32 @@ | ||
use std::{path::Path, sync::Arc}; | ||
use std::sync::Arc; | ||
|
||
use cairo_lang_runner::SierraCasmRunner; | ||
use cairo_lang_sierra::program::Program; | ||
use walkdir::WalkDir; | ||
|
||
pub fn prepare_programs() -> impl Iterator<Item = (Arc<Program>, String)> { | ||
let programs = Path::new("programs/compile_benches") | ||
.read_dir() | ||
.unwrap() | ||
pub fn prepare_programs(path: &str) -> Vec<(Arc<Program>, String)> { | ||
WalkDir::new(path) | ||
.into_iter() | ||
.filter_map(|entry| { | ||
let path = entry.unwrap().path(); | ||
let e = entry.unwrap(); | ||
let path = e.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(), | ||
cairo_native::utils::cairo_to_sierra(path), | ||
path.display().to_string(), | ||
)), | ||
_ => None, | ||
} | ||
}) | ||
.collect::<Vec<_>>(); // collect so iter is not lazy evaluated on bench | ||
.collect::<Vec<_>>() | ||
} | ||
|
||
programs.into_iter() | ||
#[allow(unused)] // its used but clippy doesn't detect it well | ||
pub fn create_vm_runner(program: &Program) -> SierraCasmRunner { | ||
SierraCasmRunner::new( | ||
program.clone(), | ||
Some(Default::default()), | ||
Default::default(), | ||
) | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.