Skip to content

Refinements: cheaper lexical guards and precompiled dispatchers (#56), plus a review fix - #59

Merged
Largo merged 4 commits into
masterfrom
review/56-refine-counter
Oct 29, 2026
Merged

Largo merged 4 commits into
masterfrom
review/56-refine-counter

Conversation

@Largo

@Largo Largo commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Lands #56 (@sholden's refinement guard: three commits, unchanged) plus one review fix on top.

  • 9e3bb666 Refinements: the refine-block count can't be left raised. EvaluateRefinementBlock raised RefineBlocksRunning and then computed the block's refinements still outside the try whose finally lowers it. An exception there left the count raised for the life of the process, which silently turned off every runtime's record-based guards for calls in blocks. The activation is now computed first, and nothing that can throw comes between the increment and the try.

The branch starts at #56's exact head (4e5b801), so merging it with a merge commit also marks #56 as merged.

Checked: Debug build, net8.0, Linux, 0 warnings. IronRuby.Tests: 0 failures. #56's own CI is green on that head.

🤖 Generated with Claude Code

sholden and others added 4 commits October 29, 2026 10:41
The rule of a call a refinement could change tests, on every call, that the
caller's scope has the refinements the call was bound under. The scope
answered by recursion. A block scope asked its parent, which asked its own,
and every step checked again that something had been refined, read the
context's refinement version and looked for the memo that only scopes with
rare state keep. For a call in a block in a method that was two calls and
some twenty dependent loads.

The rule now calls RubyScope.GetLexicalRefinements. It leaves out the
"anything refined?" test, since a rule with the guard is only built once
something has been refined, and that stays true. It walks the chain in a
loop. A scope without rare state has no `using' of its own and no memo, so a
block steps to its parent unless its Proc has a refine block's override, and
a method answers from its definition record. The first scope with rare state
answers from its memo, read in place, or takes the general step as before,
which recurses from there. GetActiveRefinements is the same walk behind the
"anything refined?" test, so every caller gets the answer it got before.

A refined call in a block in a method, 20 million calls on one core in
Release, took 0.75 s before and 0.66 s after, against 0.56 s for the same
call to an ordinary method. Made directly in the method it went from 0.68 s
to 0.63 s. Five million calls in a block at the top level went from 0.27 s to
0.25 s, against 0.22 s, and in a class body from 0.32 s to 0.29 s, against
0.25 s.

There are no new tests, since every answer is the same. Refinements1-7, the
refinement and binding matrices and the refinement specs cover the walk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… scopes

A method sees the refinements recorded where its `def' ran, and nothing can
`using' anything inside a method. So the scope of a method has exactly the
refinements its record holds. So does the scope of a block written directly
in one, unless a refine block is running the block, because Module#refine
puts an override on the Proc for as long as it runs.

The rule of a refined call made from such a scope now tests that instead of
walking. From a method scope it tests that the scope's record holds the
refinements the call was bound under. From a block scope it tests that the
block's parent is a method scope whose record holds them, and that no refine
block is running. The test is chosen for the kind of scope the call was bound
from, and the rule is shared like any other. The scope of another method with
the same refinements passes it, and any other scope fails it and is bound
with the walk.

- RubyUtils.EvaluateRefinementBlock counts the refine blocks running, in
  RubyOps.RefineBlocksRunning, around setting and restoring the override.
  While none is running no Proc has an override, so the walk also steps over
  a block scope without reading its Proc.
- RubyMethodBody.SetDefinitionRefinements bumps the refinement version when a
  `def' runs again under other refinements and compiled code may already use
  its record, so that a scope that memoized the old answer agrees with the
  rules that read the record. That changes one case. A call still running
  when its own `def' ran again under other refinements kept the old ones if
  its scope had evaluated a string, and saw the new ones otherwise. It now
  sees the new ones either way. CRuby keeps the old ones, but IronRuby only
  reaches this case because a block run before and after a top-level `using'
  sees it the second time, and CRuby's block doesn't.

I first gave each method body binders of its own, whose rules tested the
record without a type test. That was no faster, and a file of 2000 methods
calling LINQ's names took twice as long to warm up, because bodies no longer
shared rules.

The same 20 million refined calls on one core went from 0.66 s to 0.62 s in a
block in a method and from 0.63 s to 0.60 s directly in it, against 0.56 to
0.58 s for an ordinary call. #2's benchmark of a hot refined call, run five
times as long, went from 1.96 s to 1.87 s, against 1.80 s.

Refinements8 covers a `def' run again under other refinements, with calls
bound against the old record in the method and in a block, and a call still
running, with and without an eval. In Refinements9, Module#refine runs a
block in a method, and one inside it, after their calls were bound against
the method's record. It fails if the rule or the walk ignores the count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A call a refinement could change never got a precompiled dispatcher, because
a dispatcher tested nothing but the receiver's class version. Where the rule
reads a method's record, a dispatcher can read it too. That is a call made
from the scope of a method, or of a block written directly in one. The
dispatcher tests that the scope is a method scope whose record holds the
refinements the call was resolved under, or a block scope whose parent's
record holds them while no refine block runs.

RubyCallAction.BindPrecompiled resolves such a call under the caller's
refinements and asks the RubyMethodInfo for a refined dispatcher. These are
variants of the WithScope and WithScopeAndBlock dispatchers, generated from
the same templates for arities 0 to 5. Like the others, a refined dispatcher
is pointed at the compiled body when the light compiler compiles it, and a
rule never is. Any other refined call gets a rule as before, and the
dispatchers of unrefined calls are unchanged.

#2's benchmark of a hot refined call, run five times as long on one core,
took 1.87 s with the rule and 1.81 s with the dispatcher, against 1.80 s for
the same call to an ordinary method.

Refinements10 makes refined calls on a Ruby object with no arguments, with
two and with a block, in a method and in a block in it. Then it runs the
`def' again under other refinements and has Module#refine run the block. It
fails if the dispatcher ignores the record or the refine block count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
EvaluateRefinementBlock raised RubyOps.RefineBlocksRunning and then, still
outside the try whose finally lowers it, computed the block's refinements.
An exception there left the count above zero for the life of the process,
and with it every runtime's record-based guards for calls in blocks off, in
silence. The refinements are now computed before the count goes up, and
nothing that can throw comes between the increment and the try.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 29, 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 11 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: 986d535b-49eb-435c-a7cf-16c20d52054d
Commits

Reviewing files that changed from the base of the PR and between 3cc808e and 9e3bb66.

Files ignored due to path filters: 2
  • Src/Ruby/Compiler/ReflectionCache.Generated.cs is excluded by !**/*.generated.*
  • Src/Ruby/Runtime/Calls/MethodDispatcher.Generated.cs is excluded by !**/*.generated.*
Files selected for processing: 10
  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/RefinementTests.cs
  • Src/Ruby/Compiler/ReflectionCache.cs
  • Src/Ruby/Runtime/Calls/MethodDispatcher.cs
  • Src/Ruby/Runtime/Calls/RubyCallAction.cs
  • Src/Ruby/Runtime/Calls/RubyMethodBody.cs
  • Src/Ruby/Runtime/Calls/RubyMethodInfo.cs
  • Src/Ruby/Runtime/RubyOps.cs
  • Src/Ruby/Runtime/RubyScope.cs
  • Src/Ruby/Runtime/RubyUtils.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

Comment @coderabbitai help to get the list of available commands.

@Largo
Largo merged commit 4635a40 into master Oct 29, 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