cargo-perf
Preventive performance analysis for Rust. Catch performance anti-patterns before they reach production.
Installation
Usage
# Analyze current directory
# Analyze specific path
# Output as JSON
# Output as SARIF (for GitHub integration)
# List available rules
# Initialize config file
Rules
| Rule | Severity | Description |
|---|---|---|
async-block-in-async |
Error | Detects blocking std calls inside async functions |
clone-in-hot-loop |
Warning | Detects .clone() calls on heap types inside loops |
regex-in-loop |
Warning | Detects Regex::new() inside loops |
collect-then-iterate |
Warning | Detects .collect() immediately followed by .iter() |
Configuration
Create cargo-perf.toml in your project root:
[]
# Set rule severity: "deny" (error), "warn" (warning), "allow" (ignore)
= "deny"
= "warn"
= "allow"
[]
= "console" # "console", "json", "sarif"
= "auto" # "auto", "always", "never"
Dog-fooding Results
cargo-perf is tested on itself. During development, it detected a real issue in its own codebase:
$ cargo-perf check src/
warning: `.clone()` called inside loop; consider borrowing or moving the clone outside [clone-in-hot-loop]
--> src/reporter/sarif.rs:101:40
help: Use a reference or move the clone outside the loop
Found 1 warning(s)
The warning identified a .clone() call inside a loop in the SARIF reporter. This was refactored to collect data outside the loop, eliminating the unnecessary allocations. After the fix:
$ cargo-perf check src/
No performance issues found.
This demonstrates that cargo-perf catches real performance issues, even in well-reviewed code.
CI Integration
GitHub Actions with SARIF
- name: Run cargo-perf
run: |
cargo install cargo-perf
cargo perf --format sarif > results.sarif
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: results.sarif
Fail on Warnings
License
MIT OR Apache-2.0