Fast Allocations in Ruby 3.5
Published on: 2025-06-27 00:01:55
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
... Read full article.