name: rust-optimise description: Rust性能优化涵盖内存分配、所有权效率、数据结构选择、迭代器模式、异步并发、算法复杂度、编译时优化和微优化。适用于优化Rust代码性能、分析热点路径、减少分配或选择最优数据结构时使用。补充rust-refactor技能(惯用模式和架构)。不涵盖代码风格、命名约定或项目组织(参见rust-refactor技能)。
Rust性能优化最佳实践
Rust应用程序的性能优化指南。包含8个类别的42条规则,按影响优先级从关键(内存分配、所有权)到增量(微优化)排序。
何时应用
- 优化Rust代码性能或减少分配
- 选择数据结构以实现最优算法复杂度
- 使用迭代器以避免不必要的中间分配
- 使用Tokio或其他运行时编写异步代码
- 分析热点路径并消除性能瓶颈
- 审查代码中的性能反模式
规则类别按优先级
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 内存分配 | 关键 | mem- |
| 2 | 所有权与借用 | 关键 | own- |
| 3 | 数据结构选择 | 高 | ds- |
| 4 | 迭代器与集合模式 | 高 | iter- |
| 5 | 异步与并发 | 中高 | async- |
| 6 | 算法复杂度 | 中 | algo- |
| 7 | 编译时优化 | 中 | comp- |
| 8 | 微优化 | 低 | micro- |
快速参考
1. 内存分配(关键)
mem-avoid-unnecessary-clone- 避免不必要的克隆调用mem-preallocate-vec-capacity- 预分配Vec容量mem-use-cow-for-conditional-ownership- 使用Cow进行条件所有权mem-use-arc-for-shared-immutable-data- 使用Arc共享不可变数据mem-avoid-format-for-simple-concatenation- 避免使用format!进行简单拼接mem-use-smallvec-for-small-collections- 使用SmallVec处理小集合
2. 所有权与借用(关键)
own-accept-str-slice-not-string- 接受&str而非&Stringown-accept-slice-not-vec- 接受&[T]而非&Vec<T>own-use-into-for-flexible-ownership- 使用Into<T>实现灵活所有权own-return-borrowed-when-possible- 尽可能返回借用数据own-use-asref-for-generic-borrows- 使用AsRef<T>进行通用借用
3. 数据结构选择(高)
ds-use-hashset-for-membership- 使用HashSet进行成员测试ds-use-hashmap-for-key-lookup- 使用HashMap进行键值查找ds-use-btreemap-for-sorted-iteration- 使用BTreeMap进行有序迭代ds-use-vecdeque-for-queue-operations- 使用VecDeque进行队列操作ds-use-entry-api-for-conditional-insert- 使用Entry API进行条件插入
4. 迭代器与集合模式(高)
iter-chain-instead-of-intermediate-collect- 链式迭代器而非中间收集iter-use-iter-over-into-iter-when-borrowing- 借用时使用iter()而非into_iter()iter-use-filter-map-for-combined-operations- 使用filter_map进行组合操作iter-use-flat-map-for-nested-iteration- 使用flat_map进行嵌套迭代iter-use-extend-for-bulk-append- 使用extend()进行批量追加iter-use-fold-for-accumulation- 使用fold()进行复杂累加
5. 异步与并发(中高)
async-avoid-blocking-in-async-context- 避免在异步上下文中阻塞async-use-join-for-concurrent-futures- 使用join!处理并发futureasync-use-rwlock-over-mutex-for-read-heavy- 读多场景使用RwLock而非Mutexasync-minimize-lock-scope- 最小化锁范围async-use-buffered-for-bounded-concurrency- 使用buffered()进行有界并发async-avoid-holding-lock-across-await- 避免跨await持有锁
6. 算法复杂度(中)
algo-avoid-nested-loops-for-lookup- 避免嵌套循环进行查找algo-use-binary-search-for-sorted-data- 对排序数据使用二分搜索algo-use-sort-unstable-when-order-irrelevant- 顺序无关时使用sort_unstablealgo-use-select-nth-unstable-for-partial-sort- 使用select_nth_unstable进行部分排序algo-use-chunks-for-batch-processing- 使用chunks()进行批处理
7. 编译时优化(中)
comp-use-const-for-compile-time-computation- 使用const进行编译时计算comp-prefer-static-dispatch- 优先静态分发而非动态comp-reduce-monomorphization-bloat- 减少单态化膨胀comp-use-const-generics-for-array-sizes- 使用常量泛型处理数组大小comp-avoid-repeated-parsing-of-static-data- 避免重复解析静态数据
8. 微优化(低)
micro-use-inline-for-small-functions- 对小热函数应用inline属性micro-avoid-bounds-checks-in-hot-loops- 避免在热循环中进行边界检查micro-use-wrapping-arithmetic-when-safe- 安全时使用包裹算术micro-use-byte-literals-for-ascii- 对ASCII使用字节字面量
参考文献
- https://nnethercote.github.io/perf-book/
- https://rust-lang.github.io/api-guidelines/
- https://doc.rust-lang.org/nomicon/
- https://tokio.rs/tokio/tutorial
相关技能
- 对于惯用模式、架构和代码组织,请参见
rust-refactor技能