Skip to content

Commit c8cf1ff

Browse files
a bunch of experiments :)
some interesting things here, main things for now I want to pull into the main branch are reworking the parser to be single pass and some of the tweaks to interpreter dispatch. Acc-stuff has gotten a bit out of hand and doesn't really show too much perf gains (or none) in the current state, this will need a bit more thought-out rework of our other superinstructions too. Also we might want to switch back to 16-byte operands since there wasn't too much of a perf gain. Signed-off-by: Henry <mail@henrygressmann.de>
1 parent c67ce64 commit c8cf1ff

34 files changed

Lines changed: 5477 additions & 2349 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Optional `send` support for moving stores and store-local handles across threads
1717
- Optional portable atomic shared pointers and counters for targets without native compare-and-swap
1818
- Optional `nightly-tail-calls` backend using Rust's unstable explicit tail calls for interpreter dispatch
19+
- Streaming parser-managed numeric accumulator bytecode
1920

2021
### Changed
2122

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ pretty_env_logger = "0.5"
3131
serde = { version = "1.0", features = ["derive"] }
3232
serde_json = { version = "1.0" }
3333
wasm-testsuite = { version = "0.7" }
34-
wasmparser = { version = "0.258", default-features = false }
35-
wast = "258"
36-
wat = "1.258"
34+
wasmparser = { version = "0.259", default-features = false }
35+
wast = "259"
36+
wat = "1.259"
3737

3838
criterion = { version = "0.8", default-features = false, features = ["cargo_bench_support", "rayon"] }
3939

benches/tinywasm_modes.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,25 @@ const TIME_BUDGET_PER_ROUND: core::time::Duration = core::time::Duration::from_m
1111
const BENCH_MEASUREMENT_TIME: core::time::Duration = core::time::Duration::from_secs(10);
1212

1313
fn tinywasm_parse() -> Result<Module> {
14-
let parser = tinywasm_parser::Parser::default();
14+
Ok(tinywasm_parser::Parser::default().parse_module_bytes(WASM)?)
15+
}
16+
17+
fn tinywasm_parse_single_threaded() -> Result<Module> {
18+
let parser = tinywasm_parser::Parser::new(tinywasm_parser::ParserOptions::new().with_parser_threads(1));
1519
Ok(parser.parse_module_bytes(WASM)?)
1620
}
1721

22+
fn accumulator_module() -> Result<Module> {
23+
let mut wat = String::from("(module (func (export \"chain\") (param i32) (result i32) local.get 0 ");
24+
for _ in 0..1024 {
25+
wat.push_str("i32.const 1 i32.add ");
26+
}
27+
wat.push_str("))");
28+
let wasm = wat::parse_str(wat).expect("valid accumulator benchmark module");
29+
let parser = tinywasm_parser::Parser::default();
30+
Ok(parser.parse_module_bytes(&wasm)?)
31+
}
32+
1833
fn setup_typed_func(module: &Module, engine: Option<Engine>) -> Result<(Store, FunctionTyped<(), ()>)> {
1934
let mut store = match engine {
2035
Some(engine) => Store::new(engine),
@@ -34,6 +49,13 @@ fn run_call(store: &mut Store, func: &FunctionTyped<(), ()>) -> Result<()> {
3449
Ok(())
3550
}
3651

52+
fn setup_chain(module: &Module) -> Result<(Store, FunctionTyped<i32, i32>)> {
53+
let mut store = Store::default();
54+
let instance = ModuleInstance::instantiate(&mut store, module, None)?;
55+
let func = instance.func::<i32, i32>(&store, "chain")?;
56+
Ok((store, func))
57+
}
58+
3759
fn run_resume_with_fuel(store: &mut Store, func: &FunctionTyped<(), ()>) -> Result<()> {
3860
let mut execution = func.call_resumable(store, ())?;
3961
loop {
@@ -55,6 +77,11 @@ fn run_resume_with_time_budget(store: &mut Store, func: &FunctionTyped<(), ()>)
5577
}
5678

5779
fn criterion_benchmark(c: &mut Criterion) {
80+
let mut parse_group = c.benchmark_group("tinywasm_parse_modes");
81+
parse_group.measurement_time(BENCH_MEASUREMENT_TIME);
82+
parse_group.bench_function("streaming", |b| b.iter(|| tinywasm_parse_single_threaded().expect("parse module")));
83+
parse_group.finish();
84+
5885
let module = tinywasm_parse().expect("tinywasm_parse");
5986
let mut group = c.benchmark_group("tinywasm_modes");
6087
group.measurement_time(BENCH_MEASUREMENT_TIME);
@@ -94,6 +121,18 @@ fn criterion_benchmark(c: &mut Criterion) {
94121
});
95122

96123
group.finish();
124+
125+
let accumulator_chain = accumulator_module().expect("parse accumulator benchmark");
126+
let mut group = c.benchmark_group("accumulator");
127+
group.measurement_time(BENCH_MEASUREMENT_TIME);
128+
group.bench_function("chain", |b| {
129+
b.iter_batched_ref(
130+
|| setup_chain(&accumulator_chain).expect("setup accumulator chain"),
131+
|(store, func)| assert_eq!(func.call(store, 0).expect("run accumulator chain"), 1024),
132+
BatchSize::LargeInput,
133+
)
134+
});
135+
group.finish();
97136
}
98137

99138
criterion_group!(benches, criterion_benchmark);

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
too-many-arguments-threshold = 8

crates/cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition.workspace = true
55
rust-version.workspace = true
66
description = "Minimal command-line interface for TinyWasm"
77
documentation = "https://docs.rs/tinywasm-cli"
8-
readme = "README.md"
98
repository.workspace = true
109
license.workspace = true
1110
keywords = ["cli", "runtime", "tinywasm", "wasm", "webassembly"]

crates/parser/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version.workspace = true
44
edition.workspace = true
55
rust-version.workspace = true
66
description = "Parser and lowering pipeline for TinyWasm"
7-
readme = "README.md"
87
repository.workspace = true
98
license.workspace = true
109
keywords.workspace = true

crates/parser/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let bytes = include_bytes!("./file.wasm");
1919
let parser = Parser::default();
2020
let module = parser.parse_module_bytes(bytes)?;
2121

22-
let parser = Parser::new(ParserOptions::default().with_rewrite_optimization(false));
22+
let parser = Parser::new(ParserOptions::default().with_operand_deduplication(true));
2323
let module = parser.parse_module_bytes(bytes)?;
2424

2525
let module = parser.parse_module_file("path/to/file.wasm")?;

0 commit comments

Comments
 (0)