0.06 0.11 0.72 0.05 0.11 0.36 0.13 0.29 1.1 JRuby(oj) JRuby(json) MRI(oj) Million of loads per second (higher is better) Data from https://techblog.thescore.com/2014/05/23/benchmarking-json-generation-in-ruby/
2006 • Almost as long as Rails has existed! • Thousands of JRoR instances around the world • JRuby 9000, Ruby 2.4, 2.5 work slowed down Rails support • Rails 5.0 not supported for at least a year • ActiveRecord suffered the most
Practically useless • Like judging a person by how much they can bench press • JRuby has won microbenchmarks for years • Easier to isolate speciﬁc measurements • Great for exploring new runtimes and tech
JVM see through all the dynamic bits of Ruby • Added in Java 7, with much input and testing from JRuby • Steadily improving performance, reducing overhead • -Xcompile.invokedynamic • May be default soon!
allocated on assignment • Any unfrozen object can grow • Looks like a Hash • Inefﬁcient for mostly-same keys • Array reduces cost, still high • Make them JVM ﬁelds! class Person # closest we get to a declaration attr_accessor :fname, :lname, :bdate def initialize(fname, lname, bdate) # encountered after first object # has already been constructed @fname, @lname, @bdate = fname, lname, bdate end def initialize_id @id ||= SecureRandom.uuid end end
Arrays are small + immutable or large + mutable • Large, mutable arrays will often continue to mutate • Manually optimized 1- and 2-element arrays using ﬁelds • Future: hook into Object Shaping for Array
calculate_cost(c) total1 = c.add 1000, 1 total2 = c.add 2000, 2 total1 + total2 end def calculate_cost(c) total1 = 1000 + 1 total2 = 2000 + 2 total1 + total2 end Disclaimer: Ruby much easier to read than IR * c.add must always be call to same method on same type == monomorphic call