Skip to content

Performance: Audit and optimize clone() usage in hot paths #985

@EffortlessSteven

Description

@EffortlessSteven

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:

  1. Increases memory allocations
  2. Reduces performance in hot paths
  3. 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:

let mut out = a.clone();

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions