rust-optimise
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Optimise Best Practices
Rust性能优化最佳实践
Performance optimization guide for Rust applications. Contains 42 rules across 8 categories, prioritized by impact from critical (memory allocation, ownership) to incremental (micro-optimizations).
这是一份Rust应用的性能优化指南,包含8个类别共42条规则,按影响优先级排序,从关键优化(内存分配、所有权)到增量优化(微优化)依次排列。
When to Apply
适用场景
- Optimizing Rust code for performance or reducing allocations
- Choosing data structures for optimal algorithmic complexity
- Working with iterators to avoid unnecessary intermediate allocations
- Writing async code with Tokio or other runtimes
- Profiling hot paths and eliminating performance bottlenecks
- Reviewing code for performance anti-patterns
- 优化Rust代码性能或减少内存分配
- 选择最优数据结构以降低算法复杂度
- 使用迭代器避免不必要的中间内存分配
- 基于Tokio或其他运行时编写异步代码
- 分析热点路径并消除性能瓶颈
- 检查代码中的性能反模式
Rule Categories by Priority
按优先级排序的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Memory Allocation | CRITICAL | |
| 2 | Ownership & Borrowing | CRITICAL | |
| 3 | Data Structure Selection | HIGH | |
| 4 | Iterator & Collection Patterns | HIGH | |
| 5 | Async & Concurrency | MEDIUM-HIGH | |
| 6 | Algorithm Complexity | MEDIUM | |
| 7 | Compile-Time Optimization | MEDIUM | |
| 8 | Micro-optimizations | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 内存分配 | 关键 | |
| 2 | 所有权与借用 | 关键 | |
| 3 | 数据结构选择 | 高 | |
| 4 | 迭代器与集合模式 | 高 | |
| 5 | 异步与并发 | 中高 | |
| 6 | 算法复杂度 | 中 | |
| 7 | 编译时优化 | 中 | |
| 8 | 微优化 | 低 | |
Quick Reference
快速参考
1. Memory Allocation (CRITICAL)
1. 内存分配(关键)
- - Avoid unnecessary clone calls
mem-avoid-unnecessary-clone - - Preallocate Vec capacity
mem-preallocate-vec-capacity - - Use Cow for conditional ownership
mem-use-cow-for-conditional-ownership - - Use Arc for shared immutable data
mem-use-arc-for-shared-immutable-data - - Avoid format! for simple concatenation
mem-avoid-format-for-simple-concatenation - - Use SmallVec for small collections
mem-use-smallvec-for-small-collections
- - 避免不必要的clone调用
mem-avoid-unnecessary-clone - - 预分配Vec容量
mem-preallocate-vec-capacity - - 使用Cow实现条件性所有权
mem-use-cow-for-conditional-ownership - - 使用Arc共享不可变数据
mem-use-arc-for-shared-immutable-data - - 简单字符串拼接避免使用format!
mem-avoid-format-for-simple-concatenation - - 小型集合使用SmallVec
mem-use-smallvec-for-small-collections
2. Ownership & Borrowing (CRITICAL)
2. 所有权与借用(关键)
- - Accept &str instead of &String
own-accept-str-slice-not-string - - Accept &[T] instead of &Vec<T>
own-accept-slice-not-vec - - Use Into<T> for flexible ownership
own-use-into-for-flexible-ownership - - Return borrowed data when possible
own-return-borrowed-when-possible - - Use AsRef<T> for generic borrows
own-use-asref-for-generic-borrows
- - 参数接受&str而非&String
own-accept-str-slice-not-string - - 参数接受&[T]而非&Vec<T>
own-accept-slice-not-vec - - 使用Into<T>实现灵活的所有权转换
own-use-into-for-flexible-ownership - - 尽可能返回借用的数据
own-return-borrowed-when-possible - - 使用AsRef<T>实现通用借用
own-use-asref-for-generic-borrows
3. Data Structure Selection (HIGH)
3. 数据结构选择(高)
- - Use HashSet for membership tests
ds-use-hashset-for-membership - - Use HashMap for key-value lookups
ds-use-hashmap-for-key-lookup - - Use BTreeMap for sorted iteration
ds-use-btreemap-for-sorted-iteration - - Use VecDeque for queue operations
ds-use-vecdeque-for-queue-operations - - Use Entry API for conditional insert
ds-use-entry-api-for-conditional-insert
- - 成员测试使用HashSet
ds-use-hashset-for-membership - - 键值对查找使用HashMap
ds-use-hashmap-for-key-lookup - - 有序迭代使用BTreeMap
ds-use-btreemap-for-sorted-iteration - - 队列操作使用VecDeque
ds-use-vecdeque-for-queue-operations - - 条件插入使用Entry API
ds-use-entry-api-for-conditional-insert
4. Iterator & Collection Patterns (HIGH)
4. 迭代器与集合模式(高)
- - Chain iterators instead of intermediate collect
iter-chain-instead-of-intermediate-collect - - Use iter() over into_iter() when borrowing
iter-use-iter-over-into-iter-when-borrowing - - Use filter_map for combined operations
iter-use-filter-map-for-combined-operations - - Use flat_map for nested iteration
iter-use-flat-map-for-nested-iteration - - Use extend() for bulk append
iter-use-extend-for-bulk-append - - Use fold() for complex accumulation
iter-use-fold-for-accumulation
- - 链式调用迭代器而非中间collect
iter-chain-instead-of-intermediate-collect - - 借用场景使用iter()而非into_iter()
iter-use-iter-over-into-iter-when-borrowing - - 组合过滤与映射使用filter_map
iter-use-filter-map-for-combined-operations - - 嵌套迭代使用flat_map
iter-use-flat-map-for-nested-iteration - - 批量追加使用extend()
iter-use-extend-for-bulk-append - - 复杂累加使用fold()
iter-use-fold-for-accumulation
5. Async & Concurrency (MEDIUM-HIGH)
5. 异步与并发(中高)
- - Avoid blocking in async context
async-avoid-blocking-in-async-context - - Use join! for concurrent futures
async-use-join-for-concurrent-futures - - Use RwLock over Mutex for read-heavy
async-use-rwlock-over-mutex-for-read-heavy - - Minimize lock scope
async-minimize-lock-scope - - Use buffered() for bounded concurrency
async-use-buffered-for-bounded-concurrency - - Avoid holding lock across await
async-avoid-holding-lock-across-await
- - 异步上下文避免阻塞操作
async-avoid-blocking-in-async-context - - 并发执行Future使用join!
async-use-join-for-concurrent-futures - - 读密集场景使用RwLock而非Mutex
async-use-rwlock-over-mutex-for-read-heavy - - 最小化锁的作用域
async-minimize-lock-scope - - 有界并发使用buffered()
async-use-buffered-for-bounded-concurrency - - await期间避免持有锁
async-avoid-holding-lock-across-await
6. Algorithm Complexity (MEDIUM)
6. 算法复杂度(中)
- - Avoid nested loops for lookups
algo-avoid-nested-loops-for-lookup - - Use binary search for sorted data
algo-use-binary-search-for-sorted-data - - Use sort_unstable when order irrelevant
algo-use-sort-unstable-when-order-irrelevant - - Use select_nth_unstable for partial sort
algo-use-select-nth-unstable-for-partial-sort - - Use chunks() for batch processing
algo-use-chunks-for-batch-processing
- - 查找操作避免嵌套循环
algo-avoid-nested-loops-for-lookup - - 有序数据使用二分查找
algo-use-binary-search-for-sorted-data - - 无需稳定排序时使用sort_unstable
algo-use-sort-unstable-when-order-irrelevant - - 部分排序使用select_nth_unstable
algo-use-select-nth-unstable-for-partial-sort - - 批量处理使用chunks()
algo-use-chunks-for-batch-processing
7. Compile-Time Optimization (MEDIUM)
7. 编译时优化(中)
- - Use const for compile-time computation
comp-use-const-for-compile-time-computation - - Prefer static dispatch over dynamic
comp-prefer-static-dispatch - - Reduce monomorphization bloat
comp-reduce-monomorphization-bloat - - Use const generics for array sizes
comp-use-const-generics-for-array-sizes - - Avoid repeated parsing of static data
comp-avoid-repeated-parsing-of-static-data
- - 编译时计算使用const
comp-use-const-for-compile-time-computation - - 优先使用静态分发而非动态分发
comp-prefer-static-dispatch - - 减少单态化膨胀
comp-reduce-monomorphization-bloat - - 数组大小使用const泛型
comp-use-const-generics-for-array-sizes - - 避免重复解析静态数据
comp-avoid-repeated-parsing-of-static-data
8. Micro-optimizations (LOW)
8. 微优化(低)
- - Apply inline attribute to small hot functions
micro-use-inline-for-small-functions - - Avoid bounds checks in hot loops
micro-avoid-bounds-checks-in-hot-loops - - Use wrapping arithmetic when safe
micro-use-wrapping-arithmetic-when-safe - - Use byte literals for ASCII
micro-use-byte-literals-for-ascii
- - 热点小函数添加inline属性
micro-use-inline-for-small-functions - - 热点循环中避免边界检查
micro-avoid-bounds-checks-in-hot-loops - - 安全场景使用包装算术
micro-use-wrapping-arithmetic-when-safe - - ASCII字符使用字节字面量
micro-use-byte-literals-for-ascii
References
参考资料
Related Skills
相关技能
- For idiomatic patterns, architecture, and code organization, see skill
rust-refactor
- 如需了解惯用模式、架构与代码组织,请参考技能
rust-refactor