Skip to content

Commit 5b31817

Browse files
chore: reduce call & instantiation overhead
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 238e7aa commit 5b31817

9 files changed

Lines changed: 412 additions & 450 deletions

File tree

crates/parser/src/conversion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<Box<[C
241241
// In practice, the len can never be something other than 2,
242242
// but we'll keep this here since it's part of the spec
243243
// Invalid modules will be rejected by the validator anyway (there are also tests for this in the testsuite)
244-
assert!(ops.len() >= 2);
245-
assert!(matches!(ops[ops.len() - 1], wasmparser::Operator::End));
244+
debug_assert!(ops.len() >= 2);
245+
debug_assert!(matches!(ops[ops.len() - 1], wasmparser::Operator::End));
246246

247247
let mut out = Vec::with_capacity(ops.len().saturating_sub(1));
248248
for op in ops.iter().take(ops.len() - 1) {

crates/tinywasm/src/func.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ impl Function {
2525
store.value_stack.clear();
2626
store.value_stack.extend_from_wasmvalues(params)?;
2727
let locals_base = store.value_stack.enter_locals(&wasm_func.func.params, &wasm_func.func.locals)?;
28-
let stack_offset = wasm_func.func.locals;
29-
let callframe = CallFrame::new(self.addr, wasm_func.owner, locals_base, stack_offset);
28+
let callframe = CallFrame::new(self.addr, locals_base, wasm_func.func.locals);
3029

3130
// Execute until completion and then collect result values from the stack.
3231
InterpreterRuntime::exec(store, callframe)?;
@@ -56,8 +55,7 @@ impl Function {
5655
store.value_stack.clear();
5756
store.value_stack.extend_from_wasmvalues(params)?;
5857
let locals_base = store.value_stack.enter_locals(&wasm_func.func.params, &wasm_func.func.locals)?;
59-
let stack_offset = wasm_func.func.locals;
60-
let callframe = CallFrame::new(self.addr, wasm_func.owner, locals_base, stack_offset);
58+
let callframe = CallFrame::new(self.addr, locals_base, wasm_func.func.locals);
6159

6260
Ok(FuncExecution {
6361
store,

crates/tinywasm/src/imports.rs

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ impl From<&Import> for ExternName {
9898
#[derive(Default, Clone)]
9999
#[cfg_attr(feature = "debug", derive(Debug))]
100100
pub struct Imports {
101-
globals: BTreeMap<ExternName, Global>,
102-
tables: BTreeMap<ExternName, Table>,
103-
memories: BTreeMap<ExternName, Memory>,
104-
function_handles: BTreeMap<ExternName, Function>,
101+
externs: BTreeMap<ExternName, Extern>,
105102
modules: BTreeMap<String, crate::ModuleInstance>,
106103
}
107104

@@ -112,30 +109,15 @@ pub(crate) struct ResolvedImports {
112109
pub(crate) funcs: Vec<FuncAddr>,
113110
}
114111

115-
impl ResolvedImports {
116-
pub(crate) const fn new() -> Self {
117-
Self { globals: Vec::new(), tables: Vec::new(), memories: Vec::new(), funcs: Vec::new() }
118-
}
119-
}
120-
121112
impl Imports {
122113
/// Create a new empty import set
123114
pub const fn new() -> Self {
124-
Self {
125-
globals: BTreeMap::new(),
126-
tables: BTreeMap::new(),
127-
memories: BTreeMap::new(),
128-
function_handles: BTreeMap::new(),
129-
modules: BTreeMap::new(),
130-
}
115+
Self { externs: BTreeMap::new(), modules: BTreeMap::new() }
131116
}
132117

133118
/// Merge two import sets
134119
pub fn merge(mut self, other: Self) -> Self {
135-
self.globals.extend(other.globals);
136-
self.tables.extend(other.tables);
137-
self.memories.extend(other.memories);
138-
self.function_handles.extend(other.function_handles);
120+
self.externs.extend(other.externs);
139121
self.modules.extend(other.modules);
140122
self
141123
}
@@ -151,38 +133,13 @@ impl Imports {
151133
/// Define an import value.
152134
pub fn define(&mut self, module: &str, name: &str, value: impl Into<Extern>) -> &mut Self {
153135
let name = ExternName { module: module.to_string(), name: name.to_string() };
154-
match value.into() {
155-
Extern::Global(v) => {
156-
self.globals.insert(name, v);
157-
}
158-
Extern::Table(v) => {
159-
self.tables.insert(name, v);
160-
}
161-
Extern::Memory(v) => {
162-
self.memories.insert(name, v);
163-
}
164-
Extern::Function(v) => {
165-
self.function_handles.insert(name, v);
166-
}
167-
}
136+
self.externs.insert(name, value.into());
168137
self
169138
}
170139

171140
pub(crate) fn take_defined(&self, import: &Import) -> Option<Extern> {
172141
let name = ExternName::from(import);
173-
if let Some(v) = self.globals.get(&name) {
174-
return Some(Extern::Global(*v));
175-
}
176-
if let Some(v) = self.tables.get(&name) {
177-
return Some(Extern::Table(*v));
178-
}
179-
if let Some(v) = self.memories.get(&name) {
180-
return Some(Extern::Memory(*v));
181-
}
182-
if let Some(v) = self.function_handles.get(&name) {
183-
return Some(Extern::Function(v.clone()));
184-
}
185-
None
142+
self.externs.get(&name).cloned()
186143
}
187144

188145
#[cfg(not(feature = "debug"))]
@@ -242,13 +199,21 @@ impl Imports {
242199
Ok(())
243200
}
244201

245-
pub(crate) fn link(
246-
self,
247-
store: &mut crate::Store,
248-
module: &Module,
249-
_idx: ModuleInstanceAddr,
250-
) -> Result<ResolvedImports> {
251-
let mut imports = ResolvedImports::new();
202+
pub(crate) fn link(&self, store: &mut crate::Store, module: &Module) -> Result<ResolvedImports> {
203+
let (global_count, table_count, mem_count, func_count) =
204+
module.imports.iter().fold((0, 0, 0, 0), |(g, t, m, f), import| match import.kind {
205+
ImportKind::Global(_) => (g + 1, t, m, f),
206+
ImportKind::Table(_) => (g, t + 1, m, f),
207+
ImportKind::Memory(_) => (g, t, m + 1, f),
208+
ImportKind::Function(_) => (g, t, m, f + 1),
209+
});
210+
211+
let mut imports = ResolvedImports {
212+
globals: Vec::with_capacity(global_count),
213+
tables: Vec::with_capacity(table_count),
214+
memories: Vec::with_capacity(mem_count),
215+
funcs: Vec::with_capacity(func_count),
216+
};
252217

253218
for import in &*module.imports {
254219
if let Some(defined) = self.take_defined(import) {
@@ -299,9 +264,8 @@ impl Imports {
299264
let Some(instance) = self.modules.get(&name.module) else {
300265
return Err(LinkingError::unknown_import(import).into());
301266
};
302-
if instance.0.store_id != store.id() {
303-
return Err(crate::Error::InvalidStore);
304-
}
267+
instance.validate_store(store)?;
268+
305269
let val = instance.export_addr(&import.name).ok_or_else(|| LinkingError::unknown_import(import))?;
306270

307271
{

0 commit comments

Comments
 (0)