Skip to content

Commit 13b0838

Browse files
committed
performance(sierra-gas): Compute topological order for prepare_wallet once.
SIERRA_UPDATE_PATCH_CHANGE_TAG=Just performance gain - no interface effect.
1 parent 4076e3f commit 13b0838

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

crates/cairo-lang-sierra-gas/src/compute_costs.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,14 @@ pub fn compute_costs<
136136
});
137137
}
138138

139-
context.prepare_wallet(specific_cost_context)?;
139+
let prepare_wallet_order = context.prepare_wallet_order()?;
140+
context.prepare_wallet(&prepare_wallet_order, specific_cost_context);
140141

141142
// Compute the excess cost and the corresponding target value for each statement.
142143
context.target_values = context.compute_target_values(specific_cost_context)?;
143144

144145
// Recompute the wallet values for each statement, after setting the target values.
145-
context.prepare_wallet(specific_cost_context)?;
146+
context.prepare_wallet(&prepare_wallet_order, specific_cost_context);
146147

147148
// Check that enforcing the wallet values succeeded.
148149
for (idx, value) in enforced_wallet_values.iter() {
@@ -448,34 +449,31 @@ impl<CostType: CostTypeTrait> CostContext<'_, CostType> {
448449
/// Prepares the values for [Self::wallet_at].
449450
fn prepare_wallet<SpecificCostContext: SpecificCostContextTrait<CostType>>(
450451
&mut self,
452+
order: &[StatementIdx],
451453
specific_cost_context: &SpecificCostContext,
452-
) -> Result<(), CostError> {
453-
let rev_topological_order = compute_reverse_topological_order(
454-
self.program.statements.len(),
455-
true,
456-
|current_idx| {
457-
match &self.program.get_statement(current_idx).unwrap() {
458-
Statement::Return(_) => {
459-
// Return has no dependencies.
460-
vec![]
461-
}
462-
Statement::Invocation(invocation) => get_branch_requirements_dependencies(
463-
current_idx,
464-
invocation,
465-
&self.branch_costs[current_idx.0],
466-
),
467-
}
468-
},
469-
)?;
470-
454+
) {
471455
self.costs.resize(self.program.statements.len(), Default::default());
472-
for current_idx in rev_topological_order {
456+
for idx in order {
473457
// The computation of the dependencies was completed.
474-
self.costs[current_idx.0] =
475-
self.no_cache_compute_wallet_at(&current_idx, specific_cost_context);
458+
self.costs[idx.0] = self.no_cache_compute_wallet_at(idx, specific_cost_context);
476459
}
460+
}
477461

478-
Ok(())
462+
/// Returns the order for the preparation of the wallet values for [Self::prepare_wallet].
463+
fn prepare_wallet_order(&self) -> Result<Vec<StatementIdx>, CostError> {
464+
compute_reverse_topological_order(self.program.statements.len(), true, |current_idx| {
465+
match &self.program.get_statement(current_idx).unwrap() {
466+
Statement::Return(_) => {
467+
// Return has no dependencies.
468+
vec![]
469+
}
470+
Statement::Invocation(invocation) => get_branch_requirements_dependencies(
471+
current_idx,
472+
invocation,
473+
&self.branch_costs[current_idx.0],
474+
),
475+
}
476+
})
479477
}
480478

481479
/// Helper function for `prepare_wallet()`.

0 commit comments

Comments
 (0)