A Muggle's Guide to Tail Call Optimization in Ruby
summary
Claim: Ruby 1.9.2 has TCO but disabled; the VM actually reuses a single stack frame instead of growing one per recursive call, so tail recursion becomes a loop and avoids SystemStackError until ≈10,000 frames. Tools demonstrated: flip two flags in MRI, use RubyVM::InstructionSequence or the TCO method gem to enable it per-compile. Tradeoffs: set_trace_func and some debugging/gems break, Rails test runs show failures, and real-world speedups are modest though memory pressure and GC can improve.
AI-written summary · about
chapters (22)
- 0:00Introduction
- 3:28What is a tail call
- 8:07What is tail recursion
- 12:00What is tail call optimization
- 16:32How to enable tail call optimization
- 23:46Benefits of tail call optimization
- 26:58Limitations of tail call optimization
- 29:45Where to find Danny
- 30:43If you use the mixin
- 31:56Stack errors
- 32:46Recursive strategies
- 33:15Benchmarks
- 33:56How to tell if a method is optimized
- 35:21Dependencies
- 36:01New Relic
- 36:18What do I think would need to happen
- 36:45Best way to do it
- 37:16Making it easier to explore
- 38:21Other VM optimizations
- 39:10JRuby
- 40:02Ruby Test Suite
- 40:31When to use it








