Rails is a JIT compiler

Or, you wouldn’t believe how Aaron made enums faster with one weird trick.

The title was bait to get you to open this article. But fear not! We will be generating code at run-time today. (See the PR for a sneak peek.)

Sam popped into the JIT channel asking how we might optimize Rails enums in Roundhouse, motivated by the following code snippet:

class Order < ActiveRecord::Base
  enum :status, { pending: 0, paid: 1, shipped: 2, refunded: 3 }

  def shippable?
    paid? && !refunded?
  end
end

In particular, there is a lot of dynamic behavior going on with the code generated by Rails enum and Sam wanted to know if inlining it all away in an (external-to-Rails) build step would help.