Add benchmarks and add tiny performance improvements (#6)

* Add some benchmarks

* Replace extend -> extend_from_slice (1-2% performance improvement)

* Add many_blocks benchmarks
This commit is contained in:
Alexander Theißen
2022-01-24 21:20:47 +01:00
committed by GitHub
parent 184b3f8b3a
commit 8291876394
15 changed files with 62 additions and 2 deletions
+9
View File
@@ -11,11 +11,20 @@ categories = ["wasm", "no-std"]
repository = "https://github.com/paritytech/wasm-instrument"
include = ["src/**/*", "LICENSE-*", "README.md"]
[[bench]]
name = "benches"
harness = false
[profile.bench]
lto = "fat"
codegen-units = 1
[dependencies]
parity-wasm = { version = "0.42", default-features = false }
[dev-dependencies]
binaryen = "0.12"
criterion = "0.3"
diff = "0.1"
rand = "0.8"
wabt = "0.10"
+51
View File
@@ -0,0 +1,51 @@
use criterion::{
criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, Criterion,
Throughput,
};
use std::{
fs::{read, read_dir},
path::PathBuf,
};
use wasm_instrument::{
gas_metering, inject_stack_limiter,
parity_wasm::{deserialize_buffer, elements::Module},
};
fn fixture_dir() -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("benches");
path.push("fixtures");
path
}
fn any_fixture<F, M>(group: &mut BenchmarkGroup<M>, f: F)
where
F: Fn(Module),
M: Measurement,
{
for entry in read_dir(fixture_dir()).unwrap() {
let entry = entry.unwrap();
let bytes = read(&entry.path()).unwrap();
group.throughput(Throughput::Bytes(bytes.len().try_into().unwrap()));
group.bench_function(entry.file_name().to_str().unwrap(), |bench| {
bench.iter(|| f(deserialize_buffer(&bytes).unwrap()))
});
}
}
pub fn gas_metering(c: &mut Criterion) {
let mut group = c.benchmark_group("Gas Metering");
any_fixture(&mut group, |module| {
gas_metering::inject(module, &gas_metering::ConstantCostRules::default(), "env").unwrap();
});
}
pub fn stack_height_limiter(c: &mut Criterion) {
let mut group = c.benchmark_group("Stack Height Limiter");
any_fixture(&mut group, |module| {
inject_stack_limiter(module, 128).unwrap();
});
}
criterion_group!(benches, gas_metering, stack_height_limiter);
criterion_main!(benches);
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -286,7 +286,7 @@ fn instrument_function(ctx: &mut Context, func: &mut Instructions) -> Result<(),
ctx.stack_height_global_idx(),
ctx.stack_limit()
);
new_instrs.extend(new_seq);
new_instrs.extend_from_slice(&new_seq);
true
} else {
false
+1 -1
View File
@@ -82,7 +82,7 @@ pub fn generate_thunks(
for (arg_idx, _) in thunk.signature.params().iter().enumerate() {
thunk_body.push(elements::Instruction::GetLocal(arg_idx as u32));
}
thunk_body.extend(instrumented_call.iter().cloned());
thunk_body.extend_from_slice(&instrumented_call);
thunk_body.push(elements::Instruction::End);
// TODO: Don't generate a signature, but find an existing one.