Rust performance
Skill Mattakushi432/Claude-Code-Skills-Custom-DevTools-Pack/plugins/devtools-pack/skills/rust-performance
When to activate: Rust performance, profiling, zero-cost abstractions, iterators, SIMD, memory layout, flamegraph, allocation optimizationFrom its SKILL.md
npx -y skills add Mattakushi432/Claude-Code-Skills-Custom-DevTools-Pack --skill rust-performanceAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
4.1 KB, ~1.1k tokens by cl100k_base, as published. Nobody here has run it
Rust Performance Patterns
Profiling Tools
# Flamegraph (CPU profiling)
cargo install flamegraph
cargo flamegraph --bin my-server
# Benchmarks
cargo install cargo-criterion
cargo criterion
# Inspect generated assembly
cargo install cargo-asm
cargo asm my_crate::hot_function
# Memory profiling with dhat
# Add dhat feature to Cargo.toml and instrument main
Iterator Performance
Iterators compile to the same machine code as hand-written loops — zero overhead.
// Single-pass chain — no intermediate allocations
let result: Vec<_> = data.iter()
.filter(|&&x| x > 0)
.map(|&x| x * 2)
.collect();
// Avoid collect() in the middle of a chain
// BAD: two allocations
let filtered: Vec<_> = data.iter().filter(|&&x| x > 0).collect();
let mapped: Vec<_> = filtered.iter().map(|&&x| x * 2).collect();
Avoiding Allocations
// &str instead of String when ownership isn't needed
fn process(input: &str) -> usize { input.len() }
// Stack allocation for small collections
use smallvec::SmallVec;
let mut v: SmallVec<[u8; 16]> = SmallVec::new(); // on stack until > 16 bytes
// Cow for conditionally owned data
use std::borrow::Cow;
fn normalize(s: &str) -> Cow<str> {
if s.chars().all(|c| c.is_lowercase()) {
Cow::Borrowed(s)
} else {
Cow::Owned(s.to_lowercase())
}
}
// Pre-allocate known sizes
let mut result = Vec::with_capacity(items.len());
for item in &items { result.push(transform(item)); }
Memory Layout Optimization
// Largest fields first to minimize padding
#[derive(Debug)]
struct Optimized {
b: u64, // 8 bytes
d: u32, // 4 bytes
a: u8, // 1 byte
c: u8, // 1 byte
// 2 bytes padding
} // 16 bytes total
// Compare to field-order that wastes space
#[derive(Debug)]
struct Wasteful {
a: u8, // 1 byte + 7 padding
b: u64, // 8 bytes
c: u8, // 1 byte + 3 padding
d: u32, // 4 bytes
} // 24 bytes total
// Explicit layout control
#[repr(C)] // C-compatible layout
#[repr(align(64))] // cache-line aligned
struct CacheAligned { data: [u8; 64] }
Cache-Friendly Data Structures
// Structure of Arrays (SoA) — better for bulk operations than Array of Structs
struct Particles {
x: Vec<f32>,
y: Vec<f32>,
z: Vec<f32>,
mass: Vec<f32>,
}
fn update_x(p: &mut Particles, velocities: &[f32], dt: f32) {
// Sequential memory access = cache-friendly
for (x, &vx) in p.x.iter_mut().zip(velocities.iter()) {
*x += vx * dt;
}
}
Parallelism with rayon
use rayon::prelude::*;
let sum: i64 = data.par_iter().map(|&x| expensive(x)).sum();
let mut v: Vec<i32> = (0..1_000_000).collect();
v.par_sort();
String Performance
// write! to a pre-allocated String instead of format! in hot paths
use std::fmt::Write;
let mut s = String::with_capacity(64);
write!(s, "user_{}_event_{}", user_id, event_id).unwrap();
// smol_str: stack-allocated for short strings
use smol_str::SmolStr;
let s: SmolStr = "short string".into(); // no heap for ≤23 bytes
Release Profile Tuning
[profile.release]
opt-level = 3
lto = "thin" # link-time optimization
codegen-units = 1 # cross-function optimization
strip = true
panic = "abort" # no unwinding overhead
[profile.bench]
inherits = "release"
debug = true # keep symbols for profiling
Common Anti-Patterns
clone()in hot loops — profile first; often avoidable with references or restructuringStringwhere&strsuffices — unnecessary heap allocationdyn Traitin hot paths — use generics (monomorphization) for zero-cost dispatch- Blocking I/O on async runtimes — use
tokio::task::spawn_blocking - Profiling debug builds — always profile
--releasebuilds; debug is 10x slower
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.