关于 ruby-optimise
面向 Ruby 应用和 gem 开发者的社区维护性能指南,包含 42 条规则,分为八类并按影响排序,涵盖对象分配至运行时配置。
主要功能
- 01八类共 42 条规则
- 02按性能影响排序
- 03涵盖分配与集合处理
- 04提供 I/O 和数据库建议
- 05并发与运行时调优
适用场景
- 01审查 Ruby 代码的性能问题
- 02优化 ActiveRecord 查询
- 03分批处理大型集合
- 04减少内存占用和 GC 压力
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pproenca/ dot-skills --skill ruby-optimise安装后在 Claude Code 中输入/ruby-optimise确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
AGENTS.mdmetadata.jsonREADME.mdassets/templates/_template.mdreferences/_sections.mdalloc-avoid-implicit-conversions.mdalloc-avoid-temp-arrays.mdalloc-avoid-unnecessary-dup.mdalloc-freeze-constants.mdalloc-lazy-initialization.mdalloc-reuse-buffers.mdconc-avoid-shared-mutable-state.mdconc-fiber-for-io.mdconc-ractor-cpu-bound.mdconc-thread-pool-sizing.mdds-array-preallocation.mdds-hash-default-value.mdds-set-for-membership.mdds-sort-by-over-sort.mdds-struct-over-openstruct.mdenum-avoid-count-in-loops.mdenum-chunk-batch-processing.mdenum-each-with-object.mdenum-flat-map.mdenum-lazy-large-collections.mdenum-single-pass.mdio-avoid-queries-in-loops.mdio-batch-find-each.mdio-cache-expensive-queries.mdio-connection-pool-sizing.md详细介绍
Ruby 社区最佳实践
Ruby 应用的全面性能优化指南,由社区维护。包含 8 个类别中的 42 条规则,按影响程度排序,为自动重构和代码生成提供指导。
适用场景
在以下情况下参考这些指南:
- 编写新的 Ruby 代码或 gem
- 优化 ActiveRecord 查询和数据库访问模式
- 处理大型集合或构建数据管道
- 审查代码中的内存膨胀和 GC 压力
- 配置生产环境的 Ruby 运行时设置
按优先级划分的规则类别
| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 对象分配 | CRITICAL | alloc- |
| 2 | 集合与枚举 | CRITICAL | enum- |
| 3 | I/O 与数据库 | HIGH | io- |
| 4 | 字符串处理 | HIGH | str- |
| 5 | 方法与分派 | MEDIUM-HIGH | meth- |
| 6 | 数据结构 | MEDIUM | ds- |
| 7 | 并发 | MEDIUM | conc- |
| 8 | 运行时与配置 | LOW-MEDIUM | runtime- |
快速参考
1. 对象分配(CRITICAL)
alloc-avoid-unnecessary-dup- 避免不必要的对象复制alloc-freeze-constants- 冻结常量集合alloc-lazy-initialization- 对开销较大的对象使用延迟初始化alloc-avoid-temp-arrays- 避免创建临时数组alloc-reuse-buffers- 在循环中复用缓冲区alloc-avoid-implicit-conversions- 避免在热点路径中重复计算
2. 集合与枚举(CRITICAL)
enum-single-pass- 使用单次遍历完成集合转换enum-lazy-large-collections- 对大型集合使用惰性枚举器enum-flat-map- 使用 flat_map 替代 map.flattenenum-each-with-object- 构建集合时使用 each_with_object 而非 injectenum-avoid-count-in-loops- 避免在循环条件中重复计算集合大小enum-chunk-batch-processing- 使用 each_slice 进行批处理
3. I/O 与数据库(HIGH)
io-eager-load-associations- 预加载 ActiveRecord 关联io-select-only-needed-columns- 仅选择需要的列io-batch-find-each- 对大型记录集使用 find_eachio-avoid-queries-in-loops- 避免在循环中执行数据库查询io-stream-large-files- 逐行流式处理大型文件io-connection-pool-sizing- 根据线程数设置连接池大小io-cache-expensive-queries- 缓存开销较大的数据库查询结果
4. 字符串处理(HIGH)
str-frozen-literals- 启用冻结字符串字面量str-shovel-over-plus- 构建字符串时使用 shovel 运算符str-interpolation-over-concatenation- 使用字符串插值而非拼接str-avoid-repeated-gsub- 将多个 gsub 调用合并为一次替换str-symbol-for-identifiers- 将符号用于标识符和哈希键
5. 方法与分派(中高)
meth-avoid-method-missing-hot-paths- 避免在热点路径中使用 method_missingmeth-cache-method-references- 缓存方法引用以供重复调用meth-block-vs-proc- 直接传递块,而不是将其转换为 Procmeth-avoid-dynamic-send- 避免在性能关键代码中使用动态 sendmeth-reduce-method-chain-depth- 减少热点循环中的方法链深度
6. 数据结构(中)
ds-set-for-membership- 使用 Set 进行成员资格测试ds-struct-over-openstruct- 使用 Struct 替代 OpenStructds-sort-by-over-sort- 使用 sort_by,而不是带块的 sortds-array-preallocation- 已知大小时预分配数组ds-hash-default-value- 使用 Hash 默认值,而不是条件赋值
7. 并发(中)
conc-fiber-for-io- 使用 Fiber 实现 I/O 密集型并发conc-thread-pool-sizing- 根据工作负载设置线程池大小conc-ractor-cpu-bound- 使用 Ractor 实现 CPU 密集型并行处理conc-avoid-shared-mutable-state- 避免线程间共享可变状态
8. 运行时与配置(低中)
runtime-enable-yjit- 为生产环境启用 YJITruntime-tune-gc-parameters- 根据工作负载调整 GC 参数runtime-frozen-string-literal-default- 将冻结字符串字面量设为项目默认值runtime-optimize-require- 优化 require 加载顺序
使用方法
阅读各个参考文件,了解详细说明和代码示例:
参考文件
| 文件 | 说明 |
|---|---|
| references/_sections.md | 类别定义和排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本和参考信息 |
评论
支持 Markdown