---
title: "Optimizing Ruby’s Memory Layout: Variable Width Allocation"
description: "Ruby 3.2's Variable Width Allocation replaces CRuby's single 40-byte RVALUE slot with five size pools (40, 80, 160, 320 and 640 bytes) so classes, strings, arrays and objects can store their data adjacent to the object instead of in separate malloc'd memory."
url: "https://swe.quest/articles/optimizing-ruby-s-memory-layout-variable-width-allocation-7db49cee151f4d44"
canonical: "https://shopify.engineering/ruby-variable-width-allocation"
source: "Shopify Engineering"
published: "2022-12-25"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 13
tags: ["memory-management","ruby","runtimes","performance"]
---

# Optimizing Ruby’s Memory Layout: Variable Width Allocation

A post on [Shopify Engineering](https://swe.quest/sources/shopify.md), published 2022-12-25, 13 min read, Deep dive. Original: [https://shopify.engineering/ruby-variable-width-allocation](https://shopify.engineering/ruby-variable-width-allocation)

## Summary

Ruby 3.2's Variable Width Allocation replaces CRuby's single 40-byte RVALUE slot with five size pools (40, 80, 160, 320 and 640 bytes) so classes, strings, arrays and objects can store their data adjacent to the object instead of in separate malloc'd memory. Heap pages grew from 16 KB to 64 KB, and compaction moves resized objects into the best-fitting pool to restore data locality. Embedded strings now go up to 615 bytes and arrays and objects up to 78 elements or instance variables. Microbenchmarks show up to 35% speedups and railsbench gains 2.1%. The change also adds GC.stat\_heap, offers a build flag to disable the feature, and opens the door to MMTk-based garbage collector research.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Memory management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)
- [Ruby](https://swe.quest/tags/ruby.md) (Languages)
- [Runtimes & VMs](https://swe.quest/tags/runtimes.md) (Compilers & runtimes)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)

## Prerequisites

- garbage collection basics ([easy posts on Memory management](https://swe.quest/tags/memory-management.md?difficulty=easy))
- CPU cache lines ([easy posts on Hardware](https://swe.quest/tags/hardware.md?difficulty=easy))
- malloc and heap allocation

## Keywords

Variable Width Allocation, CRuby, Ruby 3.2, RVALUE, MMTk, GC.stat\_heap

## Related posts

- [Year in Review 2022: Tenderlove's Ruby and Rails Reflections and Predictions](https://swe.quest/articles/year-in-review-2022-tenderlove-s-ruby-and-rails-reflections-and-predictions-d873d71ac3e91545.md): Shopify Engineering · 2022-12-22 · Easy · 8 min read · Ruby, Runtimes & VMs, Memory management, Developer tools, Compilers
  Ruby's 2022 highlights include YJIT's rewrite in Rust and its new ARM support, so \`ruby --yjit\` now runs on Apple Silicon development machines as well as production.
- [Adventures in Garbage Collection: Improving GC Performance in our Massive Monolith](https://swe.quest/articles/adventures-in-garbage-collection-improving-gc-performance-in-our-massive-7c8cc3e83e915743.md): Shopify Engineering · 2023-04-06 · Hard · 13 min read · Memory management, Ruby, Performance engineering, Runtimes & VMs
  Shopify cut Ruby garbage-collection tail latency in its monolith by finding why major GCs were triggered, via GC.latest\_gc\_info(:major\_by), and removing each cause in turn.
- [The effect of switching to TCMalloc on RocksDB memory use](https://swe.quest/articles/the-effect-of-switching-to-tcmalloc-on-rocksdb-memory-use-97e46a9ddc75be5e.md): Cloudflare · 2021-02-03 · Medium · 8 min read · Memory management, Performance engineering, Linux, Storage engines
  Quicksilver's memory roughly doubled after its storage moved to RocksDB, climbing from about 15GB to 30GB over several days.
- [Code Ranges: A Deeper Look at Ruby Strings](https://swe.quest/articles/code-ranges-a-deeper-look-at-ruby-strings-b87e06d5cb44fa84.md): Shopify Engineering · 2022-04-14 · Hard · 14 min read · Ruby, Runtimes & VMs, Data structures, Performance engineering
  Ruby strings are mutable and encoding-aware across 100+ encodings, so runtimes cache a per-string "code range" (UNKNOWN, 7BIT, VALID, BROKEN) to avoid repeated O(n) validity scans.
- [A Database Without Dynamic Memory Allocation](https://swe.quest/articles/a-database-without-dynamic-memory-allocation-4be7684fa723cb5d.md): TigerBeetle · 2022-10-12 · Medium · 6 min read · Memory management, Zig, Databases, Storage engines, Testing
  TigerBeetle allocates all memory once at startup and never again, using fixed limits for connections, in-flight messages per client, replicas, message size and queries per message so that total memory is a simple compile-time sum.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
