Skip to content

Commit a072fe9

Browse files
committed
feat: improve cost tracker performance
1 parent e2eacd2 commit a072fe9

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

clarity/src/vm/costs/mod.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::vm::types::Value::UInt;
3838
use crate::vm::types::{
3939
FunctionType, PrincipalData, QualifiedContractIdentifier, TupleData, TypeSignature,
4040
};
41-
use crate::vm::{eval_all, ClarityName, SymbolicExpression, Value};
41+
use crate::vm::{CallStack, ClarityName, Environment, LocalContext, SymbolicExpression, Value};
4242

4343
pub mod constants;
4444
pub mod cost_functions;
@@ -1054,7 +1054,7 @@ pub fn parse_cost(
10541054
// TODO: add tests from mutation testing results #4832
10551055
#[cfg_attr(test, mutants::skip)]
10561056
fn compute_cost(
1057-
cost_tracker: &mut TrackerData,
1057+
cost_tracker: &TrackerData,
10581058
cost_function_reference: ClarityCostFunctionReference,
10591059
input_sizes: &[u64],
10601060
eval_in_epoch: StacksEpochId,
@@ -1073,7 +1073,7 @@ fn compute_cost(
10731073

10741074
let cost_contract = cost_tracker
10751075
.cost_contracts
1076-
.get_mut(&cost_function_reference.contract_id)
1076+
.get(&cost_function_reference.contract_id)
10771077
.ok_or(CostErrors::CostComputationFailed(format!(
10781078
"CostFunction not found: {cost_function_reference}"
10791079
)))?;
@@ -1088,14 +1088,23 @@ fn compute_cost(
10881088
)));
10891089
}
10901090

1091-
let function_invocation = [SymbolicExpression::list(program)];
1091+
let function_invocation = SymbolicExpression::list(program);
1092+
let eval_result = global_context.execute(|global_context| {
1093+
let context = LocalContext::new();
1094+
let mut call_stack = CallStack::new();
1095+
let publisher: PrincipalData = cost_contract.contract_identifier.issuer.clone().into();
1096+
let mut env = Environment::new(
1097+
global_context,
1098+
cost_contract,
1099+
&mut call_stack,
1100+
Some(publisher.clone()),
1101+
Some(publisher.clone()),
1102+
None,
1103+
);
10921104

1093-
let eval_result = eval_all(
1094-
&function_invocation,
1095-
cost_contract,
1096-
&mut global_context,
1097-
None,
1098-
);
1105+
let result = super::eval(&function_invocation, &mut env, &context)?;
1106+
Ok(Some(result))
1107+
});
10991108

11001109
parse_cost(&cost_function_reference.to_string(), eval_result)
11011110
}

0 commit comments

Comments
 (0)