Skip to content

Call-site binding: read parameter attributes and class getters once per type - #88

Merged
Largo merged 1 commit into
masterfrom
ccr-4d94625d-0splsp-r4
Nov 4, 2026
Merged

Largo merged 1 commit into
masterfrom
ccr-4d94625d-0splsp-r4

Conversation

@Largo

@Largo Largo commented Nov 4, 2026

Copy link
Copy Markdown
Owner

This PR removes two reflection lookups, both in 2016 code, that run every time a call site is bound. Each lookup answers the same thing every time for the same parameter or type.

  • RubyOverloadResolver called ParameterInfo.IsDefined for [DefaultProtocol] (in HasExplicitProtocolConversion and Convert) and for [ParamArray], several times per candidate overload. Each call walks the parameter's metadata and allocates. Each parameter is now read once and kept in a ConditionalWeakTable.
  • MetaObjectBuilder.AddTargetTypeTest called type.GetMethod for the ImmediateClass getter and checked Type.IsVisible, which walks generic arguments and declaring types, for every rule on a Ruby object. Both are now cached per type, also in a weak table.

Every new class pays for bindings, because its call sites miss their cached rules. A profile of Struct.new and Class.new showed where that time went:

  • Type.IsVisible was the largest self-time entry at 10.9 %, and two thirds of it came from AddTargetTypeTest.
  • CustomAttribute.IsDefined took another 7.6 %.

Numbers

The benchmark is one Struct.new(:a, :b, keyword_init: true), one Struct.new(:a, :b) and one Class.new per iteration, best of 6, Release, net8.0. It went from 828–858 to 783–817 µs, about 5 % faster.

Loading a set of standard libraries at startup showed no difference that stood out from the noise.

Testing (Linux, net8.0, Release)

  • Util/run-tests.sh: PASSED, 0 failure lines.
  • ruby/spec, with failure/error lists identical to the build without the change:
    • language and core/{kernel,module,class}: 6,269 examples, 1 failure.
    • core/{struct,data,string,array,hash,integer,proc,method}: 8,726 examples, 1 failure and 19 errors.

Generated by Claude Code

…er type

Two reflection lookups at every binding of a call site, both answering
the same thing every time for the same parameter or type:

- RubyOverloadResolver (2016): ParameterInfo.IsDefined for
  [DefaultProtocol] (HasExplicitProtocolConversion, Convert) and
  [ParamArray] - several times per candidate overload, each a walk of
  the parameter's metadata that allocates. Now one read per parameter,
  kept in a ConditionalWeakTable.
- MetaObjectBuilder.AddTargetTypeTest (2016): type.GetMethod for the
  ImmediateClass getter and Type.IsVisible, which walks generic arguments
  and declaring types, for every rule on a Ruby object. Now cached per
  type, weakly.

Bindings are what every new class costs: its call sites miss their
rules. In a profile of Struct.new/Class.new, Type.IsVisible was the
largest self-time entry (10.9%, two thirds from AddTargetTypeTest) and
CustomAttribute.IsDefined another 7.6%.

Struct.new(:a, :b, keyword_init: true) + Struct.new(:a, :b) + Class.new,
best of 6, Release, net8.0: 828-858 -> 783-817 us (about 5%).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KPwZf5xqgL3eZiUCWPfG2b
@coderabbitai

coderabbitai Bot commented Nov 4, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8114b3f1-c107-4a4b-a44b-f474b3b8d6dc
Commits

Reviewing files that changed from the base of the PR and between 9312954 and cb58aef.

Files selected for processing: 2
  • Src/Ruby/Runtime/Calls/MetaObjectBuilder.cs
  • Src/Ruby/Runtime/Calls/RubyOverloadResolver.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@Largo
Largo merged commit 92deebd into master Nov 4, 2026
8 checks passed
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