Skip to content

Commit

Permalink
feat(wasm): use only one Engine per global context
Browse files Browse the repository at this point in the history
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 while while
instantiating a `ClarityWasmContext`.

See-also: stacks-network/clarity-wasm#468
  • Loading branch information
ureeves committed Nov 11, 2024
1 parent 2dfe72a commit 13ff1b9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions clarity/src/vm/clarity_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ pub fn initialize_contract(
let mut call_stack = CallStack::new();
let epoch = global_context.epoch_id;
let clarity_version = *contract_context.get_clarity_version();
let engine = global_context.engine.clone();
let init_context = ClarityWasmContext::new_init(
global_context,
contract_context,
Expand All @@ -408,7 +409,6 @@ pub fn initialize_contract(
sponsor.clone(),
Some(contract_analysis),
);
let engine = Engine::default();
let module = init_context
.contract_context()
.with_wasm_module(|wasm_module| {
Expand Down Expand Up @@ -485,6 +485,7 @@ pub fn call_function<'a>(
) -> Result<Value, Error> {
let epoch = global_context.epoch_id;
let clarity_version = *contract_context.get_clarity_version();
let engine = global_context.engine.clone();
let context = ClarityWasmContext::new_run(
global_context,
contract_context,
Expand All @@ -499,7 +500,6 @@ pub fn call_function<'a>(
.contract_context()
.lookup_function(function_name)
.ok_or(CheckErrors::UndefinedFunction(function_name.to_string()))?;
let engine = Engine::default();
let module = context
.contract_context()
.with_wasm_module(|wasm_module| unsafe {
Expand Down
7 changes: 6 additions & 1 deletion clarity/src/vm/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ use serde_json::json;
use stacks_common::consts::CHAIN_ID_TESTNET;
use stacks_common::types::chainstate::StacksBlockId;
use stacks_common::types::StacksEpochId;
use wasmtime::{Engine, Linker};

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

#[derive(Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -1653,6 +1655,8 @@ impl<'a> GlobalContext<'a> {
cost_track: LimitedCostTracker,
epoch_id: StacksEpochId,
) -> GlobalContext {
let engine = Engine::default();

GlobalContext {
database,
cost_track,
Expand All @@ -1663,6 +1667,7 @@ impl<'a> GlobalContext<'a> {
epoch_id,
chain_id,
eval_hooks: None,
engine,
}
}

Expand Down

0 comments on commit 13ff1b9

Please sign in to comment.