Skip to content

Commit 04fabf2

Browse files
Avoid copying from candidate. (#7527)
1 parent feddc9e commit 04fabf2

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

crates/cairo-lang-lowering/src/optimizations/match_optimizer.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ pub fn optimize_matches(lowered: &mut FlatLowered) {
169169
fn statement_can_be_optimized_out(
170170
stmt: &Statement,
171171
info: &mut AnalysisInfo<'_>,
172+
mut candidate: OptimizationCandidate<'_>,
172173
statement_location: (BlockId, usize),
173174
) -> Option<FixInfo> {
174175
let Statement::EnumConstruct(StatementEnumConstruct { variant, input, output }) = stmt else {
175176
return None;
176177
};
177-
let candidate = info.candidate.as_mut()?;
178178
if *output != candidate.match_variable {
179179
return None;
180180
}
@@ -198,7 +198,7 @@ fn statement_can_be_optimized_out(
198198
// Compute the demand based on the demand of the specific arm, rather than the current demand
199199
// (which contains the union of the demands from all the arms).
200200
// Apply the remapping of the input variable and the additional remappings if exist.
201-
let mut demand = candidate.arm_demands[arm_idx].clone();
201+
let mut demand = std::mem::take(&mut candidate.arm_demands[arm_idx]);
202202
demand
203203
.apply_remapping(&mut EmptyDemandReporter {}, [(var_id, (&input.var_id, ()))].into_iter());
204204

@@ -218,8 +218,8 @@ fn statement_can_be_optimized_out(
218218
arm_idx,
219219
target_block: arm.block_id,
220220
remapping,
221-
reachable_blocks: candidate.arm_reachable_blocks[arm_idx].clone(),
222-
additional_remapping: candidate.additional_remappings.clone().unwrap_or_default(),
221+
reachable_blocks: std::mem::take(&mut candidate.arm_reachable_blocks[arm_idx]),
222+
additional_remapping: candidate.additional_remappings.unwrap_or_default(),
223223
})
224224
}
225225

@@ -288,17 +288,20 @@ impl<'a> Analyzer<'a> for MatchOptimizerContext {
288288
statement_location: StatementLocation,
289289
stmt: &Statement,
290290
) {
291-
if let Some(fix_info) = statement_can_be_optimized_out(stmt, info, statement_location) {
292-
self.fixes.push(fix_info);
293-
} else {
294-
info.demand.variables_introduced(&mut EmptyDemandReporter {}, stmt.outputs(), ());
295-
info.demand.variables_used(
296-
&mut EmptyDemandReporter {},
297-
stmt.inputs().iter().map(|VarUsage { var_id, .. }| (var_id, ())),
298-
);
291+
if let Some(candidate) = std::mem::take(&mut info.candidate) {
292+
if let Some(fix_info) =
293+
statement_can_be_optimized_out(stmt, info, candidate, statement_location)
294+
{
295+
self.fixes.push(fix_info);
296+
return;
297+
}
299298
}
300299

301-
info.candidate = None;
300+
info.demand.variables_introduced(&mut EmptyDemandReporter {}, stmt.outputs(), ());
301+
info.demand.variables_used(
302+
&mut EmptyDemandReporter {},
303+
stmt.inputs().iter().map(|VarUsage { var_id, .. }| (var_id, ())),
304+
);
302305
}
303306

304307
fn visit_goto(

0 commit comments

Comments
 (0)