关于 rust-optimise
面向 Rust 开发者的性能优化指南,按影响优先级整理 8 类规则,涵盖内存、借用、异步和算法等实践。适用于优化、审查及编写 Rust 代码。
主要功能
- 01涵盖 8 类优化主题
- 02按影响优先级排序规则
- 03减少内存分配与克隆
- 04指导异步与并发实践
- 05包含算法和迭代器建议
适用场景
- 01排查 Rust 性能瓶颈
- 02减少代码中的不必要分配
- 03选择适合的数据结构
- 04审查异步代码中的性能问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pproenca/ dot-skills --skill rust-optimise安装后在 Claude Code 中输入/rust-optimise确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
AGENTS.mdREADME.mdmetadata.jsonassets/templates/_template.mdreferences/_sections.mdalgo-avoid-nested-loops-for-lookup.mdalgo-use-binary-search-for-sorted-data.mdalgo-use-chunks-for-batch-processing.mdalgo-use-select-nth-unstable-for-partial-sort.mdalgo-use-sort-unstable-when-order-irrelevant.mdasync-avoid-blocking-in-async-context.mdasync-avoid-holding-lock-across-await.mdasync-minimize-lock-scope.mdasync-use-buffered-for-bounded-concurrency.mdasync-use-join-for-concurrent-futures.mdasync-use-rwlock-over-mutex-for-read-heavy.mdcomp-avoid-repeated-parsing-of-static-data.mdcomp-prefer-static-dispatch.mdcomp-reduce-monomorphization-bloat.mdcomp-use-const-for-compile-time-computation.mdcomp-use-const-generics-for-array-sizes.mdds-use-btreemap-for-sorted-iteration.mdds-use-entry-api-for-conditional-insert.mdds-use-hashmap-for-key-lookup.mdds-use-hashset-for-membership.mdds-use-vecdeque-for-queue-operations.mditer-chain-instead-of-intermediate-collect.mditer-use-extend-for-bulk-append.mditer-use-filter-map-for-combined-operations.mditer-use-flat-map-for-nested-iteration.md详细介绍
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- 避免不必要的 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] 而不是 &Vecown-use-into-for-flexible-ownership- 使用 Into 实现灵活的所有权处理own-return-borrowed-when-possible- 尽可能返回借用数据own-use-asref-for-generic-borrows- 使用 AsRef 实现泛型借用
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- 使用 const 泛型指定数组大小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技能
评论
支持 Markdown