Speeding up Instance Variables with Red-Black Trees
summary
Ruby replaces the linear parent lookup for instance-variable shapes with a red-black tree parent cache and ports the logic to C implementation, keeping shapes immutable so no deletions are needed. Benchmarks compare Ruby 3.2.2, 3.3 and a 3.4 branch: reads and writes show cache-miss cost stops growing with the number of ivars in 3.3, and 3.4 further separates hit vs miss times; undefined-var checks also become dramatically faster.
AI-written summary · about
chapters (12)
- 0:00Introduction to talk
- 1:41What are red-black trees
- 4:28Functional red-black trees
- 15:24Object shapes and instance variables
- 20:33Inline caches in Ruby
- 22:08Why cache misses happen
- 25:25Ruby 3.3 red-black tree cache
- 26:12Benchmark results Ruby 3.2
- 26:33Benchmark results Ruby 3.3
- 28:12Undefined instance variable checks
- 29:18Ruby 3.4 performance gains
- 30:06Key takeaways and conclusion








