ruby-optimise
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity 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
按优先级划分的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Object Allocation | CRITICAL | |
| 2 | Collection & Enumeration | CRITICAL | |
| 3 | I/O & Database | HIGH | |
| 4 | String Handling | HIGH | |
| 5 | Method & Dispatch | MEDIUM-HIGH | |
| 6 | Data Structures | MEDIUM | |
| 7 | Concurrency | MEDIUM | |
| 8 | Runtime & Configuration | LOW-MEDIUM | |
| 优先级 | 分类 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | 对象分配 | 关键 | |
| 2 | 集合与枚举 | 关键 | |
| 3 | I/O与数据库 | 高 | |
| 4 | 字符串处理 | 高 | |
| 5 | 方法与调度 | 中高 | |
| 6 | 数据结构 | 中 | |
| 7 | 并发 | 中 | |
| 8 | 运行时与配置 | 中低 | |
Quick Reference
快速参考
1. Object Allocation (CRITICAL)
1. 对象分配(关键)
- - Avoid Unnecessary Object Duplication
alloc-avoid-unnecessary-dup - - Freeze Constant Collections
alloc-freeze-constants - - Use Lazy Initialization for Expensive Objects
alloc-lazy-initialization - - Avoid Temporary Array Creation
alloc-avoid-temp-arrays - - Reuse Buffers in Loops
alloc-reuse-buffers - - Avoid Repeated Computation in Hot Paths
alloc-avoid-implicit-conversions
- - 避免不必要的对象复制
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. 集合与枚举(关键)
- - Use Single-Pass Collection Transforms
enum-single-pass - - Use Lazy Enumerators for Large Collections
enum-lazy-large-collections - - Use flat_map Instead of map.flatten
enum-flat-map - - Use each_with_object Over inject for Building Collections
enum-each-with-object - - Avoid Recomputing Collection Size in Conditions
enum-avoid-count-in-loops - - Use each_slice for Batch Processing
enum-chunk-batch-processing
- - 使用单遍集合转换
enum-single-pass - - 对大型集合使用延迟枚举器
enum-lazy-large-collections - - 使用flat_map替代map.flatten
enum-flat-map - - 构建集合时使用each_with_object而非inject
enum-each-with-object - - 避免在条件中重复计算集合大小
enum-avoid-count-in-loops - - 使用each_slice进行批处理
enum-chunk-batch-processing
3. I/O & Database (HIGH)
3. I/O与数据库(高)
- - Eager Load ActiveRecord Associations
io-eager-load-associations - - Select Only Needed Columns
io-select-only-needed-columns - - Use find_each for Large Record Sets
io-batch-find-each - - Avoid Database Queries Inside Loops
io-avoid-queries-in-loops - - Stream Large Files Line by Line
io-stream-large-files - - Size Connection Pools to Match Thread Count
io-connection-pool-sizing - - Cache Expensive Database Results
io-cache-expensive-queries
- - 预加载ActiveRecord关联
io-eager-load-associations - - 仅选择所需列
io-select-only-needed-columns - - 对大型记录集使用find_each
io-batch-find-each - - 避免在循环内执行数据库查询
io-avoid-queries-in-loops - - 逐行流式处理大文件
io-stream-large-files - - 根据线程数调整连接池大小
io-connection-pool-sizing - - 缓存开销大的数据库查询结果
io-cache-expensive-queries
4. String Handling (HIGH)
4. 字符串处理(高)
- - Enable Frozen String Literals
str-frozen-literals - - Use Shovel Operator for String Building
str-shovel-over-plus - - Use String Interpolation Over Concatenation
str-interpolation-over-concatenation - - Chain gsub Calls into a Single Replacement
str-avoid-repeated-gsub - - Use Symbols for Identifiers and Hash Keys
str-symbol-for-identifiers
- - 启用冻结字符串字面量
str-frozen-literals - - 使用 shovel 操作符构建字符串
str-shovel-over-plus - - 使用字符串插值替代拼接
str-interpolation-over-concatenation - - 将多个gsub调用链式合并为单次替换
str-avoid-repeated-gsub - - 对标识符和哈希键使用Symbol
str-symbol-for-identifiers
5. Method & Dispatch (MEDIUM-HIGH)
5. 方法与调度(中高)
- - Avoid method_missing in Hot Paths
meth-avoid-method-missing-hot-paths - - Cache Method References for Repeated Calls
meth-cache-method-references - - Pass Blocks Directly Instead of Converting to Proc
meth-block-vs-proc - - Avoid Dynamic send in Performance-Critical Code
meth-avoid-dynamic-send - - Reduce Method Chain Depth in Hot Loops
meth-reduce-method-chain-depth
- - 避免在热点路径中使用method_missing
meth-avoid-method-missing-hot-paths - - 缓存方法引用以用于重复调用
meth-cache-method-references - - 直接传递Block而非转换为Proc
meth-block-vs-proc - - 在性能关键代码中避免使用动态send
meth-avoid-dynamic-send - - 减少热点循环中的方法链深度
meth-reduce-method-chain-depth
6. Data Structures (MEDIUM)
6. 数据结构(中)
- - Use Set for Membership Tests
ds-set-for-membership - - Use Struct Over OpenStruct
ds-struct-over-openstruct - - Use sort_by Instead of sort with Block
ds-sort-by-over-sort - - Preallocate Arrays When Size Is Known
ds-array-preallocation - - Use Hash Default Values Instead of Conditional Assignment
ds-hash-default-value
- - 使用Set进行成员关系检测
ds-set-for-membership - - 使用Struct而非OpenStruct
ds-struct-over-openstruct - - 使用sort_by替代带Block的sort
ds-sort-by-over-sort - - 已知大小的数组预先分配空间
ds-array-preallocation - - 使用哈希默认值而非条件赋值
ds-hash-default-value
7. Concurrency (MEDIUM)
7. 并发(中)
- - Use Fibers for I/O-Bound Concurrency
conc-fiber-for-io - - Size Thread Pools to Match Workload
conc-thread-pool-sizing - - Use Ractors for CPU-Bound Parallelism
conc-ractor-cpu-bound - - Avoid Shared Mutable State Between Threads
conc-avoid-shared-mutable-state
- - 对I/O密集型并发使用Fibers
conc-fiber-for-io - - 根据工作负载调整线程池大小
conc-thread-pool-sizing - - 对CPU密集型并行任务使用Ractors
conc-ractor-cpu-bound - - 避免线程间共享可变状态
conc-avoid-shared-mutable-state
8. Runtime & Configuration (LOW-MEDIUM)
8. 运行时与配置(中低)
- - Enable YJIT for Production
runtime-enable-yjit - - Tune GC Parameters for Your Workload
runtime-tune-gc-parameters - - Set Frozen String Literal as Project Default
runtime-frozen-string-literal-default - - Optimize Require Load Order
runtime-optimize-require
- - 在生产环境启用YJIT
runtime-enable-yjit - - 根据工作负载调整GC参数
runtime-tune-gc-parameters - - 将冻结字符串字面量设为项目默认
runtime-frozen-string-literal-default - - 优化Require加载顺序
runtime-optimize-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
参考文件
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 分类定义与排序说明 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |