Optimizing Ruby's Memory Layout
summary
Proposal moves malloced object payloads into the ruby heap by allocating variable-size slots (power-of-two multiples of the 40‑byte rvalue) and per‑size size pools, placing payload immediately after the rvalue to improve cache locality. Implementation covers classes and strings, handles resizing by falling back to malloc today, and leans on compaction as a future fix. Microbenchmarks show 35% faster string compare and other ops faster; production testing across ~500M requests yields 4% memory reduction with no latency impact on 71‑byte strings.
AI-written summary · about
chapters (28)
- 0:00Intro
- 0:48RVALUE structure
- 1:14Ruby Object Structure
- 1:40Heap page structure
- 2:43Allocating Ruby objects
- 3:45Ruby's garbage collector
- 4:16Marking phase
- 4:35Marking example
- 5:44Sweeping example
- 6:20Compact phase
- 6:50Compaction algorithm
- 7:08Compaction example
- 9:05Allocating an embedded string
- 10:57CPU cache properties
- 11:54Ruby cache performance
- 12:31Overhead of malloc
- 13:12The Variable Width Allocation project
- 13:48Variable Width cache performance
- 15:21RClass Allocation
- 17:33Size pools
- 18:55Types of String allocation
- 19:23Allocating a short string
- 20:13Allocating a longer string
- 20:46Resizing a string
- 23:01railsbench
- 23:33rdoc generation
- 25:32Resizing objects
- 26:07Shrinking RVALUE
related talks
39mPointers for Eliminating Heaps of Memory
29mWhat causes Ruby memory bloat?
46mRuby Memory Management Hacks
45mClosing Keynote: Analyzing and Reducing Ruby Memory Usage
27mParallel Ruby: Managing the Memory Monster
47mCompacting Heaps in Ruby 2.7
34mHow to optimize Ruby internal.
52mReduce Memory by Using More
