Skip to content

ZJIT: Refine singleton class invariant to only invalidate on method shadowing#950

Draft
tekknolagi wants to merge 1 commit intoShopify:ruby-upstreamfrom
tekknolagi:singleton-class-shadowing-invariant
Draft

ZJIT: Refine singleton class invariant to only invalidate on method shadowing#950
tekknolagi wants to merge 1 commit intoShopify:ruby-upstreamfrom
tekknolagi:singleton-class-shadowing-invariant

Conversation

@tekknolagi
Copy link

Summary

  • Refine NoSingletonClass invariant to NoSingletonClassWithShadowingMethod: only invalidate when a method defined on a singleton class shadows a method on the original class
  • Move the C hook from make_singleton_class() in class.c to rb_method_entry_make() in vm_method.c, checking rb_method_entry(super, mid) for shadowing
  • Creating a singleton class alone, or defining non-shadowing methods on one, no longer invalidates JIT code

Test plan

  • 1431 unit tests pass (including 5 new singleton class tests)
  • 27 integration tests pass
  • CI

…hadowing

Previously, the NoSingletonClass invariant invalidated JIT code whenever
a singleton class was created for an instance of a given class. This was
overly conservative—many uses of singleton classes (freeze, introspection,
non-shadowing methods) don't affect method dispatch for the original class.

Refine the invariant to NoSingletonClassWithShadowingMethod: only
invalidate when a method is defined on a singleton class that shadows
a method on the original class. The hook moves from make_singleton_class()
in class.c to rb_method_entry_make() in vm_method.c, where it checks
whether the newly defined method exists on the superclass chain.

Correctness is always maintained by GuardType, which checks the exact
class of the receiver—singleton class objects fail this check and
side-exit regardless of the patchpoint.
@tekknolagi tekknolagi force-pushed the singleton-class-shadowing-invariant branch from 01b413c to 8acac8b Compare February 26, 2026 20:50
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.

1 participant