Issue
Numerous .clone() calls were identified across the codebase. While many are necessary, some may be avoidable with better borrowing patterns, particularly in hot paths or loops.
High-Volume Clone Locations
| File |
Pattern |
Concern |
crates/tokmd-analysis-derived/src/lib.rs |
Multiple row.module.clone(), row.path.clone() |
Aggregation hot path |
crates/tokmd-analysis-git/src/git.rs |
commit.author.clone(), key.clone() |
Git analysis loop |
crates/tokmd-analysis-topics/src/lib.rs |
row.module.clone(), term.clone() |
Topic modeling |
crates/tokmd-analysis-api-surface/src/lib.rs |
row.lang.clone(), row.module.clone() |
API analysis |
crates/tokmd-analysis-near-dup/src/lib.rs |
files[idx_a].path.clone() |
Near-duplicate detection |
crates/tokmd-analysis-effort/src/model.rs |
a.clone() in merge operation |
Effort estimation |
crates/tokmd-analysis-fun/src/lib.rs |
Multiple zero_row.clone() in vec initialization |
Fun reporting |
Problem
Unnecessary cloning:
- Increases memory allocations
- Reduces performance in hot paths
- Adds GC pressure in WASM targets
Suggested Improvements
Use references where possible
Instead of:
module_totals.entry(row.module.clone())
Consider:
module_totals.entry(&row.module) // Using &str as key
Pre-allocate with capacity
Instead of:
Consider explicit capacity:
let mut out = Vec::with_capacity(a.len());
out.extend_from_slice(&a);
Use Arc/Rc for shared ownership
For data shared across multiple structures, consider Arc<str> or Arc<String> instead of cloning.
Audit hotspots with profiling
Run with cargo flamegraph or similar to identify which clones actually impact performance.
Severity
Low-Medium - Performance optimization opportunity
Issue
Numerous
.clone()calls were identified across the codebase. While many are necessary, some may be avoidable with better borrowing patterns, particularly in hot paths or loops.High-Volume Clone Locations
crates/tokmd-analysis-derived/src/lib.rsrow.module.clone(),row.path.clone()crates/tokmd-analysis-git/src/git.rscommit.author.clone(),key.clone()crates/tokmd-analysis-topics/src/lib.rsrow.module.clone(),term.clone()crates/tokmd-analysis-api-surface/src/lib.rsrow.lang.clone(),row.module.clone()crates/tokmd-analysis-near-dup/src/lib.rsfiles[idx_a].path.clone()crates/tokmd-analysis-effort/src/model.rsa.clone()in merge operationcrates/tokmd-analysis-fun/src/lib.rszero_row.clone()in vec initializationProblem
Unnecessary cloning:
Suggested Improvements
Use references where possible
Instead of:
Consider:
Pre-allocate with capacity
Instead of:
Consider explicit capacity:
Use Arc/Rc for shared ownership
For data shared across multiple structures, consider
Arc<str>orArc<String>instead of cloning.Audit hotspots with profiling
Run with
cargo flamegraphor similar to identify which clones actually impact performance.Severity
Low-Medium - Performance optimization opportunity