From Rails at Scale · Max Bernstein · · 1m
Rails is a JIT compiler
Rails enums can be optimized with JIT compilation for faster code execution.
In brief
Rails enums can be optimized with JIT compilation for faster code execution. Rails enums improve performance when compiled with JIT by reducing dynamic method lookups. Originally reported by Rails at Scale.
The title was bait
The article's title was designed to attract readers but promises run-time code generation.
“See the PR for a sneak peek”
Sam's optimization question
Sam asked how to optimize Rails enums in Roundhouse using a specific code snippet for order status checks.
Rails enum implementation

Rails generates methods for enum values using public_send to compare database values.
Dynamic behavior issue
The generated methods use public_send causing slow method dispatch without caching.
Nerd sniping Aaron
The author asked Aaron to fix the enum implementation using module_eval for static method generation.
Performance improvements
The fix increased shippable? performance from 1.72M i/s to 18.5M i/s with JIT compilation.
Key lessons
Static method generation improves performance by avoiding dynamic lookups and public_send calls.
At a glance
Rails enums improve performance when compiled with JIT by reducing dynamic method lookups.





