Defragging Ruby
summary
Implements a live compaction for MRI Ruby using a two-finger compaction algorithm and a four‑pass GC (mark, compact, update references, free forwards), with objects on 16,384‑byte pages and 40‑byte slots; demonstrates tagged pointers (integers encoded in low bits, max ≈ 2^62−1), builds a Raspberry Pi + MSP430 cat‑poop scale as a motivating project, and shows a Rails heap where movable pages with free slots drop from 528 to 167 after calling gc.compact, noting that object IDs change until a fix is added.
AI-written summary · about
chapters (34)
- 0:00Start
- 7:28Bathroom Scale
- 7:44msp430
- 14:00Ruby's Memory
- 15:05Memory has many Pages
- 15:19Pages have many slots
- 15:32Slots can be empty, or not
- 15:59Slot is 40 bytes
- 16:10byte = 8 bits
- 17:40Memory Layout
- 20:01Addresses divisible by 40
- 20:43C = Ruby
- 22:00Verification in Ruby
- 22:38Max Int (without allocations)
- 24:16Slots live in Pages
- 24:33Free Slots are in a list
- 28:03Confusing Nomenclature
- 30:02Normal Malloc
- 30:32Aligned Malloc 10
- 31:22Ruby Page
- 32:56Page address tricks
- 34:11Wasted page space
- 35:11Find Page For Object
- 36:21Fragmentation / Locality
- 38:44finger compaction
- 39:57Reference Updating
- 40:57Hash Keys
- 42:27"Hidden" Objects
- 42:58Mark Phase
- 43:13Two Marking Functions
- 43:37Passes on the Heap
- 43:56Compaction Code
- 44:12Pre Compaction
- 46:14Production Use







