ruby-optimise

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Community Ruby Best Practices

社区Ruby最佳实践

Comprehensive performance optimization guide for Ruby applications, maintained by the community. Contains 42 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
由社区维护的Ruby应用性能优化综合指南,包含8个分类下的42条规则,按影响优先级排序,可指导自动化重构和代码生成。

When to Apply

适用场景

Reference these guidelines when:
  • Writing new Ruby code or gems
  • Optimizing ActiveRecord queries and database access patterns
  • Processing large collections or building data pipelines
  • Reviewing code for memory bloat and GC pressure
  • Configuring Ruby runtime settings for production
在以下场景中参考本指南:
  • 编写新的Ruby代码或gem
  • 优化ActiveRecord查询和数据库访问模式
  • 处理大型集合或构建数据管道
  • 审查代码以排查内存膨胀和GC压力问题
  • 配置生产环境的Ruby运行时设置

Rule Categories by Priority

按优先级划分的规则分类

PriorityCategoryImpactPrefix
1Object AllocationCRITICAL
alloc-
2Collection & EnumerationCRITICAL
enum-
3I/O & DatabaseHIGH
io-
4String HandlingHIGH
str-
5Method & DispatchMEDIUM-HIGH
meth-
6Data StructuresMEDIUM
ds-
7ConcurrencyMEDIUM
conc-
8Runtime & ConfigurationLOW-MEDIUM
runtime-
优先级分类影响级别前缀
1对象分配关键
alloc-
2集合与枚举关键
enum-
3I/O与数据库
io-
4字符串处理
str-
5方法与调度中高
meth-
6数据结构
ds-
7并发
conc-
8运行时与配置中低
runtime-

Quick Reference

快速参考

1. Object Allocation (CRITICAL)

1. 对象分配(关键)

  • alloc-avoid-unnecessary-dup
    - Avoid Unnecessary Object Duplication
  • alloc-freeze-constants
    - Freeze Constant Collections
  • alloc-lazy-initialization
    - Use Lazy Initialization for Expensive Objects
  • alloc-avoid-temp-arrays
    - Avoid Temporary Array Creation
  • alloc-reuse-buffers
    - Reuse Buffers in Loops
  • alloc-avoid-implicit-conversions
    - Avoid Repeated Computation in Hot Paths
  • alloc-avoid-unnecessary-dup
    - 避免不必要的对象复制
  • alloc-freeze-constants
    - 冻结常量集合
  • alloc-lazy-initialization
    - 对开销大的对象使用延迟初始化
  • alloc-avoid-temp-arrays
    - 避免创建临时数组
  • alloc-reuse-buffers
    - 在循环中复用缓冲区
  • alloc-avoid-implicit-conversions
    - 避免在热点路径中重复计算

2. Collection & Enumeration (CRITICAL)

2. 集合与枚举(关键)

  • enum-single-pass
    - Use Single-Pass Collection Transforms
  • enum-lazy-large-collections
    - Use Lazy Enumerators for Large Collections
  • enum-flat-map
    - Use flat_map Instead of map.flatten
  • enum-each-with-object
    - Use each_with_object Over inject for Building Collections
  • enum-avoid-count-in-loops
    - Avoid Recomputing Collection Size in Conditions
  • enum-chunk-batch-processing
    - Use each_slice for Batch Processing
  • enum-single-pass
    - 使用单遍集合转换
  • enum-lazy-large-collections
    - 对大型集合使用延迟枚举器
  • enum-flat-map
    - 使用flat_map替代map.flatten
  • enum-each-with-object
    - 构建集合时使用each_with_object而非inject
  • enum-avoid-count-in-loops
    - 避免在条件中重复计算集合大小
  • enum-chunk-batch-processing
    - 使用each_slice进行批处理

3. I/O & Database (HIGH)

3. I/O与数据库(高)

  • io-eager-load-associations
    - Eager Load ActiveRecord Associations
  • io-select-only-needed-columns
    - Select Only Needed Columns
  • io-batch-find-each
    - Use find_each for Large Record Sets
  • io-avoid-queries-in-loops
    - Avoid Database Queries Inside Loops
  • io-stream-large-files
    - Stream Large Files Line by Line
  • io-connection-pool-sizing
    - Size Connection Pools to Match Thread Count
  • io-cache-expensive-queries
    - Cache Expensive Database Results
  • io-eager-load-associations
    - 预加载ActiveRecord关联
  • io-select-only-needed-columns
    - 仅选择所需列
  • io-batch-find-each
    - 对大型记录集使用find_each
  • io-avoid-queries-in-loops
    - 避免在循环内执行数据库查询
  • io-stream-large-files
    - 逐行流式处理大文件
  • io-connection-pool-sizing
    - 根据线程数调整连接池大小
  • io-cache-expensive-queries
    - 缓存开销大的数据库查询结果

4. String Handling (HIGH)

4. 字符串处理(高)

  • str-frozen-literals
    - Enable Frozen String Literals
  • str-shovel-over-plus
    - Use Shovel Operator for String Building
  • str-interpolation-over-concatenation
    - Use String Interpolation Over Concatenation
  • str-avoid-repeated-gsub
    - Chain gsub Calls into a Single Replacement
  • str-symbol-for-identifiers
    - Use Symbols for Identifiers and Hash Keys
  • str-frozen-literals
    - 启用冻结字符串字面量
  • str-shovel-over-plus
    - 使用 shovel 操作符构建字符串
  • str-interpolation-over-concatenation
    - 使用字符串插值替代拼接
  • str-avoid-repeated-gsub
    - 将多个gsub调用链式合并为单次替换
  • str-symbol-for-identifiers
    - 对标识符和哈希键使用Symbol

5. Method & Dispatch (MEDIUM-HIGH)

5. 方法与调度(中高)

  • meth-avoid-method-missing-hot-paths
    - Avoid method_missing in Hot Paths
  • meth-cache-method-references
    - Cache Method References for Repeated Calls
  • meth-block-vs-proc
    - Pass Blocks Directly Instead of Converting to Proc
  • meth-avoid-dynamic-send
    - Avoid Dynamic send in Performance-Critical Code
  • meth-reduce-method-chain-depth
    - Reduce Method Chain Depth in Hot Loops
  • meth-avoid-method-missing-hot-paths
    - 避免在热点路径中使用method_missing
  • meth-cache-method-references
    - 缓存方法引用以用于重复调用
  • meth-block-vs-proc
    - 直接传递Block而非转换为Proc
  • meth-avoid-dynamic-send
    - 在性能关键代码中避免使用动态send
  • meth-reduce-method-chain-depth
    - 减少热点循环中的方法链深度

6. Data Structures (MEDIUM)

6. 数据结构(中)

  • ds-set-for-membership
    - Use Set for Membership Tests
  • ds-struct-over-openstruct
    - Use Struct Over OpenStruct
  • ds-sort-by-over-sort
    - Use sort_by Instead of sort with Block
  • ds-array-preallocation
    - Preallocate Arrays When Size Is Known
  • ds-hash-default-value
    - Use Hash Default Values Instead of Conditional Assignment
  • ds-set-for-membership
    - 使用Set进行成员关系检测
  • ds-struct-over-openstruct
    - 使用Struct而非OpenStruct
  • ds-sort-by-over-sort
    - 使用sort_by替代带Block的sort
  • ds-array-preallocation
    - 已知大小的数组预先分配空间
  • ds-hash-default-value
    - 使用哈希默认值而非条件赋值

7. Concurrency (MEDIUM)

7. 并发(中)

  • conc-fiber-for-io
    - Use Fibers for I/O-Bound Concurrency
  • conc-thread-pool-sizing
    - Size Thread Pools to Match Workload
  • conc-ractor-cpu-bound
    - Use Ractors for CPU-Bound Parallelism
  • conc-avoid-shared-mutable-state
    - Avoid Shared Mutable State Between Threads
  • conc-fiber-for-io
    - 对I/O密集型并发使用Fibers
  • conc-thread-pool-sizing
    - 根据工作负载调整线程池大小
  • conc-ractor-cpu-bound
    - 对CPU密集型并行任务使用Ractors
  • conc-avoid-shared-mutable-state
    - 避免线程间共享可变状态

8. Runtime & Configuration (LOW-MEDIUM)

8. 运行时与配置(中低)

  • runtime-enable-yjit
    - Enable YJIT for Production
  • runtime-tune-gc-parameters
    - Tune GC Parameters for Your Workload
  • runtime-frozen-string-literal-default
    - Set Frozen String Literal as Project Default
  • runtime-optimize-require
    - Optimize Require Load Order
  • runtime-enable-yjit
    - 在生产环境启用YJIT
  • runtime-tune-gc-parameters
    - 根据工作负载调整GC参数
  • runtime-frozen-string-literal-default
    - 将冻结字符串字面量设为项目默认
  • runtime-optimize-require
    - 优化Require加载顺序

How to Use

使用方法

Read individual reference files for detailed explanations and code examples:
  • Section definitions - Category structure and impact levels
  • Rule template - Template for adding new rules
阅读单个参考文件以获取详细说明和代码示例:
  • 分类定义 - 分类结构和影响级别说明
  • 规则模板 - 添加新规则的模板

Reference Files

参考文件

FileDescription
references/_sections.mdCategory definitions and ordering
assets/templates/_template.mdTemplate for new rules
metadata.jsonVersion and reference information
文件描述
references/_sections.md分类定义与排序说明
assets/templates/_template.md新规则模板
metadata.json版本与参考信息