From Ruby 2 to 4: Updating a C extension for a Modern VM
summary
Datadog updates a C extension to target Ruby 4 APIs, replacing legacy patterns that slow GC and inflate memory. Practical swaps: use RB_ARRAY_ENTRY/RB_ARRAY_STORE instead of RArray pointers, use RB_HASH_ instead of RHash table ops, prefer FSTRING/dedup for long‑lived keys, and use rb_xmalloc/rb_xfree so Ruby can trigger GC on OOM. Replace data_wrap_struct with typeddata that implements write barriers, decompact* and memsize reporting. A polyfill layer targets modern APIs and falls back for older Rubies.
AI-written summary · about
chapters (14)
- 0:00Introduction and background
- 3:14Why update C extensions
- 7:28Mechanical sympathy in Ruby
- 10:36Avoiding C when possible
- 11:18Array handling tips
- 13:25Hash handling tips
- 14:30String optimization with F strings
- 17:59Memory management best practices
- 20:28Avoiding immortal objects
- 21:30Typed data pitfalls
- 24:16Write barriers and compaction
- 26:04Type embeddable flag benefits
- 27:29Performance impact and future work
- 29:12Q&A and resources








