Skip to content

Commit 54d131d

Browse files
committed
feat(wasm): use only one Engine per global context
Instantiating a `wasmtime::Engine` is an expensive operation, so it is best to do it only once for the duration of a `GlobalContext`. Cloning an engine is, however, not an expensive operation (just an `Arc::clone`) and we use it to avoid referring to the global context already referred to by the instantiated `ClarityWasmContext`. See-also: stacks-network/clarity-wasm#468
1 parent 2dfe72a commit 54d131d

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

clarity/src/vm/clarity_wasm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ pub fn initialize_contract(
399399
let mut call_stack = CallStack::new();
400400
let epoch = global_context.epoch_id;
401401
let clarity_version = *contract_context.get_clarity_version();
402+
let engine = global_context.engine.clone();
402403
let init_context = ClarityWasmContext::new_init(
403404
global_context,
404405
contract_context,
@@ -408,7 +409,6 @@ pub fn initialize_contract(
408409
sponsor.clone(),
409410
Some(contract_analysis),
410411
);
411-
let engine = Engine::default();
412412
let module = init_context
413413
.contract_context()
414414
.with_wasm_module(|wasm_module| {
@@ -485,6 +485,7 @@ pub fn call_function<'a>(
485485
) -> Result<Value, Error> {
486486
let epoch = global_context.epoch_id;
487487
let clarity_version = *contract_context.get_clarity_version();
488+
let engine = global_context.engine.clone();
488489
let context = ClarityWasmContext::new_run(
489490
global_context,
490491
contract_context,
@@ -499,7 +500,6 @@ pub fn call_function<'a>(
499500
.contract_context()
500501
.lookup_function(function_name)
501502
.ok_or(CheckErrors::UndefinedFunction(function_name.to_string()))?;
502-
let engine = Engine::default();
503503
let module = context
504504
.contract_context()
505505
.with_wasm_module(|wasm_module| unsafe {

clarity/src/vm/contexts.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ use serde_json::json;
2424
use stacks_common::consts::CHAIN_ID_TESTNET;
2525
use stacks_common::types::chainstate::StacksBlockId;
2626
use stacks_common::types::StacksEpochId;
27+
use wasmtime::{Engine, Linker};
2728

2829
use super::analysis::{self, ContractAnalysis};
2930
#[cfg(feature = "clarity-wasm")]
30-
use super::clarity_wasm::call_function;
31+
use super::clarity_wasm::{call_function, ClarityWasmContext};
3132
use super::EvalHook;
3233
use crate::vm::ast::{ASTRules, ContractAST};
3334
use crate::vm::callables::{DefinedFunction, FunctionIdentifier};
@@ -206,6 +207,7 @@ pub struct GlobalContext<'a> {
206207
/// This is the chain ID of the transaction
207208
pub chain_id: u32,
208209
pub eval_hooks: Option<Vec<&'a mut dyn EvalHook>>,
210+
pub engine: Engine,
209211
}
210212

211213
#[derive(Serialize, Deserialize, Clone)]
@@ -1653,6 +1655,8 @@ impl<'a> GlobalContext<'a> {
16531655
cost_track: LimitedCostTracker,
16541656
epoch_id: StacksEpochId,
16551657
) -> GlobalContext {
1658+
let engine = Engine::default();
1659+
16561660
GlobalContext {
16571661
database,
16581662
cost_track,
@@ -1663,6 +1667,7 @@ impl<'a> GlobalContext<'a> {
16631667
epoch_id,
16641668
chain_id,
16651669
eval_hooks: None,
1670+
engine,
16661671
}
16671672
}
16681673

0 commit comments

Comments
 (0)