1 hour ago · Tech · hide · 0 comments

Originally published on Rails At Scale. 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. That didn’t sound right to me because I thought that: Rails got a lot better in recent years about removing these dynamic lookups in favor of creating and caching methods ZJIT should be able to handle dispatch to methods (generated or otherwise) just fine So I inquired and Sam presented to us the implementation of enum which contained this gem: value =…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.