Project

General

Profile

Actions

Feature #22067

open

New TypedData bit to allow the type to be freed in parallel

Feature #22067: New TypedData bit to allow the type to be freed in parallel

Added by luke-gru (Luke Gruber) 2 days ago. Updated about 18 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:125475]

Description

CRuby TypedData types are used internally in the VM and in C extensions and currently any free functions for these types are run with the VM lock held as well as the VM barrier (or, in the case of MMTk, are not freed in parallel). In short, no other Ruby threads or GC threads can run while these free functions are called. In order to allow one or more worker GC threads to free these TypedData objects, we need a way to specify that a TypedData type can be freed in parallel alongside the Ruby GC thread or Ruby code that is being run by another thread. Otherwise, we cannot free these types in the workers and must rely on the Ruby GC thread to do so. This is because it can be unsafe to call these free functions, depending on how they're implemented.

Most TypedData are safe to free in parallel. The exceptions are those free functions that read or modify global state without locking.

Examples

static void
example_data_free(void *ptr)
{
    st_delete(live_example_datas, (st_data_t*)&ptr, NULL); // Not thread-safe!
}

If 2 of these TypedData objects are freed at the same time, this could corrupt the st_table. A lock must be held when manipulating this table.

Because Ruby code can also run alongside a sweep worker, the lock must also be held when adding to or iterating this table.

rb_nativethread_lock_t example_data_lock;

static void
example_data_free(void *ptr)
{
    rb_native_mutex_lock(&example_data_lock);
    st_delete(live_example_datas, (st_data_t*)&ptr, NULL);
    rb_native_mutex_unlock(&example_data_lock);
}

static void
live_examples_add(void *ptr)
{
    rb_native_mutex_lock(&example_data_lock);
    st_insert(live_example_datas, (st_data_t)ptr, (st_data_t)ptr);
    rb_native_mutex_unlock(&example_data_lock);
}

static void
Init_example(void)
{
    rb_native_mutex_initialize(&example_data_lock);
}

If a CRuby developer or user wants to make their code compaction-safe, they don’t need to worry about parallel sweep workers because the workers don't run during compaction.

Proposal

I propose adding a new flag to TypedData that allows both CRuby developers and extension authors to opt-in to allow their TypedData type to be freed in parallel. It could be called something like RUBY_TYPED_PARALLEL_FREE_SAFE. In our branch where we are developing parallel sweeping, most TypedData internal to the VM are given this flag. I believe C extension authors would opt in as well if they see that parallel sweeping gives good performance benefits to Ruby applications.

We would need to document what is safe and what is not safe inside these free functions for types that are marked with this bit. If the user needs to use a native mutex to protect their TypedData from being corrupted when freed in parallel, they must do so. However, if they lock this native mutex in non-free function code paths as well, they may not allocate objects or use ruby_xmalloc while this mutex is held. These are some examples of trickiness when it comes to concurrency in the Ruby VM. I believe having a section in the extension documentation about these types of free functions (or elsewhere such as the Concurrency Guide) would give CRuby developers and extension authors more confidence in adding this bit to their types.

A possible future: parallel marking?

If in the future CRuby gets parallel marking, we believe we probably would need another bit for TypedData so we can register them as parallel-mark safe. If that’s the case, it’s unfortunate that authors that want this feature would need to update their extension again with a new bit if this bit were to be made public. One could argue that we should have a single bit that indicates safety for both parallel freeing and parallel marking. However, we believe the specifics of what could and couldn’t be executed inside these free and mark functions would be too hard to work out today for a combined bit without locking us into a specific parallel marking implementation.

Alternative

We could not expose the parallel-free-safe bit to extension authors and only free internal TypedData objects in parallel. However, this does slow the current implementation of parallel sweeping down because even if one of these objects is on a heap page, the Ruby GC thread needs to further post-process the page after the sweep thread sweeps it. It would also limit further optimizations to parallel sweeping.

Details of the bit

Worker threads could only free TypedData objects that have this new bit set alongside the RUBY_TYPED_FREE_IMMEDIATELY bit. Otherwise, the Ruby GC thread must free them. Also, if the type has 0 or RUBY_TYPED_DEFAULT_FREE/RUBY_DEFAULT_FREE as the free function, it can be freed in parallel.

Conclusion

Parallel sweeping is being actively developed and adding this bit increases performance of the developing implementation of parallel sweeping. It allows TypedData types to be freed by worker thread(s), which decreases GC pause times. Since TypedData are also exposed to extension authors, we need to whitelist these types for parallel freeing, as not all of these functions are currently safe. By documenting what is and isn’t safe, extension authors can write correct free functions that are safe to be freed by worker threads. Alternatively, we could have this bit be internal to CRuby.


Files

Screenshot_20260513_185452.webp (99.8 KB) Screenshot_20260513_185452.webp A GC with the MMTk GC module running railsbench wks (Kunshan Wang), 05/13/2026 11:13 AM
Screenshot_20260513_192449.webp (31.9 KB) Screenshot_20260513_192449.webp Default GC running railsbench wks (Kunshan Wang), 05/13/2026 11:30 AM
Screenshot_20260513_192734.webp (22.4 KB) Screenshot_20260513_192734.webp Default GC zoom-in 1 wks (Kunshan Wang), 05/13/2026 11:30 AM
Screenshot_20260513_192910.webp (27.3 KB) Screenshot_20260513_192910.webp Defautl GC zoom-in 2 wks (Kunshan Wang), 05/13/2026 11:30 AM
Actions

Also available in: PDF Atom