Repository navigation
Refinements: cheaper lexical guards and precompiled dispatchers (#56), plus a review fix - #59
Merged
Merged
Conversation
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]>
|
Warning Review limit reachedYou'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. View limit details
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. Comment |
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.
Lands #56 (@sholden's refinement guard: three commits, unchanged) plus one review fix on top.
9e3bb666Refinements: the refine-block count can't be left raised.EvaluateRefinementBlockraisedRefineBlocksRunningand then computed the block's refinements still outside thetrywhosefinallylowers 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 thetry.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