Skip to content

Commit

Permalink
Improve clippy CI and fix some benchmark clippy warnings (#1081)
Browse files Browse the repository at this point in the history
* add clippy job for linting benchmark code

* fix warning that will become a hard error in the future

* fix some benchmark clippy warnings

Not sure if those warnings are useful to begin with ...

* apply rustfmt
  • Loading branch information
Robbepop authored Jun 22, 2024
1 parent c1c5af9 commit d088395
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ jobs:
run: cargo clippy --workspace --no-default-features -- -D warnings
- name: Clippy (tests)
run: cargo clippy --workspace --tests -- -D warnings
- name: Clippy (benches)
run: cargo clippy --workspace --benches -- -D warnings
- name: Clippy (fuzz)
run: pushd fuzz && cargo clippy -- -D warnings && popd

Expand Down
43 changes: 24 additions & 19 deletions crates/wasmi/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,21 +590,21 @@ fn bench_instantiate_contract(c: &mut Criterion, name: &str, path: &str) {
.define(
"seal0",
"seal_value_transferred",
Func::wrap(&mut store, |_0: i32, _1: i32| unimplemented!()),
Func::wrap(&mut store, |_0: i32, _1: i32| ()),
)
.unwrap();
linker
.define(
"seal0",
"seal_input",
Func::wrap(&mut store, |_0: i32, _1: i32| unimplemented!()),
Func::wrap(&mut store, |_0: i32, _1: i32| ()),
)
.unwrap();
linker
.define(
"seal0",
"seal_caller",
Func::wrap(&mut store, |_0: i32, _1: i32| unimplemented!()),
Func::wrap(&mut store, |_0: i32, _1: i32| ()),
)
.unwrap();
linker
Expand All @@ -629,10 +629,7 @@ fn bench_instantiate_contract(c: &mut Criterion, name: &str, path: &str) {
.define(
"seal0",
"seal_deposit_event",
Func::wrap(
&mut store,
|_0: i32, _1: i32, _2: i32, _3: i32| unimplemented!(),
),
Func::wrap(&mut store, |_0: i32, _1: i32, _2: i32, _3: i32| ()),
)
.unwrap();
linker
Expand Down Expand Up @@ -662,14 +659,14 @@ fn bench_instantiate_contract(c: &mut Criterion, name: &str, path: &str) {
.define(
"seal0",
"seal_return",
Func::wrap(&mut store, |_0: i32, _1: i32, _2: i32| unimplemented!()),
Func::wrap(&mut store, |_0: i32, _1: i32, _2: i32| ()),
)
.unwrap();
linker
.define(
"seal0",
"seal_hash_blake2_256",
Func::wrap(&mut store, |_0: i32, _1: i32, _2: i32| unimplemented!()),
Func::wrap(&mut store, |_0: i32, _1: i32, _2: i32| ()),
)
.unwrap();
b.iter(|| {
Expand Down Expand Up @@ -1386,7 +1383,7 @@ fn bench_execute_vec_add(c: &mut Criterion) {
fn test_for<A, B>(
b: &mut Bencher,
vec_add: Func,
mut store: &mut Store<()>,
store: &mut Store<()>,
mem: Memory,
len: usize,
vec_a: A,
Expand All @@ -1404,16 +1401,24 @@ fn bench_execute_vec_add(c: &mut Criterion) {
let ptr_b = ptr_a + len_a;

// Reset `result` buffer to zeros:
mem.data_mut(&mut store)[ptr_result..ptr_result + (len * size_of::<i32>())].fill(0);
mem.data_mut(&mut *store)[ptr_result..ptr_result + (len * size_of::<i32>())].fill(0);
// Initialize `a` buffer:
for (n, a) in vec_a.into_iter().take(len).enumerate() {
mem.write(&mut store, ptr_a + (n * size_of::<i32>()), &a.to_le_bytes())
.unwrap();
mem.write(
&mut *store,
ptr_a + (n * size_of::<i32>()),
&a.to_le_bytes(),
)
.unwrap();
}
// Initialize `b` buffer:
for (n, b) in vec_b.into_iter().take(len).enumerate() {
mem.write(&mut store, ptr_b + (n * size_of::<i32>()), &b.to_le_bytes())
.unwrap();
mem.write(
&mut *store,
ptr_b + (n * size_of::<i32>()),
&b.to_le_bytes(),
)
.unwrap();
}

// Prepare parameters and all Wasm `vec_add`:
Expand All @@ -1424,25 +1429,25 @@ fn bench_execute_vec_add(c: &mut Criterion) {
Val::I32(len as i32),
];
b.iter(|| {
vec_add.call(&mut store, &params, &mut []).unwrap();
vec_add.call(&mut *store, &params, &mut []).unwrap();
});

// Validate the result buffer:
for n in 0..len {
let mut buffer4 = [0x00; 4];
let mut buffer8 = [0x00; 8];
let a = {
mem.read(&store, ptr_a + (n * size_of::<i32>()), &mut buffer4)
mem.read(&*store, ptr_a + (n * size_of::<i32>()), &mut buffer4)
.unwrap();
i32::from_le_bytes(buffer4)
};
let b = {
mem.read(&store, ptr_b + (n * size_of::<i32>()), &mut buffer4)
mem.read(&*store, ptr_b + (n * size_of::<i32>()), &mut buffer4)
.unwrap();
i32::from_le_bytes(buffer4)
};
let actual_result = {
mem.read(&store, ptr_result + (n * size_of::<i64>()), &mut buffer8)
mem.read(&*store, ptr_result + (n * size_of::<i64>()), &mut buffer8)
.unwrap();
i64::from_le_bytes(buffer8)
};
Expand Down

0 comments on commit d088395

Please sign in to comment.