Add write barrier protection to RBS Location objects#3022
Open
soutaro wants to merge 1 commit into
Open
Conversation
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.
466a328 to
4058e12
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
RUBY_TYPED_WB_PROTECTEDflag to thelocation_typedata type definition to enable write barrier protectionlocation_initialize,location_initialize_copy,rbs_new_location,rbs_new_location2, andrbs_new_location_from_loc_rangeto useRB_OBJ_WRITEmacroQnil, then useRB_OBJ_WRITEto properly register the object reference with Ruby's garbage collectorImplementation 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_WRITEinstead 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