cargo-perf
Static analysis for async correctness and runtime performance in Rust.
The Problem
These bugs compile fine and ship to production:
// Blocks the async runtime — causes timeouts under load
async
// Deadlock — holds lock across yield point
async
// 737x slower — regex compilation in hot loop
for line in lines
cargo-perf catches all of these.
Installation
Usage
Rules
Errors (High Confidence)
| 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) |
Warnings (Medium Confidence)
| 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 |
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 |
CI Integration
# .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.
Suppressing warnings
// cargo-perf-ignore: clone-in-hot-loop
let owned = data.clone; // intentional in cold path
Or for a whole function:
Benchmarks
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.
IDE Integration
cargo-perf includes an LSP server for real-time diagnostics in your editor.
Installation
VS Code
See editors/vscode/ for the extension.
Neovim
require. =
require..
Other Editors
See editors/README.md for Emacs, Helix, Zed, and generic LSP setup.
Custom Rules (Plugin System)
Extend cargo-perf with your own rules:
use ;
use ;
;
let mut registry = new;
registry.add_rule;
let diagnostics = analyze_with_plugins?;
See examples/custom_rule.rs for a complete example.
License
MIT OR Apache-2.0