Crate cargo_perf

Crate cargo_perf 

Source
Expand description

§cargo-perf

Static analysis for async Rust performance anti-patterns.

cargo-perf complements clippy with checks specific to async code and common loop-related performance issues that clippy doesn’t catch.

§Quick Start

use cargo_perf::{analyze, Config};
use std::path::Path;

let config = Config::default();
let diagnostics = analyze(Path::new("src/"), &config).unwrap();

for diag in diagnostics {
    println!("{}: {} at {}:{}",
        diag.rule_id,
        diag.message,
        diag.file_path.display(),
        diag.line
    );
}

§Rules

cargo-perf includes 12 rules organized into categories:

§Async Rules (Errors)

  • async-block-in-async: Blocking std calls in async functions
  • lock-across-await: Lock guards held across .await points

§Database Rules (Errors)

  • n-plus-one-query: Database queries inside loops (SQLx, Diesel, SeaORM)

§Async Warnings

  • unbounded-channel: Unbounded channels that can cause memory exhaustion
  • unbounded-spawn: Task spawning in loops without concurrency limits

§Loop Rules (Warnings)

  • clone-in-hot-loop: .clone() on heap types inside loops
  • regex-in-loop: Regex::new() inside loops
  • format-in-loop: format!() inside loops
  • string-concat-loop: String + operator in loops
  • vec-no-capacity: Vec::new() + push in loop
  • mutex-in-loop: Mutex::lock() inside loops

§Iterator Rules (Warnings)

  • collect-then-iterate: .collect().iter() anti-pattern

§Extending with Custom Rules

Use the plugin module to add custom rules:

use cargo_perf::plugin::{PluginRegistry, analyze_with_plugins};

let mut registry = PluginRegistry::new();
registry.add_builtin_rules();
registry.add_rule(Box::new(MyCustomRule));

let diagnostics = analyze_with_plugins(path, &config, &registry)?;

§Configuration

Rules can be configured via cargo-perf.toml:

[rules]
async-block-in-async = "deny"   # error
clone-in-hot-loop = "warn"      # warning
regex-in-loop = "allow"         # disabled

§Suppression

Suppress warnings inline:

// Attribute-based (function/item scope)
#[allow(cargo_perf::clone_in_hot_loop)]
fn my_function() { /* ... */ }

// Comment-based (next line only)
// cargo-perf-ignore: clone-in-hot-loop
let x = data.clone();

Re-exports§

pub use baseline::Baseline;
pub use config::Config;
pub use engine::AnalysisContext;
pub use engine::AnalysisProgress;
pub use engine::Engine;
pub use error::Error;
pub use error::Result;
pub use fix::FixError;
pub use plugin::analyze_with_plugins;
pub use plugin::PluginRegistry;
pub use plugin::PluginRegistryBuilder;
pub use rules::Diagnostic;
pub use rules::Fix;
pub use rules::Replacement;
pub use rules::Rule;
pub use rules::Severity;

Modules§

baseline
Baseline support for ignoring known issues.
config
discovery
File discovery utilities for cargo-perf.
engine
Analysis engine - coordinates file discovery and rule execution.
error
Custom error types for cargo-perf.
fix
Auto-fix functionality for applying suggested code changes.
plugin
Plugin system for extending cargo-perf with custom rules.
reporter
rules
suppression
Inline suppression support for cargo-perf diagnostics.

Macros§

define_rule
A helper macro for defining custom rules more concisely.
impl_loop_tracking_visitor
Macro to implement loop-tracking visitor methods.

Functions§

analyze
Analyze Rust files at the given path for performance anti-patterns.