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 |
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 |
CI Integration
GitHub Action
Use the official GitHub Action for the easiest setup:
# .github/workflows/perf.yml
name: Performance Analysis
on:
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
Action Inputs
| 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 |
Manual Setup
# .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