Skip to content

Commit 764a4a3

Browse files
⚡ Bolt: Optimize liveness dataflow iteration order and reduce allocations
Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent 5630c42 commit 764a4a3

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

‎compiler/optimizer/src/analysis/liveness.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ impl LivenessAnalysis {
6666

6767
while changed {
6868
changed = false;
69-
for block in &func.blocks {
69+
// OPTIMIZATION: Liveness is a backward dataflow analysis. Iterating
70+
// through blocks in reverse order speeds up convergence significantly.
71+
for block in func.blocks.iter().rev() {
7072
let block_id = block.id;
7173

7274
// LiveOut = union of LiveIn(succs)
@@ -89,14 +91,16 @@ impl LivenessAnalysis {
8991

9092
if let Some(old_in) = live_in.get_mut(&block_id) {
9193
if old_in.len() != new_in.len() {
92-
*old_in = new_in.clone();
94+
// OPTIMIZATION: Reuse existing HashSet capacity to avoid allocations in hot loop.
95+
old_in.clone_from(&new_in);
9396
changed = true;
9497
}
9598
}
9699

97100
if let Some(old_out) = live_out.get_mut(&block_id) {
98101
if old_out.len() != new_out.len() {
99-
*old_out = new_out.clone();
102+
// OPTIMIZATION: Reuse existing HashSet capacity to avoid allocations in hot loop.
103+
old_out.clone_from(&new_out);
100104
changed = true;
101105
}
102106
}

0 commit comments

Comments
 (0)