Skip to content

Commit

Permalink
Implement Module::new_unchecked (#829)
Browse files Browse the repository at this point in the history
* use derive(Default) for ReusableAllocations

* use Self::Allocations

* make many FuncTranslator methods private

None of them needed to be public. This was a simple oversight.

* impl WasmTranslator for FuncTranslator

* rename parameter

* remove unnecessary map_err(Into::into) in macro

* implement Module::new_unchecked

* apply rustfmt

* add translation benchmarks for Module::new_unchecked
  • Loading branch information
Robbepop authored Dec 3, 2023
1 parent 5d4ea7b commit 1ce2370
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 150 deletions.
55 changes: 42 additions & 13 deletions crates/wasmi/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,56 +98,85 @@ enum FuelMetering {
Disabled,
}

fn bench_translate_for(c: &mut Criterion, name: &str, path: &str, fuel_metering: FuelMetering) {
/// How to translate a Wasm module.
enum Validation {
/// Uses [`Module::new`].
Checked,
/// Uses [`Module::new_unchecked`].
Unchecked,
}

fn bench_translate_for(
c: &mut Criterion,
name: &str,
path: &str,
validation: Validation,
fuel_metering: FuelMetering,
) {
let fuel_id = match fuel_metering {
FuelMetering::Enabled => "fuel",
FuelMetering::Disabled => "default",
};
let bench_id = format!("translate/{name}/{fuel_id}");
let safety = match validation {
Validation::Checked => "checked",
Validation::Unchecked => "unchecked",
};
let bench_id = format!("translate/{name}/{safety}/{fuel_id}");
let mut config = bench_config();
if matches!(fuel_metering, FuelMetering::Enabled) {
config.consume_fuel(true);
}
let create_module = match validation {
Validation::Checked => {
|engine: &Engine, bytes: &[u8]| -> Module { Module::new(&engine, &bytes[..]).unwrap() }
}
Validation::Unchecked => |engine: &Engine, bytes: &[u8]| -> Module {
// Safety: We made sure that all translation benchmark inputs are valid Wasm.
unsafe { Module::new_unchecked(engine, &bytes[..]).unwrap() }
},
};
c.bench_function(&bench_id, |b| {
let wasm_bytes = load_wasm_from_file(path);
b.iter(|| {
let engine = Engine::new(&config);
let _module = Module::new(&engine, &wasm_bytes[..]).unwrap();
_ = create_module(&engine, &wasm_bytes[..]);
})
});
}

fn bench_translate_for_both(c: &mut Criterion, name: &str, path: &str) {
bench_translate_for(c, name, path, FuelMetering::Disabled);
bench_translate_for(c, name, path, FuelMetering::Enabled);
fn bench_translate_for_all(c: &mut Criterion, name: &str, path: &str) {
bench_translate_for(c, name, path, Validation::Checked, FuelMetering::Disabled);
bench_translate_for(c, name, path, Validation::Checked, FuelMetering::Enabled);
bench_translate_for(c, name, path, Validation::Unchecked, FuelMetering::Disabled);
bench_translate_for(c, name, path, Validation::Unchecked, FuelMetering::Enabled);
}

fn bench_translate_wasm_kernel(c: &mut Criterion) {
bench_translate_for_both(c, "wasm_kernel", WASM_KERNEL);
bench_translate_for_all(c, "wasm_kernel", WASM_KERNEL);
}

fn bench_translate_spidermonkey(c: &mut Criterion) {
bench_translate_for_both(c, "spidermonkey", "benches/wasm/spidermonkey.wasm");
bench_translate_for_all(c, "spidermonkey", "benches/wasm/spidermonkey.wasm");
}

fn bench_translate_bz2(c: &mut Criterion) {
bench_translate_for_both(c, "bz2", "benches/wasm/bz2.wasm");
bench_translate_for_all(c, "bz2", "benches/wasm/bz2.wasm");
}

fn bench_translate_pulldown_cmark(c: &mut Criterion) {
bench_translate_for_both(c, "pulldown_cmark", "benches/wasm/pulldown-cmark.wasm");
bench_translate_for_all(c, "pulldown_cmark", "benches/wasm/pulldown-cmark.wasm");
}

fn bench_translate_erc20(c: &mut Criterion) {
bench_translate_for_both(c, "erc20", "benches/wasm/erc20.wasm");
bench_translate_for_all(c, "erc20", "benches/wasm/erc20.wasm");
}

fn bench_translate_erc721(c: &mut Criterion) {
bench_translate_for_both(c, "erc721", "benches/wasm/erc721.wasm");
bench_translate_for_all(c, "erc721", "benches/wasm/erc721.wasm");
}

fn bench_translate_erc1155(c: &mut Criterion) {
bench_translate_for_both(c, "erc1155", "benches/wasm/erc1155.wasm");
bench_translate_for_all(c, "erc1155", "benches/wasm/erc1155.wasm");
}

fn bench_instantiate_wasm_kernel(c: &mut Criterion) {
Expand Down
10 changes: 8 additions & 2 deletions crates/wasmi/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ pub use self::{
limits::StackLimits,
resumable::{ResumableCall, ResumableInvocation, TypedResumableCall, TypedResumableInvocation},
traits::{CallParams, CallResults},
translator::{Instr, TranslationError, ValidatingFuncTranslator},
translator::{Instr, TranslationError},
};
pub(crate) use self::{
config::FuelCosts,
executor::Stack,
func_args::{FuncFinished, FuncParams, FuncResults},
func_types::DedupFuncType,
translator::{FuncTranslatorAllocations, ReusableAllocations, WasmTranslator},
translator::{
FuncTranslator,
FuncTranslatorAllocations,
ReusableAllocations,
ValidatingFuncTranslator,
WasmTranslator,
},
};
use crate::{core::Trap, Func, FuncType, StoreContextMut};
use alloc::{sync::Arc, vec::Vec};
Expand Down
Loading

0 comments on commit 1ce2370

Please sign in to comment.