DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

Supercharging Ruby with Embedded TypedData Objects

June 7, 2025

Ruby continues to evolve, balancing its expressive elegance with a growing focus on performance. One of the latest improvements introduced in Ruby 3.3 is embedded TypedData objects , a low-level optimization with impressive results. This advancement, developed by Peter Zhu and Jean Boussier, is already paying dividends in core Ruby operations—and soon, third-party extensions may benefit too.

What Are TypedData Objects?

In CRuby, every object has a defined type. For objects backed by native C data—such as Time, Mutex, or Enumerator—Ruby uses TypedData objects. These allow Ruby to interface directly with native memory, which is especially useful in C extensions like Nokogiri or pg.

Traditionally, TypedData objects involve two allocations :

  1. One for the Ruby object itself.
  2. One for the external data pointer it wraps.

This structure, while flexible, introduces some overhead in both memory and performance.

What Changed in Ruby 3.3?

Article content

The Ruby core team introduced embedded TypedData objects , a feature that co-locates the native data directly after the Ruby object in memory. This simple change leads to powerful results:

  • ✅ Single allocation instead of two
  • 🚀 Faster access due to better memory locality
  • 🧠 Smaller memory footprint
  • ♻ Improved garbage collection performance

To enable embedding, a TypedData type must be marked with the RUBY_TYPED_EMBEDDABLE flag. However, this optimization is only safe when the data is non-shared , as object compaction may relocate it.

Real-World Speedups

This optimization isn’t just theoretical. Several Ruby operations now run significantly faster:

Article content

These aren’t niche improvements—these methods are called millions of times in real-world Ruby applications.

What’s Next?

Article content

The Ruby team plans to make embedded TypedData accessible to third-party native extensions , which opens the door to further performance gains across the Ruby ecosystem. Developers of C extensions will be able to opt into this optimization to improve both runtime and memory behavior.


Reference

This article is based on “Implementing Embedded TypedData Objects” by Peter Zhu, published on Rails at Scale.

Article content

Top comments (0)