Fast Allocations in Ruby 3.5

https://news.ycombinator.com/rss Hits: 6
Summary

Many Ruby applications allocate objects. What if we could make allocating objects six times faster? We can! Read on to learn more! Speeding up allocations in Ruby Object allocation in Ruby 3.5 will be much faster than previous versions of Ruby. I want to start this article with benchmarks and graphs, but if you stick around I’ll also be explaining how we achieved this speedup. For allocation benchmarks, we’ll compare types of parameters (positional and keyword) with and without YJIT enabled. We’ll also vary the number of parameters we pass to initialize so that we can see how performance changes as the number of parameters increases. The full benchmark code can be found expanded below, but it’s basically as follows: class Foo # Measure performance as parameters increase def initialize(a1, a2, aN) end end def test i = 0 while i < 5_000_000 Foo.new(1, 2, N) Foo.new(1, 2, N) Foo.new(1, 2, N) Foo.new(1, 2, N) Foo.new(1, 2, N) i += 1 end end test Full Benchmark Code Positional parameters benchmark: N = (ARGV[0] || 0).to_i class Foo class_eval <<-eorb def initialize(#{N.times.map { "a#{_1}" }.join(", ") }) end eorb end eval <<-eorb def test i = 0 while i < 5_000_000 Foo.new(#{N.times.map { _1.to_s }.join(", ") }) Foo.new(#{N.times.map { _1.to_s }.join(", ") }) Foo.new(#{N.times.map { _1.to_s }.join(", ") }) Foo.new(#{N.times.map { _1.to_s }.join(", ") }) Foo.new(#{N.times.map { _1.to_s }.join(", ") }) i += 1 end end eorb test Keyword parameters benchmark: N = (ARGV[0] || 0).to_i class Foo class_eval <<-eorb def initialize(#{N.times.map { "a#{_1}:" }.join(", ") }) end eorb end eval <<-eorb def test i = 0 while i < 5_000_000 Foo.new(#{N.times.map { "a#{_1}: #{_1}" }.join(", ") }) Foo.new(#{N.times.map { "a#{_1}: #{_1}" }.join(", ") }) Foo.new(#{N.times.map { "a#{_1}: #{_1}" }.join(", ") }) Foo.new(#{N.times.map { "a#{_1}: #{_1}" }.join(", ") }) Foo.new(#{N.times.map { "a#{_1}: #{_1}" }.join(", ") }) i += 1 end end eorb test We want to measure how long this script will take,...

First seen: 2025-05-22 14:25

Last seen: 2025-05-22 19:26