Skip to content

Commit 49fe4e4

Browse files
committed
performance(vec-alloaction): Simpler vector allocations.
SIERRA_UPDATE_PATCH_CHANGE_TAG=Just performance gain - no interface effect.
1 parent b76eb60 commit 49fe4e4

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

crates/cairo-lang-lowering/src/reorganize_blocks.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn reorganize_blocks<'db>(lowered: &mut Lowered<'db>) {
2121
return;
2222
}
2323
let mut ctx = TopSortContext {
24-
old_block_rev_order: Default::default(),
24+
old_block_rev_order: Vec::with_capacity(lowered.blocks.len()),
2525
incoming_gotos: vec![0; lowered.blocks.len()],
2626
can_be_merged: vec![true; lowered.blocks.len()],
2727
remappings_ctx: Context::new(lowered.variables.len()),
@@ -75,10 +75,11 @@ pub fn reorganize_blocks<'db>(lowered: &mut Lowered<'db>) {
7575

7676
let mut block = &lowered.blocks[block_id];
7777
loop {
78-
for stmt in &block.statements {
79-
statements
80-
.push(var_reassigner.rebuild_statement(&rebuilder.rebuild_statement(stmt)));
81-
}
78+
statements.extend(
79+
block.statements.iter().map(|stmt| {
80+
var_reassigner.rebuild_statement(&rebuilder.rebuild_statement(stmt))
81+
}),
82+
);
8283
if let BlockEnd::Goto(target_block_id, remappings) = &block.end
8384
&& !rebuilder.block_remapping.contains_key(target_block_id)
8485
{

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

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -214,27 +214,22 @@ fn get_branch_requirements_dependencies(
214214
/// Rectify (see [CostTypeTrait::rectify]) is needed in case the branch cost is negative
215215
/// (e.g., in `coupon_refund`).
216216
fn get_branch_requirements<
217+
'a,
217218
CostType: CostTypeTrait,
218219
SpecificCostContext: SpecificCostContextTrait<CostType>,
219220
>(
220-
specific_context: &SpecificCostContext,
221-
wallet_at_fn: &impl Fn(&StatementIdx) -> WalletInfo<CostType>,
222-
idx: &StatementIdx,
223-
invocation: &Invocation,
224-
libfunc_cost: &[BranchCost],
221+
specific_context: &'a SpecificCostContext,
222+
wallet_at_fn: &'a impl Fn(&StatementIdx) -> WalletInfo<CostType>,
223+
idx: &'a StatementIdx,
224+
invocation: &'a Invocation,
225+
libfunc_cost: &'a [BranchCost],
225226
rectify: bool,
226-
) -> Vec<WalletInfo<CostType>> {
227-
zip_eq(&invocation.branches, libfunc_cost)
228-
.map(|(branch_info, branch_cost)| {
229-
let res = specific_context.get_branch_requirement(
230-
wallet_at_fn,
231-
idx,
232-
branch_info,
233-
branch_cost,
234-
);
235-
if rectify { res.rectify() } else { res }
236-
})
237-
.collect()
227+
) -> impl ExactSizeIterator<Item = WalletInfo<CostType>> + 'a {
228+
zip_eq(&invocation.branches, libfunc_cost).map(move |(branch_info, branch_cost)| {
229+
let res =
230+
specific_context.get_branch_requirement(wallet_at_fn, idx, branch_info, branch_cost);
231+
if rectify { res.rectify() } else { res }
232+
})
238233
}
239234

240235
/// For every `branch_align`, `withdraw_gas`, `redeposit_gas` and `coupon_refund` statements,
@@ -257,9 +252,10 @@ fn analyze_gas_statements<
257252
return Ok(());
258253
};
259254
let libfunc_cost = &context.branch_costs[idx.0];
260-
let branch_requirements: Vec<WalletInfo<CostType>> = get_branch_requirements(
255+
let wallet_at_fn = &|statement_idx: &StatementIdx| context.wallet_at(statement_idx);
256+
let branch_requirements = get_branch_requirements(
261257
specific_context,
262-
&|statement_idx| context.wallet_at(statement_idx),
258+
wallet_at_fn,
263259
idx,
264260
invocation,
265261
libfunc_cost,
@@ -269,7 +265,7 @@ fn analyze_gas_statements<
269265
let wallet_value = context.wallet_at(idx).value;
270266

271267
for (branch_info, branch_cost, branch_requirement) in
272-
zip_eq3(&invocation.branches, libfunc_cost, &branch_requirements)
268+
zip_eq3(&invocation.branches, libfunc_cost, branch_requirements)
273269
{
274270
if let BranchCost::WithdrawGas(WithdrawGasBranchInfo { success: true, .. }) = branch_cost {
275271
// Note that `idx.next(&branch_info.target)` is indeed branch align due to
@@ -359,12 +355,11 @@ impl<CostType: CostTypeTrait> WalletInfo<CostType> {
359355
/// `target_value` is the target value for this statement. See [CostContext::target_values].
360356
fn merge(
361357
branch_costs: &[BranchCost],
362-
branches: Vec<Self>,
358+
branches: impl ExactSizeIterator<Item = Self>,
363359
target_value: Option<&CostType>,
364360
) -> Self {
365361
let n_branches = branches.len();
366-
let mut max_value =
367-
CostType::max(branches.iter().map(|wallet_info| wallet_info.value.clone()));
362+
let mut max_value = CostType::max(branches.map(|wallet_info| wallet_info.value));
368363

369364
// If there are multiple branches, there must be a branch_align in each of them, which
370365
// can be used to increase the wallet value up to the target value.
@@ -490,9 +485,10 @@ impl<CostType: CostTypeTrait> CostContext<'_, CostType> {
490485
let libfunc_cost = &self.branch_costs[idx.0];
491486

492487
// For each branch, compute the required value for the wallet.
493-
let branch_requirements: Vec<WalletInfo<CostType>> = get_branch_requirements(
488+
let wallet_at_fn = &|statement_idx: &StatementIdx| self.wallet_at(statement_idx);
489+
let branch_requirements = get_branch_requirements(
494490
specific_cost_context,
495-
&|statement_idx| self.wallet_at(statement_idx),
491+
wallet_at_fn,
496492
idx,
497493
invocation,
498494
libfunc_cost,
@@ -601,9 +597,10 @@ impl<CostType: CostTypeTrait> CostContext<'_, CostType> {
601597

602598
let libfunc_cost = &self.branch_costs[idx.0];
603599

600+
let wallet_at_fn = &|statement_idx: &StatementIdx| self.wallet_at(statement_idx);
604601
let branch_requirements = get_branch_requirements(
605602
specific_cost_context,
606-
&|statement_idx| self.wallet_at(statement_idx),
603+
wallet_at_fn,
607604
idx,
608605
invocation,
609606
libfunc_cost,

0 commit comments

Comments
 (0)