Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 6850e8b

Browse files
chore: overall code cleanup
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 712b7da commit 6850e8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+377
-470
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ test-2="test --package tinywasm --test test-two --release -- --enable "
66
test-wast="test --package tinywasm --test test-wast -- --enable "
77
test-wast-release="test --package tinywasm --test test-wast --release -- --enable "
88
generate-charts="test --package tinywasm --test generate-charts -- --enable "
9+
benchmark="bench -p benchmarks --bench"

BENCHMARKS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All benchmarks are run on a Ryzen 7 5800X with 32GB of RAM, running Linux 6.6.
44
WebAssembly files are optimized using [wasm-opt](https://github.com/WebAssembly/binaryen),
5-
and the benchmark code is available in the `benches` folder.
5+
and the benchmark code is available in the `crates/benchmarks` folder.
66

77
These are mainly preliminary benchmarks, and I will be adding more in the future that are also looking into memory usage and other metrics.
88

@@ -67,15 +67,15 @@ After profiling and fixing some low-hanging fruits, I found the biggest bottlene
6767
Benchmarks are run using [Criterion.rs](https://github.com/bheisler/criterion.rs). To run a benchmark, use the following command:
6868

6969
```sh
70-
$ cargo bench --bench <name>
70+
$ cargo benchmark <name>
7171
```
7272

7373
# Profiling
7474

7575
To profile a benchmark, use the following command:
7676

7777
```sh
78-
$ cargo flamegraph --bench <name> -- --bench
78+
$ cargo flamegraph -p benchmarks --bench <name> -- --bench
7979
```
8080

8181
This will generate a flamegraph in `flamegraph.svg` and a `perf.data` file.

Cargo.lock

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members=["crates/*"]
3+
default-members=[".", "crates/tinywasm", "crates/types", "crates/parser"]
34
resolver="2"
45

56
[profile.wasm]
@@ -25,31 +26,13 @@ edition="2021"
2526
name="wasm-rust"
2627
test=false
2728

28-
[[bench]]
29-
name="selfhosted"
30-
harness=false
31-
32-
[[bench]]
33-
name="fibonacci"
34-
harness=false
35-
36-
37-
[[bench]]
38-
name="argon2id"
39-
harness=false
29+
[dev-dependencies]
30+
color-eyre="0.6"
31+
tinywasm={path="crates/tinywasm", features=["unsafe"]}
32+
wat={version="1.0"}
4033

4134
[profile.bench]
4235
opt-level=3
4336
lto="thin"
4437
codegen-units=1
4538
debug=true
46-
47-
[dev-dependencies]
48-
color-eyre="0.6"
49-
criterion={version="0.5", features=["html_reports"]}
50-
51-
tinywasm={path="crates/tinywasm", features=["unsafe"]}
52-
wat={version="1.0"}
53-
wasmi={version="0.31", features=["std"]}
54-
wasmer={version="4.2", features=["cranelift", "singlepass"]}
55-
argon2={version="0.5"}

crates/benchmarks/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name="benchmarks"
3+
publish=false
4+
edition.workspace=true
5+
6+
[dependencies]
7+
criterion={version="0.5", features=["html_reports"]}
8+
tinywasm={path="../../crates/tinywasm", features=["unsafe"]}
9+
wat={version="1.0"}
10+
wasmi={version="0.31", features=["std"]}
11+
wasmer={version="4.2", features=["cranelift", "singlepass"]}
12+
argon2={version="0.5"}
13+
14+
[[bench]]
15+
name="selfhosted"
16+
harness=false
17+
18+
[[bench]]
19+
name="fibonacci"
20+
harness=false
21+
22+
[[bench]]
23+
name="argon2id"
24+
harness=false

benches/argon2id.rs renamed to crates/benchmarks/benches/argon2id.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn run_native(params: (i32, i32, i32)) {
3636
run_native(params.0, params.1, params.2)
3737
}
3838

39-
const ARGON2ID: &[u8] = include_bytes!("../examples/rust/out/argon2id.wasm");
39+
const ARGON2ID: &[u8] = include_bytes!("../../../examples/rust/out/argon2id.wasm");
4040
fn criterion_benchmark(c: &mut Criterion) {
4141
let twasm = wasm_to_twasm(ARGON2ID);
4242
let params = (1000, 2, 1);
@@ -47,8 +47,8 @@ fn criterion_benchmark(c: &mut Criterion) {
4747

4848
group.bench_function("native", |b| b.iter(|| run_native(black_box(params))));
4949
group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm, black_box(params), "argon2id")));
50-
group.bench_function("wasmi", |b| b.iter(|| run_wasmi(&ARGON2ID, black_box(params), "argon2id")));
51-
group.bench_function("wasmer", |b| b.iter(|| run_wasmer(&ARGON2ID, black_box(params), "argon2id")));
50+
group.bench_function("wasmi", |b| b.iter(|| run_wasmi(ARGON2ID, black_box(params), "argon2id")));
51+
group.bench_function("wasmer", |b| b.iter(|| run_wasmer(ARGON2ID, black_box(params), "argon2id")));
5252
}
5353

5454
criterion_group!(

benches/fibonacci.rs renamed to crates/benchmarks/benches/fibonacci.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ fn run_wasmer(wasm: &[u8], iterations: i32, name: &str) {
2020
let engine: Engine = wasmer::Singlepass::default().into();
2121
let mut store = Store::default();
2222
let import_object = imports! {};
23-
let module = wasmer::Module::from_binary(&engine, &wasm).expect("wasmer::Module::from_binary");
23+
let module = wasmer::Module::from_binary(&engine, wasm).expect("wasmer::Module::from_binary");
2424
let instance = Instance::new(&mut store, &module, &import_object).expect("Instance::new");
25-
let fib = instance.exports.get_typed_function::<i32, i32>(&mut store, name).expect("get_function");
25+
let fib = instance.exports.get_typed_function::<i32, i32>(&store, name).expect("get_function");
2626
fib.call(&mut store, iterations).expect("call");
2727
}
2828

@@ -45,25 +45,25 @@ fn run_native_recursive(n: i32) -> i32 {
4545
run_native_recursive(n - 1) + run_native_recursive(n - 2)
4646
}
4747

48-
const FIBONACCI: &[u8] = include_bytes!("../examples/rust/out/fibonacci.wasm");
48+
const FIBONACCI: &[u8] = include_bytes!("../../../examples/rust/out/fibonacci.wasm");
4949
fn criterion_benchmark(c: &mut Criterion) {
5050
let twasm = wasm_to_twasm(FIBONACCI);
5151

5252
{
5353
let mut group = c.benchmark_group("fibonacci");
5454
group.bench_function("native", |b| b.iter(|| run_native(black_box(60))));
5555
group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm, black_box(60), "fibonacci")));
56-
group.bench_function("wasmi", |b| b.iter(|| run_wasmi(&FIBONACCI, black_box(60), "fibonacci")));
57-
group.bench_function("wasmer", |b| b.iter(|| run_wasmer(&FIBONACCI, black_box(60), "fibonacci")));
56+
group.bench_function("wasmi", |b| b.iter(|| run_wasmi(FIBONACCI, black_box(60), "fibonacci")));
57+
group.bench_function("wasmer", |b| b.iter(|| run_wasmer(FIBONACCI, black_box(60), "fibonacci")));
5858
}
5959

6060
{
6161
let mut group = c.benchmark_group("fibonacci-recursive");
6262
group.measurement_time(std::time::Duration::from_secs(5));
6363
group.bench_function("native", |b| b.iter(|| run_native_recursive(black_box(26))));
6464
group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm, black_box(26), "fibonacci_recursive")));
65-
group.bench_function("wasmi", |b| b.iter(|| run_wasmi(&FIBONACCI, black_box(26), "fibonacci_recursive")));
66-
group.bench_function("wasmer", |b| b.iter(|| run_wasmer(&FIBONACCI, black_box(26), "fibonacci_recursive")));
65+
group.bench_function("wasmi", |b| b.iter(|| run_wasmi(FIBONACCI, black_box(26), "fibonacci_recursive")));
66+
group.bench_function("wasmer", |b| b.iter(|| run_wasmer(FIBONACCI, black_box(26), "fibonacci_recursive")));
6767
}
6868
}
6969

benches/selfhosted.rs renamed to crates/benchmarks/benches/selfhosted.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
mod util;
2-
use criterion::{criterion_group, criterion_main, Criterion};
3-
42
use crate::util::twasm_to_module;
3+
use criterion::{criterion_group, criterion_main, Criterion};
54

65
fn run_native() {
76
use tinywasm::*;
8-
let module = tinywasm::Module::parse_bytes(include_bytes!("../examples/rust/out/print.wasm")).expect("parse");
7+
let module = tinywasm::Module::parse_bytes(include_bytes!("../../../examples/rust/out/print.wasm")).expect("parse");
98
let mut store = Store::default();
109
let mut imports = Imports::default();
1110
imports.define("env", "printi32", Extern::typed_func(|_: FuncContext<'_>, _: i32| Ok(()))).expect("define");
@@ -46,18 +45,18 @@ fn run_wasmer(wasm: &[u8]) {
4645
"printi32" => Function::new_typed(&mut store, |_: i32| {}),
4746
},
4847
};
49-
let module = wasmer::Module::from_binary(&engine, &wasm).expect("wasmer::Module::from_binary");
48+
let module = wasmer::Module::from_binary(&engine, wasm).expect("wasmer::Module::from_binary");
5049
let instance = Instance::new(&mut store, &module, &import_object).expect("Instance::new");
5150
let hello = instance.exports.get_function("hello").expect("get_function");
5251
hello.call(&mut store, &[]).expect("call");
5352
}
5453

55-
const TINYWASM: &[u8] = include_bytes!("../examples/rust/out/tinywasm.wasm");
54+
const TINYWASM: &[u8] = include_bytes!("../../../examples/rust/out/tinywasm.wasm");
5655
fn criterion_benchmark(c: &mut Criterion) {
5756
let twasm = util::wasm_to_twasm(TINYWASM);
5857

5958
let mut group = c.benchmark_group("selfhosted");
60-
group.bench_function("native", |b| b.iter(|| run_native()));
59+
group.bench_function("native", |b| b.iter(run_native));
6160
group.bench_function("tinywasm", |b| b.iter(|| run_tinywasm(&twasm)));
6261
group.bench_function("wasmi", |b| b.iter(|| run_wasmi(TINYWASM)));
6362
group.bench_function("wasmer", |b| b.iter(|| run_wasmer(TINYWASM)));

benches/util/mod.rs renamed to crates/benchmarks/benches/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn wasm_to_twasm(wasm: &[u8]) -> Vec<u8> {
1010

1111
#[inline]
1212
pub fn twasm_to_module(twasm: &[u8]) -> tinywasm::Module {
13-
unsafe { TinyWasmModule::from_twasm_unchecked(&twasm) }.into()
13+
unsafe { TinyWasmModule::from_twasm_unchecked(twasm) }.into()
1414
}
1515

1616
pub fn tinywasm(twasm: &[u8]) -> (tinywasm::Store, tinywasm::ModuleInstance) {

crates/parser/src/conversion.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub(crate) fn convert_module_globals<'a, T: IntoIterator<Item = wasmparser::Resu
131131
Ok(globals)
132132
}
133133

134-
pub(crate) fn convert_module_export(export: wasmparser::Export) -> Result<Export> {
134+
pub(crate) fn convert_module_export(export: wasmparser::Export<'_>) -> Result<Export> {
135135
let kind = match export.kind {
136136
wasmparser::ExternalKind::Func => ExternalKind::Func,
137137
wasmparser::ExternalKind::Table => ExternalKind::Table,
@@ -146,7 +146,7 @@ pub(crate) fn convert_module_export(export: wasmparser::Export) -> Result<Export
146146
}
147147

148148
pub(crate) fn convert_module_code(
149-
func: wasmparser::FunctionBody,
149+
func: wasmparser::FunctionBody<'_>,
150150
mut validator: FuncValidator<ValidatorResources>,
151151
) -> Result<CodeSection> {
152152
let locals_reader = func.get_locals_reader()?;
@@ -205,18 +205,17 @@ pub(crate) fn convert_memarg(memarg: wasmparser::MemArg) -> MemoryArg {
205205
MemoryArg { offset: memarg.offset, align: memarg.align, align_max: memarg.max_align, mem_addr: memarg.memory }
206206
}
207207

208-
pub(crate) fn process_const_operators(ops: OperatorsReader) -> Result<ConstInstruction> {
208+
pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<ConstInstruction> {
209209
let ops = ops.into_iter().collect::<wasmparser::Result<Vec<_>>>()?;
210210
// In practice, the len can never be something other than 2,
211211
// but we'll keep this here since it's part of the spec
212212
// Invalid modules will be rejected by the validator anyway (there are also tests for this in the testsuite)
213213
assert!(ops.len() >= 2);
214214
assert!(matches!(ops[ops.len() - 1], wasmparser::Operator::End));
215-
216215
process_const_operator(ops[ops.len() - 2].clone())
217216
}
218217

219-
pub fn process_const_operator(op: wasmparser::Operator) -> Result<ConstInstruction> {
218+
pub(crate) fn process_const_operator(op: wasmparser::Operator<'_>) -> Result<ConstInstruction> {
220219
match op {
221220
wasmparser::Operator::RefNull { ty } => Ok(ConstInstruction::RefNull(convert_valtype(&ty))),
222221
wasmparser::Operator::RefFunc { function_index } => Ok(ConstInstruction::RefFunc(function_index)),
@@ -229,7 +228,7 @@ pub fn process_const_operator(op: wasmparser::Operator) -> Result<ConstInstructi
229228
}
230229
}
231230

232-
pub fn process_operators<'a>(
231+
pub(crate) fn process_operators<'a>(
233232
mut offset: usize,
234233
ops: impl Iterator<Item = Result<wasmparser::Operator<'a>, wasmparser::BinaryReaderError>>,
235234
mut validator: FuncValidator<ValidatorResources>,
@@ -515,7 +514,6 @@ pub fn process_operators<'a>(
515514
return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported instruction: {:?}", op)));
516515
}
517516
};
518-
519517
instructions.push(res);
520518
}
521519

@@ -524,6 +522,5 @@ pub fn process_operators<'a>(
524522
}
525523

526524
validator.finish(offset)?;
527-
528525
Ok(instructions.into_boxed_slice())
529526
}

0 commit comments

Comments
 (0)