rust-performance
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Performance
Rust 性能
Optimize from evidence. Define the user-visible metric, reproduce the workload, establish a stable baseline, profile the dominant cost, change one cause, and prove both correctness and improvement.
基于证据进行优化。定义用户可感知的指标,复现工作负载,建立稳定的基线,定位主要开销,每次只改变一个原因,并证明正确性与性能改进。
Scope and Routing
范围与路由
Use this skill for microbenchmarks, end-to-end load tests, CPU profiles, allocation and heap analysis, binary size, compile time, cache behavior, and regression budgets.
Route concurrency semantics to , telemetry pipelines to , general test architecture to , release profiles and features to , and semantic review to .
rust-concurrencyrust-observabilityrust-testingrust-cargo-buildrust-code-review当涉及微基准测试、端到端负载测试、CPU profile、内存分配与堆分析、二进制大小、编译时间、缓存行为和回归预算时,使用此 skill。
将并发语义路由至 ,将遥测数据管道路由至 ,将通用测试架构路由至 ,将 release profile 和 feature 路由至 ,将语义审查路由至 。
rust-concurrencyrust-observabilityrust-testingrust-cargo-buildrust-code-reviewWorkflow
工作流
1. Define the performance contract
1. 定义性能契约
Record the workload, dataset, inputs, concurrency, platform, CPU and memory limits, toolchain, target, features, allocator, build profile, warm-up, cache state, and success metrics. Distinguish throughput from p50/p95/p99 latency and steady-state from startup or shutdown.
Do not optimize debug builds or synthetic inputs unless they represent the actual problem.
记录工作负载、数据集、输入、并发度、平台、CPU 和内存限制、工具链、目标平台、feature、分配器、构建 profile、预热过程、缓存状态以及成功指标。区分吞吐量与 p50/p95/p99 延迟,以及稳态与启动或关闭阶段。
除非 debug 构建或合成输入能够代表实际问题,否则不要对其进行优化。
2. Establish a reproducible baseline
2. 建立可复现的基线
- Verify correctness before benchmarking.
- Use release-like optimization, codegen units, LTO, panic strategy, and target CPU settings intentionally.
- Isolate background load, power management, thermal throttling, and noisy shared runners.
- Record raw samples and uncertainty, not only one average.
- Compare the same commit, toolchain, dependency graph, and hardware when enforcing small regressions.
Read Benchmarking and Regression Gates.
- 在进行基准测试前验证正确性。
- 根据实际情况有意识地使用接近 release 的优化、codegen units、LTO、panic strategy 和目标 CPU 设置。
- 隔离后台负载、电源管理、热降频和存在噪声的共享 runner。
- 记录原始样本和不确定性,而不只是单个平均值。
- 在判定小幅回归时,应使用相同的 commit、工具链、依赖图和硬件进行比较。
阅读基准测试与回归门禁。
3. Choose the measurement layer
3. 选择测量层级
| Question | Tool or evidence |
|---|---|
| Is a pure operation faster? | Criterion or Divan microbenchmark |
| Did instruction-level cost change? | iai-callgrind on supported systems |
| Where is CPU time spent? | samply, cargo-flamegraph, Instruments, perf, pprof |
| Where are allocations retained? | DHAT, heap profiler, allocator metrics |
| Why is the binary large? | cargo-bloat and feature inspection |
| What drives compile time? | Cargo build timings and cargo-llvm-lines |
| Does the service meet its SLO? | Representative load test plus observability |
A microbenchmark does not prove end-to-end latency, overload behavior, or memory bounds.
| 问题 | 工具或证据 |
|---|---|
| 纯操作是否变快? | Criterion 或 Divan 微基准测试 |
| 指令级开销是否发生变化? | 在受支持的系统上使用 iai-callgrind |
| CPU 时间花在何处? | samply、cargo-flamegraph、Instruments、perf、pprof |
| 哪些内存分配被保留? | DHAT、heap profiler、allocator metrics |
| 二进制文件为何较大? | cargo-bloat 和 feature 检查 |
| 什么因素决定编译时间? | Cargo build timings 和 cargo-llvm-lines |
| 服务是否满足 SLO? | 具有代表性的负载测试和可观测性 |
微基准测试无法证明端到端延迟、过载行为或内存边界。
4. Profile before editing
4. 修改前先进行性能分析
Capture a profile under the failing workload. Preserve symbols and use the same optimized artifact intended for comparison. Separate on-CPU work, waiting, lock contention, I/O, allocation, page faults, and scheduler overhead. Read CPU and Latency Profiling.
在问题工作负载下采集 profile。保留符号,并使用拟用于比较的同一优化构建产物。区分 CPU 实际执行的工作、等待、锁竞争、I/O、内存分配、缺页错误和调度器开销。阅读CPU 与延迟性能分析。
5. Optimize the dominant cause
5. 优化主要开销
Common changes include algorithmic complexity, fewer passes, better batching, avoiding repeated parsing or allocation, borrowing instead of cloning, data-layout improvements, reduced synchronization, bounded queues, streaming, feature reduction, and moving CPU work off async workers.
Do not use unsafe, custom allocators, SIMD, lock-free structures, or caching until simpler changes are insufficient and invariants plus fallback behavior are tested.
常见改动包括优化算法复杂度、减少遍历次数、改进批处理、避免重复解析或内存分配、借用而非克隆、改进数据布局、减少同步、使用有界队列、采用流式处理、减少 feature,以及将 CPU 密集型工作移出 async worker。
在简单改动不足之前,不要使用 unsafe、自定义分配器、SIMD、无锁结构或缓存;使用它们之前,应充分测试不变量和回退行为。
6. Measure memory, size, and build cost
6. 测量内存、大小和构建成本
Track peak RSS, retained heap, allocation rate, fragmentation, cache growth, buffer bounds, binary sections, monomorphization, debug information, enabled features, macro expansion, and incremental versus clean compile time. Read Memory, Binary Size, and Compile Time.
跟踪峰值 RSS、保留堆、分配速率、碎片、缓存增长、缓冲区边界、二进制文件的各个 section、单态化、调试信息、启用的 feature、宏展开,以及增量编译与全新编译的耗时。阅读内存、二进制大小与编译时间。
7. Prove the result
7. 证明结果
Rerun correctness tests and the exact baseline protocol. Report absolute and relative results, variance, hardware and software context, tradeoffs, and regressions in secondary metrics. Add a durable benchmark or budget only when the environment can detect the intended threshold reliably.
重新运行正确性测试,并使用完全相同的基线协议进行测量。报告绝对结果与相对结果、方差、硬件和软件环境、权衡,以及次要指标中的回归。只有当环境能够可靠检测目标阈值时,才添加持久化 benchmark 或性能预算。
Completion Criteria
完成标准
- Define a representative workload and user-visible metric.
- Record reproducible baseline evidence before optimization.
- Profile and identify the dominant cost rather than guessing.
- Preserve correctness and safety through targeted tests.
- Re-measure under the same protocol and report uncertainty plus tradeoffs.
- Add a stable regression check or explain why the environment is too noisy.
- 定义具有代表性的工作负载和用户可感知的指标。
- 在优化前记录可复现的基线证据。
- 通过性能分析定位主要开销,而不是依靠猜测。
- 通过针对性测试保持正确性和安全性。
- 使用相同协议重新测量,并报告不确定性和权衡。
- 添加稳定的回归检查,或说明环境噪声为何过大。
Resources
资源
- Benchmarking and Regression Gates
- CPU and Latency Profiling
- Memory, Binary Size, and Compile Time
- Optimization Workflow
- Execution Scenarios
- : a compiled Criterion benchmark target.
examples/golden-performance/
- 基准测试与回归门禁
- CPU 与延迟性能分析
- 内存、二进制大小与编译时间
- 优化工作流
- 执行场景
- :一个已编译的 Criterion benchmark target。
examples/golden-performance/
Upstream Sources
上游来源
Data Privacy
数据隐私
This skill does not collect, store, or transmit user data. Profiles, heap dumps, command lines, symbols, and benchmark datasets may contain sensitive information; confirm storage and upload policy before sharing them.
此 skill 不收集、存储或传输用户数据。性能分析结果、heap dump、命令行、符号和基准测试数据集可能包含敏感信息;共享前请确认存储和上传政策。