Skip to content

Commit e716825

Browse files
committed
runtime: Cache InstancePre on ValidModule for faster trigger instantiation
Extract linker construction from per-trigger `from_valid_module_with_ctx` into a standalone `build_linker()` function called once at module validation time. The linker is pre-linked via `linker.instantiate_pre()` and the resulting `InstancePre` is stored on `ValidModule`. This eliminates the per-trigger cost of: - Rebuilding a Linker with ~60 func_wrap_async registrations - Resolving imports against the module Chain-specific host functions (e.g. ethereum.call) are now dispatched generically: the linker registers them by import name and looks up the actual HostFn from caller.data().ctx.host_fns at call time. Conditional functions (ipfs.getBlock, arweave.transactionData, box.profile) are now linked unconditionally since they already check feature flags or return errors internally.
1 parent fccebe8 commit e716825

File tree

2 files changed

+405
-316
lines changed

2 files changed

+405
-316
lines changed

runtime/wasm/src/mapping.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::gas_rules::GasRules;
2-
use crate::module::{ExperimentalFeatures, ToAscPtr, WasmInstance};
2+
use crate::module::{ExperimentalFeatures, ToAscPtr, WasmInstance, WasmInstanceData};
33
use graph::blockchain::{BlockTime, Blockchain, HostFn};
44
use graph::components::store::SubgraphFork;
55
use graph::components::subgraph::{MappingError, SharedProofOfIndexing};
@@ -222,6 +222,11 @@ const GN_START_FUNCTION_NAME: &str = "gn::start";
222222
pub struct ValidModule {
223223
pub module: wasmtime::Module,
224224

225+
/// Pre-linked instance template. Created once at module validation time and reused for every
226+
/// trigger instantiation, avoiding the cost of rebuilding the linker (~60 host function
227+
/// registrations) and resolving imports on each trigger.
228+
pub instance_pre: wasmtime::InstancePre<WasmInstanceData>,
229+
225230
// Due to our internal architecture we don't want to run the start function at instantiation time,
226231
// so we track it separately so that we can run it at an appropriate time.
227232
// Since the start function is not an export, we will also create an export for it.
@@ -340,8 +345,12 @@ impl ValidModule {
340345
epoch_counter_abort_handle = Some(graph::spawn(epoch_counter).abort_handle());
341346
}
342347

348+
let linker = crate::module::build_linker(engine, &import_name_to_modules)?;
349+
let instance_pre = linker.instantiate_pre(&module)?;
350+
343351
Ok(ValidModule {
344352
module,
353+
instance_pre,
345354
import_name_to_modules,
346355
start_function,
347356
timeout,

0 commit comments

Comments
 (0)