| Crates.io | cargo-perf |
| lib.rs | cargo-perf |
| version | 0.6.0 |
| created_at | 2026-01-06 03:19:58.088896+00 |
| updated_at | 2026-01-13 04:58:11.938637+00 |
| description | Preventive performance analysis for Rust - catch anti-patterns before production |
| homepage | |
| repository | https://github.com/cschuman/cargo-perf |
| max_upload_size | |
| id | 2025115 |
| size | 482,287 |
Static analysis for async correctness and runtime performance in Rust.
These bugs compile fine and ship to production:
// Blocks the async runtime — causes timeouts under load
async fn read_config() -> Config {
let data = std::fs::read_to_string("config.toml").unwrap();
toml::from_str(&data).unwrap()
}
// Deadlock — holds lock across yield point
async fn update(mutex: &tokio::sync::Mutex<Data>) {
let guard = mutex.lock().await;
some_async_op().await; // guard still held across await
}
// 737x slower — regex compilation in hot loop
for line in lines {
if Regex::new(r"\d+").unwrap().is_match(line) { ... }
}
cargo-perf catches all of these.
cargo install cargo-perf
cargo perf # Analyze current directory
cargo perf --strict # High-confidence rules only (CI recommended)
cargo perf --strict --fail-on error # Fail CI on issues
cargo perf --format sarif # For GitHub Code Scanning
cargo perf fix --dry-run # Preview auto-fixes
cargo perf fix # Apply auto-fixes
| Rule | What it catches |
|---|---|
async-block-in-async |
std::fs, thread::sleep, blocking I/O in async functions |
lock-across-await |
MutexGuard/RwLockGuard held across .await (deadlock risk) |
n-plus-one-query |
Database queries inside loops (SQLx, Diesel, SeaORM) |
| Rule | What it catches | Impact |
|---|---|---|
unbounded-channel |
mpsc::channel(), unbounded_channel() |
Memory exhaustion |
unbounded-spawn |
tokio::spawn in loops |
Resource exhaustion |
regex-in-loop |
Regex::new() inside loops |
737x slower |
clone-in-hot-loop |
.clone() on heap types in loops |
48x slower |
collect-then-iterate |
.collect().iter() |
2.3x slower |
vec-no-capacity |
Vec::new() + push in loop |
1.8x slower |
hashmap-no-capacity |
HashMap::new() + insert in loop |
Repeated rehashing |
string-no-capacity |
String::new() + push_str in loop |
Repeated realloc |
format-in-loop |
format!() inside loops |
Allocates each iteration |
string-concat-loop |
String + in loops |
Use push_str() |
mutex-in-loop |
Lock acquired inside loop | Acquire once outside |
Use the official GitHub Action for the easiest setup:
# .github/workflows/perf.yml
name: Performance Analysis
on: [push, pull_request]
jobs:
cargo-perf:
runs-on: ubuntu-latest
permissions:
contents: read
security-events: write # For SARIF upload
steps:
- uses: actions/checkout@v4
- uses: cschuman/cargo-perf@v1
with:
path: '.'
fail-on-error: 'true'
sarif: 'true' # Enables GitHub Code Scanning integration
| Input | Default | Description |
|---|---|---|
path |
. |
Path to analyze |
fail-on-error |
true |
Fail if errors found |
fail-on-warning |
false |
Fail if warnings found |
sarif |
true |
Upload results to GitHub Code Scanning |
version |
latest |
cargo-perf version to install |
# .github/workflows/ci.yml
- name: Performance lint
run: |
cargo install cargo-perf
cargo perf --strict --fail-on error
For a complete workflow with SARIF integration for GitHub Code Scanning, see examples/github-workflow.yml.
// cargo-perf-ignore: clone-in-hot-loop
let owned = data.clone(); // intentional in cold path
Or for a whole function:
#[allow(cargo_perf::clone_in_hot_loop)]
fn cold_path() { ... }
Real measurements (Apple M1 Pro, 1000 iterations):
| Anti-pattern | Impact |
|---|---|
Regex::new() in loop |
737x slower |
clone() in loop |
48x slower |
collect().iter() |
2.3x slower |
| Blocking in async | Blocks runtime thread |
| Lock across await | Deadlock |
See benchmarks/ for methodology.
cargo-perf includes an LSP server for real-time diagnostics in your editor.
cargo install cargo-perf --features lsp
See editors/vscode/ for the extension.
require('lspconfig.configs').cargo_perf = {
default_config = {
cmd = { 'cargo-perf', 'lsp' },
filetypes = { 'rust' },
root_dir = require('lspconfig.util').root_pattern('Cargo.toml'),
},
}
require('lspconfig').cargo_perf.setup({})
See editors/README.md for Emacs, Helix, Zed, and generic LSP setup.
Extend cargo-perf with your own rules:
use cargo_perf::plugin::{PluginRegistry, analyze_with_plugins};
use cargo_perf::rules::{Rule, Diagnostic, Severity};
struct MyCustomRule;
impl Rule for MyCustomRule {
fn id(&self) -> &'static str { "my-rule" }
fn name(&self) -> &'static str { "My Rule" }
fn description(&self) -> &'static str { "Detects my anti-pattern" }
fn default_severity(&self) -> Severity { Severity::Warning }
fn check(&self, ctx: &AnalysisContext) -> Vec<Diagnostic> {
// Your detection logic
Vec::new()
}
}
let mut registry = PluginRegistry::new();
registry.add_rule(Box::new(MyCustomRule));
let diagnostics = analyze_with_plugins(path, &config, ®istry)?;
See examples/custom_rule.rs for a complete example.
MIT OR Apache-2.0