Skip to content

Commit c8609da

Browse files
authored
Merge pull request #19 from Tcode-Motion/no-clones-optimizer-4831851234818099068
⚡ Bolt: Optimize PassManager to eliminate redundant `.clone()` allocations
2 parents 630c4a3 + d607eb8 commit c8609da

4 files changed

Lines changed: 3 additions & 7 deletions

File tree

.jules/bolt.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
## 2024-01-20 - Lifetime Refactoring in Parser to Eliminate Token Cloning
2-
**Learning:** In the Rust parser (`compiler/parser/src/parser.rs`), methods like `advance()` and `previous()` originally returned a reference tied to `&mut self`. Because `Token` was still borrowing `self` mutably, the parser couldn't call methods like `self.parse_prefix` (which requires another `&mut self` borrow) without first calling `.clone()` on the token to drop the initial borrow.
3-
**Action:** By explicitly defining the return lifetime as `&'a Token` (tied to the lifetime of the underlying token slice `&'a [Token]`, rather than the `Parser` instance), the mutable borrow of `self` ends immediately. This elegantly satisfies the borrow checker while removing the overhead of cloning tokens throughout `expressions.rs` and `statements.rs`. Look for similar lifetime constraints elsewhere in the compiler that force unnecessary copies.
4-

compiler/optimizer/src/pass_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl PassManager {
5050
let res = pass.run(module, &mut self.analyses);
5151
let duration = start.elapsed();
5252

53-
let mut pass_stats = res.stats.clone();
53+
let mut pass_stats = res.stats;
5454
pass_stats.time_taken_ns = duration.as_nanos();
5555

5656
self.stats.combine(&pass_stats);

compiler/optimizer/src/passes/dead_code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl OptimizationPass for DeadCode {
1919

2020
for func in &mut module.functions {
2121
// Retrieve Use-Def analysis
22-
let use_def = analyses.get_use_def(func).clone();
22+
let use_def = analyses.get_use_def(func);
2323

2424
for block in &mut func.blocks {
2525
block.instructions.retain(|inst| {

compiler/optimizer/src/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl OptimizationPipeline {
7474
}
7575

7676
if overall_changed {
77-
OptimizationResult::changed(self.manager.stats.clone())
77+
OptimizationResult::changed(self.manager.stats)
7878
} else {
7979
OptimizationResult::unchanged("pipeline")
8080
}

0 commit comments

Comments
 (0)