Rust Optimise Best Practices Performance optimization guide for Rust applications. Contains 42 rules across 8 categories, prioritized by impact from critical (memory allocation, ownership) to incremental (micro-optimizations). 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 Rule Categories by Priority Priority Category Impact Prefix 1 Memory Allocation CRITICAL mem- 2 Ownership & Borrowing CRITICAL own- 3 Data Structure Selection HIGH ds- 4 Iterator & Collection Patterns HIGH iter- 5 Async & Concurrency MEDIUM-HIGH async- 6 Algorithm Complexity MEDIUM algo- 7 Compile-Time Optimization MEDIUM comp- 8 Micro-optimizations LOW micro- Quick Reference 1. Memory Allocation (CRITICAL) mem-avoid-unnecessary-clone - Avoid unnecessary clone calls mem-preallocate-vec-capacity - Preallocate Vec capacity mem-use-cow-for-conditional-ownership - Use Cow for conditional ownership mem-use-arc-for-shared-immutable-data - Use Arc for shared immutable data mem-avoid-format-for-simple-concatenation - Avoid format! for simple concatenation mem-use-smallvec-for-small-collections - Use SmallVec for small collections 2. Ownership & Borrowing (CRITICAL) own-accept-str-slice-not-string - Accept &str instead of &String own-accept-slice-not-vec - Accept &[T] instead of &Vec own-use-into-for-flexible-ownership - Use Into for flexible ownership own-return-borrowed-when-possible - Return borrowed data when possible own-use-asref-for-generic-borrows - Use AsRef for generic borrows 3. Data Structure Selection (HIGH) ds-use-hashset-for-membership - Use HashSet for membership tests ds-use-hashmap-for-key-lookup - Use HashMap for key-value lookups ds-use-btreemap-for-sorted-iteration - Use BTreeMap for sorted iteration ds-use-vecdeque-for-queue-operations - Use VecDeque for queue operations ds-use-entry-api-for-conditional-insert - Use Entry API for conditional insert 4. Iterator & Collection Patterns (HIGH) iter-chain-instead-of-intermediate-collect - Chain iterators instead of intermediate collect iter-use-iter-over-into-iter-when-borrowing - Use iter() over into_iter() when borrowing iter-use-filter-map-for-combined-operations - Use filter_map for combined operations iter-use-flat-map-for-nested-iteration - Use flat_map for nested iteration iter-use-extend-for-bulk-append - Use extend() for bulk append iter-use-fold-for-accumulation - Use fold() for complex accumulation 5. Async & Concurrency (MEDIUM-HIGH) async-avoid-blocking-in-async-context - Avoid blocking in async context async-use-join-for-concurrent-futures - Use join! for concurrent futures async-use-rwlock-over-mutex-for-read-heavy - Use RwLock over Mutex for read-heavy async-minimize-lock-scope - Minimize lock scope async-use-buffered-for-bounded-concurrency - Use buffered() for bounded concurrency async-avoid-holding-lock-across-await - Avoid holding lock across await 6. Algorithm Complexity (MEDIUM) algo-avoid-nested-loops-for-lookup - Avoid nested loops for lookups algo-use-binary-search-for-sorted-data - Use binary search for sorted data algo-use-sort-unstable-when-order-irrelevant - Use sort_unstable when order irrelevant algo-use-select-nth-unstable-for-partial-sort - Use select_nth_unstable for partial sort algo-use-chunks-for-batch-processing - Use chunks() for batch processing 7. Compile-Time Optimization (MEDIUM) comp-use-const-for-compile-time-computation - Use const for compile-time computation comp-prefer-static-dispatch - Prefer static dispatch over dynamic comp-reduce-monomorphization-bloat - Reduce monomorphization bloat comp-use-const-generics-for-array-sizes - Use const generics for array sizes comp-avoid-repeated-parsing-of-static-data - Avoid repeated parsing of static data 8. Micro-optimizations (LOW) micro-use-inline-for-small-functions - Apply inline attribute to small hot functions micro-avoid-bounds-checks-in-hot-loops - Avoid bounds checks in hot loops micro-use-wrapping-arithmetic-when-safe - Use wrapping arithmetic when safe micro-use-byte-literals-for-ascii - Use byte literals for ASCII References 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-optimise
安装
npx skills add https://github.com/pproenca/dot-skills --skill rust-optimise