Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and modernize Rust based tiny_keccak benchmark #1107

Merged
merged 7 commits into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -29,9 +29,31 @@ jobs:
- name: Build (no_std + no-hash-maps)
run: cargo build --locked -p wasmi_collections --no-default-features --features no-hash-maps --verbose
- name: Build (no_std)
run: cargo build --workspace --locked --lib --no-default-features --target x86_64-unknown-none --verbose --exclude wasmi_cli --exclude wasmi_wasi --exclude wasmi_fuzz
run: >-
cargo build
--workspace
--locked
--lib
--no-default-features
--target x86_64-unknown-none
--verbose
--exclude wasmi_cli
--exclude wasmi_wasi
--exclude wasmi_fuzz
--exclude wasmi_benches_tiny_keccak
- name: Build (wasm32)
run: cargo build --workspace --locked --lib --no-default-features --target wasm32-unknown-unknown --verbose --exclude wasmi_cli --exclude wasmi_wasi --exclude wasmi_fuzz
run: >-
cargo build
--workspace
--locked
--lib
--no-default-features
--target wasm32-unknown-unknown
--verbose
--exclude wasmi_cli
--exclude wasmi_wasi
--exclude wasmi_fuzz
--exclude wasmi_benches_tiny_keccak

test-asan:
name: Test (Address Sanitizer)
16 changes: 16 additions & 0 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ members = [
"crates/core",
"crates/wasmi",
"crates/wasi",
"crates/wasmi/benches/rust/tiny_keccak",
"fuzz",
]
exclude = []
@@ -32,3 +33,11 @@ num-traits = { version = "0.2.8", default-features = false }
[profile.bench]
lto = "fat"
codegen-units = 1

[profile.wasm]
inherits = "release"
opt-level = 3
lto = "fat"
codegen-units = 1
panic = "abort"
strip = "symbols"
22 changes: 13 additions & 9 deletions crates/wasmi/benches/benches.rs
Original file line number Diff line number Diff line change
@@ -696,17 +696,21 @@ fn bench_instantiate_erc1155(c: &mut Criterion) {

fn bench_execute_tiny_keccak(c: &mut Criterion) {
c.bench_function("execute/tiny_keccak", |b| {
let (mut store, instance) = load_instance_from_file(WASM_KERNEL);
let prepare = instance
.get_typed_func::<(), i32>(&store, "prepare_tiny_keccak")
.unwrap();
let keccak = instance
.get_typed_func::<i32, ()>(&store, "bench_tiny_keccak")
let (mut store, instance) = load_instance_from_file("benches/rust/tiny_keccak.wasm");
let data_ptr = instance
.get_typed_func::<(), i32>(&store, "setup")
.unwrap()
.call(&mut store, ())
.unwrap();
let test_data_ptr = prepare.call(&mut store, ()).unwrap();
let keccak = instance.get_typed_func::<i32, ()>(&store, "run").unwrap();
b.iter(|| {
keccak.call(&mut store, test_data_ptr).unwrap();
})
keccak.call(&mut store, data_ptr).unwrap();
});
instance
.get_typed_func::<i32, ()>(&store, "teardown")
.unwrap()
.call(&mut store, data_ptr)
.unwrap();
});
}

Binary file added crates/wasmi/benches/rust/tiny_keccak.wasm
Binary file not shown.
13 changes: 13 additions & 0 deletions crates/wasmi/benches/rust/tiny_keccak/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "wasmi_benches_tiny_keccak"
version = "0.0.0"
publish = false
authors.workspace = true
edition.workspace = true

[lib]
path = "lib.rs"
crate-type = ["cdylib"]

[dependencies]
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
35 changes: 35 additions & 0 deletions crates/wasmi/benches/rust/tiny_keccak/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Benchmark - Tiny Keccak

Rust based `tiny_keccak` benchmark test case.

Build with

```
cargo build \
--package wasmi_benches_tiny_keccak \
--profile wasm \
--target wasm32-unknown-unknown
```

Post-optimize with `wasm-opt` using:

```
wasm-opt \
-O3 \
target/wasm32-unknown-unknown/wasm/wasmi_benches_tiny_keccak.wasm \
-o target/wasm32-unknown-unknown/wasm/wasmi_benches_tiny_keccak.opt.wasm
```

Finally move the post-optimized Wasm binary into the proper place:

```
cp \
target/wasm32-unknown-unknown/wasm/wasmi_benches_tiny_keccak.opt.wasm \
crates/wasmi/benches/rust/tiny_keccak.wasm
```

Benchmark with:

```
cargo bench --bench benches execute/tiny_keccak
```
27 changes: 27 additions & 0 deletions crates/wasmi/benches/rust/tiny_keccak/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use tiny_keccak::{Hasher, Keccak};

/// The data required to run the `tiny_keccak` benchmark.
pub struct TinyKeccakData {
/// The input data to the tiny keccak hasher.
input: [u8; 4096],
/// The buffer to store the hash result.
result: [u8; 32],
}

#[no_mangle]
pub extern "C" fn setup() -> Box<TinyKeccakData> {
Box::new(TinyKeccakData {
input: [254_u8; 4096],
result: [0x0_u8; 32],
})
}

#[no_mangle]
pub extern "C" fn teardown(_: Box<TinyKeccakData>) {}

#[no_mangle]
pub extern "C" fn run(data: &mut TinyKeccakData) {
let mut keccak = Keccak::v256();
keccak.update(&data.input[..]);
keccak.finalize(&mut data.result[..]);
}