Skip to content

Add write barrier protection to RBS Location objects#3022

Open
soutaro wants to merge 1 commit into
masterfrom
claude/location-minor-gc-skip-j5m0fx
Open

Add write barrier protection to RBS Location objects#3022
soutaro wants to merge 1 commit into
masterfrom
claude/location-minor-gc-skip-j5m0fx

Conversation

@soutaro

@soutaro soutaro commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

This change adds write barrier protection to the RBS Location C extension to ensure proper garbage collection tracking when Location objects hold references to Ruby objects.

Key Changes

  • Added RUBY_TYPED_WB_PROTECTED flag to the location_type data type definition to enable write barrier protection
  • Modified buffer assignment in location_initialize, location_initialize_copy, rbs_new_location, rbs_new_location2, and rbs_new_location_from_loc_range to use RB_OBJ_WRITE macro
  • Changed direct buffer assignments to first initialize with Qnil, then use RB_OBJ_WRITE to properly register the object reference with Ruby's garbage collector

Implementation Details

The write barrier protection ensures that when a Location object stores a reference to a buffer (a Ruby object), the garbage collector is properly notified of this reference. This prevents potential issues where the buffer could be prematurely garbage collected if the Location object is stored in an older generation during generational GC.

By using RB_OBJ_WRITE instead of direct assignment, we allow Ruby's write barrier mechanism to track inter-generational references correctly, which is essential for the correctness of generational garbage collection.

https://claude.ai/code/session_011JK4Acfc1aTzpgpPZxwJLF

Mark the Location TypedData object with RUBY_TYPED_WB_PROTECTED and route
every write to the embedded `buffer` VALUE through RB_OBJ_WRITE. Once an
object is write-barrier protected, the GC can skip it during minor
collections instead of marking it every time, which speeds up GC.

All buffer assignments (initialize, initialize_copy, and the three
rbs_new_location* constructors) now go through the write barrier so the
old-to-young reference from a promoted Location to its Buffer is tracked
correctly.
@soutaro soutaro force-pushed the claude/location-minor-gc-skip-j5m0fx branch from 466a328 to 4058e12 Compare July 8, 2026 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants