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

Commit d2db1f7

Browse files
chore: overall code cleanup
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 4faaf5b commit d2db1f7

File tree

14 files changed

+221
-343
lines changed

14 files changed

+221
-343
lines changed

crates/benchmarks/benches/argon2id.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ fn criterion_benchmark(c: &mut Criterion) {
4545
group.measurement_time(std::time::Duration::from_secs(7));
4646
group.sample_size(10);
4747

48-
group.bench_function("native", |b| b.iter(|| run_native(black_box(params))));
48+
// 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!(

crates/parser/src/visit.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,17 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
340340
}
341341

342342
fn visit_local_set(&mut self, idx: u32) -> Self::Output {
343-
if let Some(instruction) = self.instructions.last_mut() {
344-
match instruction {
345-
// Needs more testing, seems to make performance worse
346-
// Instruction::LocalGet(a) => *instruction = Instruction::LocalGetSet(*a, idx),
347-
_ => return self.visit(Instruction::LocalSet(idx)),
348-
};
349-
// Ok(())
350-
} else {
351-
self.visit(Instruction::LocalSet(idx))
352-
}
343+
self.visit(Instruction::LocalSet(idx))
344+
// if let Some(instruction) = self.instructions.last_mut() {
345+
// match instruction {
346+
// // Needs more testing, seems to make performance worse
347+
// // Instruction::LocalGet(a) => *instruction = Instruction::LocalGetSet(*a, idx),
348+
// _ => return self.visit(Instruction::LocalSet(idx)),
349+
// };
350+
// // Ok(())
351+
// } else {
352+
// self.visit(Instruction::LocalSet(idx))
353+
// }
353354
}
354355

355356
fn visit_local_tee(&mut self, idx: u32) -> Self::Output {
@@ -372,18 +373,19 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
372373
}
373374

374375
fn visit_i32_add(&mut self) -> Self::Output {
375-
if self.instructions.len() < 2 {
376-
return self.visit(Instruction::I32Add);
377-
}
376+
self.visit(Instruction::I32Add)
377+
// if self.instructions.len() < 2 {
378+
// return self.visit(Instruction::I32Add);
379+
// }
378380

379-
match self.instructions[self.instructions.len() - 2..] {
380-
// [Instruction::LocalGet(a), Instruction::I32Const(b)] => {
381-
// self.instructions.pop();
382-
// self.instructions.pop();
383-
// self.visit(Instruction::I32LocalGetConstAdd(a, b))
384-
// }
385-
_ => self.visit(Instruction::I32Add),
386-
}
381+
// match self.instructions[self.instructions.len() - 2..] {
382+
// // [Instruction::LocalGet(a), Instruction::I32Const(b)] => {
383+
// // self.instructions.pop();
384+
// // self.instructions.pop();
385+
// // self.visit(Instruction::I32LocalGetConstAdd(a, b))
386+
// // }
387+
// _ => self.visit(Instruction::I32Add),
388+
// }
387389
}
388390

389391
fn visit_block(&mut self, blockty: wasmparser::BlockType) -> Self::Output {
@@ -416,7 +418,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
416418

417419
match self.instructions[label_pointer] {
418420
Instruction::Else(ref mut else_instr_end_offset) => {
419-
*else_instr_end_offset = (current_instr_ptr - label_pointer as usize)
421+
*else_instr_end_offset = (current_instr_ptr - label_pointer)
420422
.try_into()
421423
.expect("else_instr_end_offset is too large, tinywasm does not support if blocks that large");
422424

crates/tinywasm/src/func.rs

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ impl FuncHandle {
2222
/// See <https://webassembly.github.io/spec/core/exec/modules.html#invocation>
2323
#[inline]
2424
pub fn call(&self, store: &mut Store, params: &[WasmValue]) -> Result<Vec<WasmValue>> {
25+
// Comments are ordered by the steps in the spec
26+
// In this implementation, some steps are combined and ordered differently for performance reasons
27+
2528
// 3. Let func_ty be the function type
2629
let func_ty = &self.ty;
2730

@@ -35,7 +38,7 @@ impl FuncHandle {
3538
}
3639

3740
// 5. For each value type and the corresponding value, check if types match
38-
if !unlikely(func_ty.params.iter().zip(params).enumerate().all(|(i, (ty, param))| {
41+
if !(func_ty.params.iter().zip(params).enumerate().all(|(i, (ty, param))| {
3942
if ty != &param.val_type() {
4043
log::error!("param type mismatch at index {}: expected {:?}, got {:?}", i, ty, param);
4144
false
@@ -57,8 +60,8 @@ impl FuncHandle {
5760
};
5861

5962
// 6. Let f be the dummy frame
60-
let call_frame =
61-
CallFrame::new(wasm_func.clone(), func_inst.owner, params.iter().map(|v| RawWasmValue::from(*v)), 0);
63+
let call_frame_params = params.iter().map(|v| RawWasmValue::from(*v));
64+
let call_frame = CallFrame::new(wasm_func.clone(), func_inst.owner, call_frame_params, 0);
6265

6366
// 7. Push the frame f to the call stack
6467
// & 8. Push the values to the stack (Not needed since the call frame owns the values)
@@ -113,6 +116,7 @@ impl<P: IntoWasmValueTuple, R: FromWasmValueTuple> FuncHandleTyped<P, R> {
113116
R::from_wasm_value_tuple(&result)
114117
}
115118
}
119+
116120
macro_rules! impl_into_wasm_value_tuple {
117121
($($T:ident),*) => {
118122
impl<$($T),*> IntoWasmValueTuple for ($($T,)*)
@@ -140,21 +144,6 @@ macro_rules! impl_into_wasm_value_tuple_single {
140144
};
141145
}
142146

143-
impl_into_wasm_value_tuple_single!(i32);
144-
impl_into_wasm_value_tuple_single!(i64);
145-
impl_into_wasm_value_tuple_single!(f32);
146-
impl_into_wasm_value_tuple_single!(f64);
147-
148-
impl_into_wasm_value_tuple!();
149-
impl_into_wasm_value_tuple!(T1);
150-
impl_into_wasm_value_tuple!(T1, T2);
151-
impl_into_wasm_value_tuple!(T1, T2, T3);
152-
impl_into_wasm_value_tuple!(T1, T2, T3, T4);
153-
impl_into_wasm_value_tuple!(T1, T2, T3, T4, T5);
154-
impl_into_wasm_value_tuple!(T1, T2, T3, T4, T5, T6);
155-
impl_into_wasm_value_tuple!(T1, T2, T3, T4, T5, T6, T7);
156-
impl_into_wasm_value_tuple!(T1, T2, T3, T4, T5, T6, T7, T8);
157-
158147
macro_rules! impl_from_wasm_value_tuple {
159148
($($T:ident),*) => {
160149
impl<$($T),*> FromWasmValueTuple for ($($T,)*)
@@ -200,21 +189,6 @@ macro_rules! impl_from_wasm_value_tuple_single {
200189
};
201190
}
202191

203-
impl_from_wasm_value_tuple_single!(i32);
204-
impl_from_wasm_value_tuple_single!(i64);
205-
impl_from_wasm_value_tuple_single!(f32);
206-
impl_from_wasm_value_tuple_single!(f64);
207-
208-
impl_from_wasm_value_tuple!();
209-
impl_from_wasm_value_tuple!(T1);
210-
impl_from_wasm_value_tuple!(T1, T2);
211-
impl_from_wasm_value_tuple!(T1, T2, T3);
212-
impl_from_wasm_value_tuple!(T1, T2, T3, T4);
213-
impl_from_wasm_value_tuple!(T1, T2, T3, T4, T5);
214-
impl_from_wasm_value_tuple!(T1, T2, T3, T4, T5, T6);
215-
impl_from_wasm_value_tuple!(T1, T2, T3, T4, T5, T6, T7);
216-
impl_from_wasm_value_tuple!(T1, T2, T3, T4, T5, T6, T7, T8);
217-
218192
pub trait ValTypesFromTuple {
219193
fn val_types() -> Box<[ValType]>;
220194
}
@@ -268,19 +242,42 @@ impl ValTypesFromTuple for () {
268242
}
269243
}
270244

271-
impl<T1> ValTypesFromTuple for T1
272-
where
273-
T1: ToValType,
274-
{
245+
impl<T: ToValType> ValTypesFromTuple for T {
275246
#[inline]
276247
fn val_types() -> Box<[ValType]> {
277-
Box::new([T1::to_val_type()])
248+
Box::new([T::to_val_type()])
278249
}
279250
}
280251

252+
impl_from_wasm_value_tuple_single!(i32);
253+
impl_from_wasm_value_tuple_single!(i64);
254+
impl_from_wasm_value_tuple_single!(f32);
255+
impl_from_wasm_value_tuple_single!(f64);
256+
257+
impl_into_wasm_value_tuple_single!(i32);
258+
impl_into_wasm_value_tuple_single!(i64);
259+
impl_into_wasm_value_tuple_single!(f32);
260+
impl_into_wasm_value_tuple_single!(f64);
261+
281262
impl_val_types_from_tuple!(T1);
282263
impl_val_types_from_tuple!(T1, T2);
283264
impl_val_types_from_tuple!(T1, T2, T3);
284265
impl_val_types_from_tuple!(T1, T2, T3, T4);
285266
impl_val_types_from_tuple!(T1, T2, T3, T4, T5);
286267
impl_val_types_from_tuple!(T1, T2, T3, T4, T5, T6);
268+
269+
impl_from_wasm_value_tuple!();
270+
impl_from_wasm_value_tuple!(T1);
271+
impl_from_wasm_value_tuple!(T1, T2);
272+
impl_from_wasm_value_tuple!(T1, T2, T3);
273+
impl_from_wasm_value_tuple!(T1, T2, T3, T4);
274+
impl_from_wasm_value_tuple!(T1, T2, T3, T4, T5);
275+
impl_from_wasm_value_tuple!(T1, T2, T3, T4, T5, T6);
276+
277+
impl_into_wasm_value_tuple!();
278+
impl_into_wasm_value_tuple!(T1);
279+
impl_into_wasm_value_tuple!(T1, T2);
280+
impl_into_wasm_value_tuple!(T1, T2, T3);
281+
impl_into_wasm_value_tuple!(T1, T2, T3, T4);
282+
impl_into_wasm_value_tuple!(T1, T2, T3, T4, T5);
283+
impl_into_wasm_value_tuple!(T1, T2, T3, T4, T5, T6);

crates/tinywasm/src/imports.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ impl Extern {
157157
};
158158

159159
let ty = tinywasm_types::FuncType { params: P::val_types(), results: R::val_types() };
160-
161160
Self::Function(Function::Host(Rc::new(HostFunction { func: Box::new(inner_func), ty })))
162161
}
163162

crates/tinywasm/src/runtime/interpreter/macros.rs

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
macro_rules! break_to {
1313
($cf:ident, $stack:ident, $break_to_relative:ident) => {{
1414
if $cf.break_to(*$break_to_relative, &mut $stack.values, &mut $stack.blocks).is_none() {
15-
if $stack.call_stack.is_empty() {
16-
return Ok(ExecResult::Return);
17-
} else {
18-
return Ok(ExecResult::Call);
15+
match $stack.call_stack.is_empty() {
16+
true => return Ok(ExecResult::Return),
17+
false => return Ok(ExecResult::Call),
1918
}
2019
}
2120
}};
@@ -62,21 +61,15 @@ macro_rules! mem_load {
6261
/// Store a value to memory
6362
macro_rules! mem_store {
6463
($type:ty, $arg:expr, $stack:ident, $store:ident, $module:ident) => {{
65-
log::debug!("mem_store!({}, {:?})", stringify!($type), $arg);
6664
mem_store!($type, $type, $arg, $stack, $store, $module)
6765
}};
6866

6967
($store_type:ty, $target_type:ty, $arg:expr, $stack:ident, $store:ident, $module:ident) => {{
7068
let (mem_addr, offset) = $arg;
71-
let mem_idx = $module.resolve_mem_addr(*mem_addr);
72-
let mem = $store.get_mem(mem_idx as usize)?;
73-
74-
let val = $stack.values.pop_t::<$store_type>()?;
75-
let addr: u64 = $stack.values.pop()?.into();
76-
77-
let val = val as $store_type;
69+
let mem = $store.get_mem($module.resolve_mem_addr(*mem_addr) as usize)?;
70+
let val: $store_type = $stack.values.pop()?.into();
7871
let val = val.to_le_bytes();
79-
72+
let addr: u64 = $stack.values.pop()?.into();
8073
mem.borrow_mut().store((*offset + addr) as usize, val.len(), &val)?;
8174
}};
8275
}
@@ -103,13 +96,11 @@ macro_rules! float_min_max {
10396

10497
/// Convert a value on the stack
10598
macro_rules! conv {
106-
($from:ty, $intermediate:ty, $to:ty, $stack:ident) => {{
107-
let a = $stack.values.pop_t::<$from>()? as $intermediate;
108-
$stack.values.push((a as $to).into());
109-
}};
11099
($from:ty, $to:ty, $stack:ident) => {{
111-
let a = $stack.values.pop_t::<$from>()?;
112-
$stack.values.push((a as $to).into());
100+
$stack.values.replace_top(|v| {
101+
let a: $from = v.into();
102+
(a as $to).into()
103+
});
113104
}};
114105
}
115106

@@ -138,76 +129,62 @@ macro_rules! checked_conv_float {
138129

139130
/// Compare two values on the stack
140131
macro_rules! comp {
141-
($op:tt, $ty:ty, $stack:ident) => {{
142-
comp!($op, $ty, $ty, $stack)
143-
}};
144-
145-
($op:tt, $intermediate:ty, $to:ty, $stack:ident) => {{
146-
let b = $stack.values.pop_t::<$intermediate>()? as $to;
147-
let a = $stack.values.pop_t::<$intermediate>()? as $to;
132+
($op:tt, $to:ty, $stack:ident) => {{
133+
let b: $to = $stack.values.pop()?.into();
134+
let a: $to = $stack.values.pop()?.into();
148135
$stack.values.push(((a $op b) as i32).into());
149136
}};
150137
}
151138

152139
/// Compare a value on the stack to zero
153140
macro_rules! comp_zero {
154141
($op:tt, $ty:ty, $stack:ident) => {{
155-
let a = $stack.values.pop_t::<$ty>()?;
142+
let a: $ty = $stack.values.pop()?.into();
156143
$stack.values.push(((a $op 0) as i32).into());
157144
}};
158145
}
159146

160147
/// Apply an arithmetic method to two values on the stack
161148
macro_rules! arithmetic {
162-
($op:ident, $ty:ty, $stack:ident) => {
163-
arithmetic!($op, $ty, $ty, $stack)
164-
};
149+
($op:ident, $to:ty, $stack:ident) => {{
150+
let b: $to = $stack.values.pop()?.into();
151+
let a: $to = $stack.values.pop()?.into();
152+
$stack.values.push((a.$op(b) as $to).into());
153+
}};
165154

166155
// also allow operators such as +, -
167156
($op:tt, $ty:ty, $stack:ident) => {{
168-
let b: $ty = $stack.values.pop_t()?;
169-
let a: $ty = $stack.values.pop_t()?;
157+
let b: $ty = $stack.values.pop()?.into();
158+
let a: $ty = $stack.values.pop()?.into();
170159
$stack.values.push((a $op b).into());
171160
}};
172-
173-
($op:ident, $intermediate:ty, $to:ty, $stack:ident) => {{
174-
let b = $stack.values.pop_t::<$to>()? as $intermediate;
175-
let a = $stack.values.pop_t::<$to>()? as $intermediate;
176-
$stack.values.push((a.$op(b) as $to).into());
177-
}};
178161
}
179162

180163
/// Apply an arithmetic method to a single value on the stack
181164
macro_rules! arithmetic_single {
182165
($op:ident, $ty:ty, $stack:ident) => {{
183-
let a = $stack.values.pop_t::<$ty>()?;
166+
let a: $ty = $stack.values.pop()?.into();
184167
$stack.values.push((a.$op() as $ty).into());
185168
}};
186169

187170
($op:ident, $from:ty, $to:ty, $stack:ident) => {{
188-
let a = $stack.values.pop_t::<$from>()?;
171+
let a: $from = $stack.values.pop()?.into();
189172
$stack.values.push((a.$op() as $to).into());
190173
}};
191174
}
192175

193176
/// Apply an arithmetic operation to two values on the stack with error checking
194177
macro_rules! checked_int_arithmetic {
195-
// Direct conversion with error checking (two types)
196-
($from:tt, $to:tt, $stack:ident) => {{
197-
checked_int_arithmetic!($from, $to, $to, $stack)
198-
}};
199-
200-
($op:ident, $from:ty, $to:ty, $stack:ident) => {{
201-
let b = $stack.values.pop_t::<$from>()? as $to;
202-
let a = $stack.values.pop_t::<$from>()? as $to;
178+
($op:ident, $to:ty, $stack:ident) => {{
179+
let b: $to = $stack.values.pop()?.into();
180+
let a: $to = $stack.values.pop()?.into();
203181

204-
if b == 0 {
182+
if unlikely(b == 0) {
205183
return Err(Error::Trap(crate::Trap::DivisionByZero));
206184
}
207185

208186
let result = a.$op(b).ok_or_else(|| Error::Trap(crate::Trap::IntegerOverflow))?;
209-
// Cast back to original type if different
210-
$stack.values.push((result as $from).into());
187+
$stack.values.push((result).into());
211188
}};
212189
}
213190

0 commit comments

Comments
 (0)