Shopify

Optimizing Ruby’s Memory Layout: Variable Width Allocation - Shopify

Dec 25, 2022
Optimizing Ruby’s Memory Layout: Variable Width Allocation - Shopify
SummaryAI, from the source post

This post details the introduction of Variable Width Allocation (VWA) in Ruby 3.2, a feature authored by Shopify engineers and others. VWA optimizes Ruby's memory layout by introducing dynamic sizing for objects, moving away from the fixed 40-byte RVALUE slot. This includes the introduction of 'size pools' with slot sizes as powers of two multiples of RVALUE size (40, 80, 160, 320, 640 bytes) to improve memory utilization and reduce fragmentation. The heap page size was increased from 16 KB to 64 KB to accommodate larger slot sizes and reduce page allocation overhead. Compaction was modified to move objects between optimal size pools. Four types (classes, arrays, strings, and objects) are now covered by VWA, with classes being the first fixed-size type implemented. Benchmark results show performance improvements, and the work enables cutting-edge GC research.

More from the Ruby Execution Models and Threading trail