Rust performance
Rust Layer 2 技能 - 性能优化,核心问题:瓶颈在哪里?掌握 benchmark、profiling、热点分析等性能调优方法From its SKILL.md
npx -y skills add morning-start/agent-skills --skill rust-performanceAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 1 stars1 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
3.4 KB, ~1.1k tokens by cl100k_base, as published. Nobody here has run it
Rust Performance - Layer 2
核心问题
瓶颈在哪里?
元认知追溯
问题 → Layer 2: 性能分析
↓
识别热点 → Layer 1: 语言机制优化
↓
架构决策 → Layer 3: 领域需求权衡
性能测量
Criterion - 基准测试
// Cargo.toml
[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
[[bench]]
name = "my_benchmark"
harness = false
// benches/my_benchmark.rs
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn fibonacci(n: u64) -> u64 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("fibonacci_20", |b| {
b.iter(|| fibonacci(black_box(20)))
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Iai - 静态分析
// benches/my_benchmark.rs
use iai::black_box;
fn fibonacci(n: u64) -> u64 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
iai::main!(func1, func2);
Profiling 工具
perf (Linux)
# 编译带调试信息
RUSTFLAGS="-g" cargo build --release
# 运行并记录
perf record -g -- ./target/release/my_app
# 生成火焰图
perf script | stackcollapse-perf.pl | flamegraph.pl > flamegraph.svg
cargo-flamegraph
# 安装
cargo install flamegraph
# 生成火焰图
cargo flamegraph --bin my_app
CPU 计数器
# Linux
perf stat -e cycles,instructions,cache-misses ./target/release/my_app
# macOS
Instruments.app (Instruments → Time Profiler)
常见优化模式
避免不必要的克隆
// 优化前
fn process(data: Vec<u8>) -> usize {
let data_clone = data.clone();
do_work(&data_clone).len()
}
// 优化后
fn process(data: &Vec<u8>) -> usize {
do_work(data).len()
}
使用合适的集合类型
// 小数据用栈
let arr: [i32; 3] = [1, 2, 3];
// 大数据用堆
let vec = vec![1, 2, 3];
// 固定大小用数组
let arr = [0u8; 1024];
减少动态分发
// 动态分发
fn process(items: &[&dyn Serializable]) { }
// 泛型静态分发
fn process<T: Serializable>(items: &[T]) { }
性能技巧
预分配容量
// 预分配减少重新分配
let mut vec = Vec::with_capacity(1000);
for i in 0..1000 {
vec.push(i);
}
缓存友好访问
// 缓存行大小约 64 字节
struct Row {
a: u64,
b: u64,
c: u64,
d: u64,
}
// 访问模式影响缓存命中率
并行化
use rayon::prelude::*;
let result: Vec<u64> = (0..1000)
.into_par_iter()
.map(|x| expensive_computation(x))
.collect();
资源索引
注意事项
- 先测量再优化,避免过早优化
- 用 benchmark 验证优化效果
- 权衡性能与可读性
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.
Gives 0 of the 12 instructions most performance cost skills give in ~1.1k tokens
Counted across 803 of the 1,058 authors here whose files we hold, read 2026-08-07
- Keep skill files under 500 lines or tokensin 82 of 803, across 16 files
- Use imperative form in instructionsin 80 of 803, across 9 files
- Draft assertions while test runs are in progressin 75 of 803, across 9 files
- Create two to three realistic test promptsin 74 of 803, across 9 files
- Write skill descriptions to be pushyin 72 of 803, across 7 files
- Save test cases to evals JSONin 72 of 803, across 6 files
- Ask questions about edge cases and input formatsin 72 of 803, across 7 files
- Save timing data immediately when runs completein 70 of 803, across 5 files
- Include all trigger conditions in the skill descriptionin 69 of 803, across 3 files
- Launch all test runs in a single turn or simultaneouslyin 69 of 803, across 3 files
- Capture intent before writing a skillin 67 of 803, across 1 file
- Import directly instead of barrel filesin 52 of 803, across 15 files
Said here and by no other author read
- use benchmark to verify optimization
- trade off performance against readability
- compile with debug symbols for profiling
- use static dispatch over dynamic dispatch
- preallocate capacity to prevent reallocation
Grouped from the skills themselves: near-identical wordings counted once, and counted by distinct author, so one author publishing three of these counts once. Length counted with cl100k_base; the agent that loads this file may tokenize it differently.