Skip to content

feat(stable-api): Add RB_OBJ_PROMOTED for GC generation checking #673

@ianks

Description

@ianks

Summary

Add stable API method for checking if a Ruby object has been promoted to the old generation in the GC, equivalent to RB_OBJ_PROMOTED.

Motivation

Understanding object GC generation is important for:

  • Implementing efficient write barriers
  • Debugging GC-related issues
  • Performance analysis of object lifetimes
  • Advanced GC-aware data structures

Ruby Source Reference

// include/ruby/internal/gc.h

static inline bool
RB_OBJ_PROMOTED_RAW(VALUE obj)
{
    RBIMPL_ASSERT_OR_ASSUME(RB_FL_ABLE(obj));
    return RB_FL_ANY_RAW(obj, RUBY_FL_PROMOTED);
}

static inline bool
RB_OBJ_PROMOTED(VALUE obj)
{
    if (! RB_FL_ABLE(obj)) {
        return false;
    }
    else {
        return RB_OBJ_PROMOTED_RAW(obj);
    }
}

Proposed API

/// Check if an object has been promoted to the old GC generation 
/// (akin to `RB_OBJ_PROMOTED`).
///
/// Objects start in the "young" generation and are promoted to "old"
/// after surviving one or more GC cycles. Old objects are scanned less
/// frequently, which is why write barriers are needed when storing
/// references from old to young objects.
///
/// # Safety
/// This function is unsafe because it may dereference a raw pointer
/// to access the object's flags. The caller must ensure the VALUE
/// is a valid Ruby object.
unsafe fn rb_obj_promoted(&self, obj: VALUE) -> bool;

Implementation Notes

  • Check RUBY_FL_PROMOTED flag (same as RUBY_FL_WB_PROTECTED, value 1<<5)
  • Must handle special constants (which are never promoted)
  • Reference: include/ruby/internal/gc.h:705-734

Checklist

  • Add method to StableApiDefinition trait
  • Implement for each Ruby version
  • Add C fallback in compiled.c
  • Add public macro wrapper in macros.rs
  • Add tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions