diff --git a/crates/cairo-lang-lowering/src/reorganize_blocks.rs b/crates/cairo-lang-lowering/src/reorganize_blocks.rs index aace6bd59bb..19e769ca2e7 100644 --- a/crates/cairo-lang-lowering/src/reorganize_blocks.rs +++ b/crates/cairo-lang-lowering/src/reorganize_blocks.rs @@ -21,7 +21,7 @@ pub fn reorganize_blocks<'db>(lowered: &mut Lowered<'db>) { return; } let mut ctx = TopSortContext { - old_block_rev_order: Default::default(), + old_block_rev_order: Vec::with_capacity(lowered.blocks.len()), incoming_gotos: vec![0; lowered.blocks.len()], can_be_merged: vec![true; lowered.blocks.len()], remappings_ctx: Context::new(lowered.variables.len()), @@ -75,10 +75,11 @@ pub fn reorganize_blocks<'db>(lowered: &mut Lowered<'db>) { let mut block = &lowered.blocks[block_id]; loop { - for stmt in &block.statements { - statements - .push(var_reassigner.rebuild_statement(&rebuilder.rebuild_statement(stmt))); - } + statements.extend( + block.statements.iter().map(|stmt| { + var_reassigner.rebuild_statement(&rebuilder.rebuild_statement(stmt)) + }), + ); if let BlockEnd::Goto(target_block_id, remappings) = &block.end && !rebuilder.block_remapping.contains_key(target_block_id) { diff --git a/crates/cairo-lang-sierra-gas/src/compute_costs.rs b/crates/cairo-lang-sierra-gas/src/compute_costs.rs index 6084e94f1cd..ecd9cb29c0c 100644 --- a/crates/cairo-lang-sierra-gas/src/compute_costs.rs +++ b/crates/cairo-lang-sierra-gas/src/compute_costs.rs @@ -214,27 +214,22 @@ fn get_branch_requirements_dependencies( /// Rectify (see [CostTypeTrait::rectify]) is needed in case the branch cost is negative /// (e.g., in `coupon_refund`). fn get_branch_requirements< + 'a, CostType: CostTypeTrait, SpecificCostContext: SpecificCostContextTrait, >( - specific_context: &SpecificCostContext, - wallet_at_fn: &impl Fn(&StatementIdx) -> WalletInfo, - idx: &StatementIdx, - invocation: &Invocation, - libfunc_cost: &[BranchCost], + specific_context: &'a SpecificCostContext, + wallet_at_fn: &'a impl Fn(&StatementIdx) -> WalletInfo, + idx: &'a StatementIdx, + invocation: &'a Invocation, + libfunc_cost: &'a [BranchCost], rectify: bool, -) -> Vec> { - zip_eq(&invocation.branches, libfunc_cost) - .map(|(branch_info, branch_cost)| { - let res = specific_context.get_branch_requirement( - wallet_at_fn, - idx, - branch_info, - branch_cost, - ); - if rectify { res.rectify() } else { res } - }) - .collect() +) -> impl ExactSizeIterator> + 'a { + zip_eq(&invocation.branches, libfunc_cost).map(move |(branch_info, branch_cost)| { + let res = + specific_context.get_branch_requirement(wallet_at_fn, idx, branch_info, branch_cost); + if rectify { res.rectify() } else { res } + }) } /// For every `branch_align`, `withdraw_gas`, `redeposit_gas` and `coupon_refund` statements, @@ -257,9 +252,10 @@ fn analyze_gas_statements< return Ok(()); }; let libfunc_cost = &context.branch_costs[idx.0]; - let branch_requirements: Vec> = get_branch_requirements( + let wallet_at_fn = &|statement_idx: &StatementIdx| context.wallet_at(statement_idx); + let branch_requirements = get_branch_requirements( specific_context, - &|statement_idx| context.wallet_at(statement_idx), + wallet_at_fn, idx, invocation, libfunc_cost, @@ -269,7 +265,7 @@ fn analyze_gas_statements< let wallet_value = context.wallet_at(idx).value; for (branch_info, branch_cost, branch_requirement) in - zip_eq3(&invocation.branches, libfunc_cost, &branch_requirements) + zip_eq3(&invocation.branches, libfunc_cost, branch_requirements) { if let BranchCost::WithdrawGas(WithdrawGasBranchInfo { success: true, .. }) = branch_cost { // Note that `idx.next(&branch_info.target)` is indeed branch align due to @@ -359,12 +355,11 @@ impl WalletInfo { /// `target_value` is the target value for this statement. See [CostContext::target_values]. fn merge( branch_costs: &[BranchCost], - branches: Vec, + branches: impl ExactSizeIterator, target_value: Option<&CostType>, ) -> Self { let n_branches = branches.len(); - let mut max_value = - CostType::max(branches.iter().map(|wallet_info| wallet_info.value.clone())); + let mut max_value = CostType::max(branches.map(|wallet_info| wallet_info.value)); // If there are multiple branches, there must be a branch_align in each of them, which // can be used to increase the wallet value up to the target value. @@ -490,9 +485,10 @@ impl CostContext<'_, CostType> { let libfunc_cost = &self.branch_costs[idx.0]; // For each branch, compute the required value for the wallet. - let branch_requirements: Vec> = get_branch_requirements( + let wallet_at_fn = &|statement_idx: &StatementIdx| self.wallet_at(statement_idx); + let branch_requirements = get_branch_requirements( specific_cost_context, - &|statement_idx| self.wallet_at(statement_idx), + wallet_at_fn, idx, invocation, libfunc_cost, @@ -601,9 +597,10 @@ impl CostContext<'_, CostType> { let libfunc_cost = &self.branch_costs[idx.0]; + let wallet_at_fn = &|statement_idx: &StatementIdx| self.wallet_at(statement_idx); let branch_requirements = get_branch_requirements( specific_cost_context, - &|statement_idx| self.wallet_at(statement_idx), + wallet_at_fn, idx, invocation, libfunc_cost,