From 16728eb67acac68cf9cd35aac662e933371634ca Mon Sep 17 00:00:00 2001 From: Largo Date: Thu, 19 Nov 2026 15:55:23 +0100 Subject: [PATCH 01/10] Light frames: name the construct that keeps a method out, not only its statement IR_LIGHT_FRAMES=why, and the census's "not a light frame: why" table built on it, said for a body statement that is not scope-free only its node type and line ("MethodCall at line 40"). The top lines of that table (MethodCall, IfExpression, SimpleAssignmentExpression...) named the statement and not what IsScopeFree refused inside it: in the AWFY benchmarks all twelve MethodCall refusals are a Range literal receiving `each' with a block, which the table could not show. IsScopeFree now takes an optional StrongBox that a refusal fills with its reason. A sub-node's refusal is its parent's and ends the walk, so the reason recorded is the innermost decision IsScopeFree took: the node type for a kind it does not take (SuperCall, RegularExpression, a Hash, with "(keyword arguments)" for a call's keywords), or the condition that refused a node it does take (an assignment's target kind, a Range in a body with literal blocks, a constant with an expression qualifier, a call's block, a switch that is off). The normal path passes null and its answers are unchanged. The reason follows the old text: "IfExpression at line 12: GlobalVariable". census.rb keeps its "why" table (it strips the new part) and adds "not a light frame: the construct", which groups the refusals by that construct across statements, with the statements holding it as the detail. Checked with the census over Src/StdLib and the AWFY benchmarks: the counts are those of the unchanged build (light frames 20318, light blocks 7389, JIT 2909/2362/23064), no row of the saved census differs in any column but light_why, and each light_why that changed is the old text plus ": ". Util/interpolation-matrix.rb prints what CRuby 4.0 prints, with and without IR_LIGHT_FRAMES=why, and census.rb -c still parses that run's trace, with the same five disagreements as before (four refinements, one nested def). Co-Authored-By: Claude Opus 5.5 --- .../Ast/Definitions/MethodDefinition.cs | 117 ++++++++++++------ Util/perf/census.rb | 8 +- 2 files changed, 83 insertions(+), 42 deletions(-) diff --git a/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs b/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs index 34597db6..f8c23cf6 100644 --- a/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs +++ b/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs @@ -540,7 +540,7 @@ private Expression FirstNotScopeFree(bool blockLiterals) { if (statements != null) { bool first = true; foreach (var statement in statements) { - if (!IsScopeFree(statement, blockLiterals) && !(first && IsBlockGuard(statement))) { + if (!IsScopeFree(statement, blockLiterals, null) && !(first && IsBlockGuard(statement))) { return statement; } first = false; @@ -551,7 +551,9 @@ private Expression FirstNotScopeFree(bool blockLiterals) { /// /// IR_LIGHT_FRAMES=why: what IsLightFrameCandidate refused - the parameters, a rescue/ensure, or the first statement - /// that is not scope-free (its node type and line). Null if it is a candidate. + /// that is not scope-free: its node type and line, then the construct inside it that IsScopeFree refused + /// (`IfExpression at line 12: GlobalVariable'), since the statement alone does not say what to teach the frames next. + /// Null if it is a candidate. /// private string WhyNotLightFrameCandidate() { if (!AreLightOptionals() || _parameters.Unsplat != null && !LightRestEnabled || _parameters.SuperSplat != null || @@ -569,7 +571,8 @@ private string WhyNotLightFrameCandidate() { if (FirstNotScopeFree(false) != null) { var statement = FirstNotScopeFree(LightBlocksEnabled); if (statement != null) { - return String.Format("{0} at line {1}", statement.GetType().Name, statement.Location.Start.Line); + return String.Format("{0} at line {1}: {2}", statement.GetType().Name, statement.Location.Start.Line, + WhyNotScopeFree(statement, LightBlocksEnabled)); } } return IsLightFrameCandidate() ? null : "parameters"; @@ -608,7 +611,7 @@ private bool AreLightOptionals() { return false; } foreach (var parameter in optional) { - if (!(parameter.Left is LocalVariable) || !IsScopeFree(parameter.Right, false)) { + if (!(parameter.Left is LocalVariable) || !IsScopeFree(parameter.Right, false, null)) { return false; } } @@ -623,39 +626,61 @@ private bool HasBlockGuard { } internal static bool IsScopeFree(Expression/*!*/ node) { - return IsScopeFree(node, false); + return IsScopeFree(node, false, null); + } + + // IR_LIGHT_FRAMES=why: names what IsScopeFree refused in the node. Every refusal of a sub-node is its parent's too and + // ends the walk, so the reason it records is the innermost one: the decision IsScopeFree took, not a guess from the node. + private static string WhyNotScopeFree(Expression/*!*/ node, bool blockLiterals) { + var why = new StrongBox(); + return IsScopeFree(node, blockLiterals, why) ? null : why.Value; + } + + // Records the reason of a refusal for WhyNotScopeFree, if it asked (why is null otherwise), and refuses. + private static bool Refuse(StrongBox why, string/*!*/ reason) { + if (why != null) { + why.Value = reason; + } + return false; } // blockLiterals: a call with a literal block is scope-free too (see TransformLightFrame); the block's own body is compiled // with a scope of its own, made with this frame's scope as its parent, which is then made on demand for it - private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals) { + private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals, StrongBox why) { // `def f = [[[...]]]' a thousand deep: such a body is not worth a light frame, nor a stack overflow to find out if (!System.Runtime.CompilerServices.RuntimeHelpers.TryEnsureSufficientExecutionStack()) { - return false; + return Refuse(why, "nested too deep"); } if (node is LocalVariable || node is Literal || node is SelfReference || node is InstanceVariable) { return true; } var constant = node as ConstantVariable; if (constant != null) { - return IsCachedConstant(constant); + return IsCachedConstant(constant) || Refuse(why, "ConstantVariable with an expression qualifier"); } var assignment = node as SimpleAssignmentExpression; if (assignment != null) { // `@x += @v' is a call of + between a read and a write - if (!IsScopeFree(assignment.Right, blockLiterals)) { + if (!IsScopeFree(assignment.Right, blockLiterals, why)) { return false; } if (assignment.Left is LocalVariable || assignment.Left is InstanceVariable) { - return assignment.Operation == null || LightCallsEnabled; + return assignment.Operation == null || LightCallsEnabled || Refuse(why, "operator assignment (IR_LIGHT_CALLS=0)"); } // `o.x = v', `a[i] = v' (and their op-assigns): calls of x= and []= var attribute = assignment.Left as AttributeAccess; if (attribute != null) { - return LightCallsEnabled && !attribute.IsSafeNavigation && IsScopeFree(attribute.Qualifier, blockLiterals); + if (!LightCallsEnabled || attribute.IsSafeNavigation) { + return Refuse(why, LightCallsEnabled ? "assignment to a safe-navigation attribute" : "assignment to an attribute (IR_LIGHT_CALLS=0)"); + } + return IsScopeFree(attribute.Qualifier, blockLiterals, why); } var item = assignment.Left as ArrayItemAccess; - return item != null && LightCallsEnabled && item.Block == null && IsScopeFree(item.Array, blockLiterals) && AreScopeFree(item.Arguments, blockLiterals); + if (item == null || !LightCallsEnabled || item.Block != null) { + return Refuse(why, item == null ? "assignment to " + assignment.Left.GetType().Name : + LightCallsEnabled ? "assignment to an element with a block" : "assignment to an element (IR_LIGHT_CALLS=0)"); + } + return IsScopeFree(item.Array, blockLiterals, why) && AreScopeFree(item.Arguments, blockLiterals, why); } // a String literal is a constant, or a copy of one; a Symbol literal is a constant if (node is StringLiteral) { @@ -664,7 +689,7 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals) { if (LightCallsEnabled) { var array = node as ArrayConstructor; if (array != null) { - return AreScopeFree(array.Arguments, blockLiterals); + return AreScopeFree(array.Arguments, blockLiterals, why); } // `(a..b)': the Range is made from its bounds and the literal's comparison storage (RubyOps.CreateInclusiveRange); // Range#initialize checks the stack before the <=> it calls for bounds that are not two Integers. Not in a body @@ -673,13 +698,16 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals) { // the scope made on entry (`(a..b)' kept such a body out of light frames until then). var range = node as RangeExpression; if (range != null) { - return !blockLiterals && IsScopeFree(range.Begin, false) && IsScopeFree(range.End, false); + if (blockLiterals) { + return Refuse(why, "RangeExpression in a body with literal blocks"); + } + return IsScopeFree(range.Begin, false, why) && IsScopeFree(range.End, false, why); } // "#{x}" converts each part through a to_s site var interpolation = node as StringConstructor; if (interpolation != null && interpolation.Kind == StringKind.Mutable) { foreach (var part in interpolation.Parts) { - if (!IsScopeFree(part, blockLiterals)) { + if (!IsScopeFree(part, blockLiterals, why)) { return false; } } @@ -687,20 +715,20 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals) { } var loop = node as WhileLoopExpression; if (loop != null) { - return IsScopeFree(loop.Condition, blockLiterals) && AreScopeFree(loop.Statements, blockLiterals); + return IsScopeFree(loop.Condition, blockLiterals, why) && AreScopeFree(loop.Statements, blockLiterals, why); } var caseExpression = node as CaseExpression; if (caseExpression != null) { - if (caseExpression.Value != null && !IsScopeFree(caseExpression.Value, blockLiterals) || !AreScopeFree(caseExpression.ElseStatements, blockLiterals)) { + if (caseExpression.Value != null && !IsScopeFree(caseExpression.Value, blockLiterals, why) || !AreScopeFree(caseExpression.ElseStatements, blockLiterals, why)) { return false; } foreach (var clause in caseExpression.WhenClauses) { foreach (var comparison in clause.Comparisons) { - if (!IsScopeFree(comparison, blockLiterals)) { + if (!IsScopeFree(comparison, blockLiterals, why)) { return false; } } - if (!AreScopeFree(clause.Statements, blockLiterals)) { + if (!AreScopeFree(clause.Statements, blockLiterals, why)) { return false; } } @@ -711,30 +739,36 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals) { // is given (LightFrameRewriter.LightScopeCall) var splat = node as SplattedArgument; if (splat != null) { - return LightSplatEnabled && IsScopeFree(splat.Argument, blockLiterals); + return (LightSplatEnabled || Refuse(why, node.GetType().Name + " (IR_LIGHT_SPLAT=0)")) && IsScopeFree(splat.Argument, blockLiterals, why); } // `inner(&block)': the Proc is the caller's own, or made by #to_proc through a conversion site that takes no // scope, and the call passes it as an argument (LightFrameRewriter.LightScopeCall) var call = node as MethodCall; if (call != null) { var blockReference = call.Block as BlockReference; - return (call.Block == null || blockLiterals && call.Block is BlockDefinition || - LightBlockPassEnabled && blockReference != null && IsScopeFree(blockReference.Expression, blockLiterals)) && - (call.Target == null || IsScopeFree(call.Target, blockLiterals)) && AreScopeFree(call.Arguments, blockLiterals); + if (call.Block != null && !(blockLiterals && call.Block is BlockDefinition)) { + if (blockReference == null || !LightBlockPassEnabled) { + return Refuse(why, blockReference == null ? "call with a literal block" : "call passing a block (IR_LIGHT_BLOCK_PASS=0)"); + } + if (!IsScopeFree(blockReference.Expression, blockLiterals, why)) { + return false; + } + } + return (call.Target == null || IsScopeFree(call.Target, blockLiterals, why)) && AreScopeFree(call.Arguments, blockLiterals, why); } var conditional = node as ConditionalExpression; if (conditional != null) { - return IsScopeFree(conditional.Condition, blockLiterals) && IsScopeFree(conditional.TrueExpression, blockLiterals) && - IsScopeFree(conditional.FalseExpression, blockLiterals); + return IsScopeFree(conditional.Condition, blockLiterals, why) && IsScopeFree(conditional.TrueExpression, blockLiterals, why) && + IsScopeFree(conditional.FalseExpression, blockLiterals, why); } var ifExpression = node as IfExpression; if (ifExpression != null) { - if (!IsScopeFree(ifExpression.Condition, blockLiterals) || !AreScopeFree(ifExpression.Body, blockLiterals)) { + if (!IsScopeFree(ifExpression.Condition, blockLiterals, why) || !AreScopeFree(ifExpression.Body, blockLiterals, why)) { return false; } if (ifExpression.ElseIfClauses != null) { foreach (var clause in ifExpression.ElseIfClauses) { - if (clause.Condition != null && !IsScopeFree(clause.Condition, blockLiterals) || !AreScopeFree(clause.Statements, blockLiterals)) { + if (clause.Condition != null && !IsScopeFree(clause.Condition, blockLiterals, why) || !AreScopeFree(clause.Statements, blockLiterals, why)) { return false; } } @@ -743,31 +777,31 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals) { } var unlessExpression = node as UnlessExpression; if (unlessExpression != null) { - return IsScopeFree(unlessExpression.Condition, blockLiterals) && AreScopeFree(unlessExpression.Statements, blockLiterals) && - (unlessExpression.ElseClause == null || AreScopeFree(unlessExpression.ElseClause.Statements, blockLiterals)); + return IsScopeFree(unlessExpression.Condition, blockLiterals, why) && AreScopeFree(unlessExpression.Statements, blockLiterals, why) && + (unlessExpression.ElseClause == null || AreScopeFree(unlessExpression.ElseClause.Statements, blockLiterals, why)); } var and = node as AndExpression; if (and != null) { - return IsScopeFree(and.Left, blockLiterals) && IsScopeFree(and.Right, blockLiterals); + return IsScopeFree(and.Left, blockLiterals, why) && IsScopeFree(and.Right, blockLiterals, why); } var or = node as OrExpression; if (or != null) { - return IsScopeFree(or.Left, blockLiterals) && IsScopeFree(or.Right, blockLiterals); + return IsScopeFree(or.Left, blockLiterals, why) && IsScopeFree(or.Right, blockLiterals, why); } var not = node as NotExpression; if (not != null) { - return IsScopeFree(not.Expression, blockLiterals); + return IsScopeFree(not.Expression, blockLiterals, why); } } var yieldCall = node as YieldCall; if (yieldCall != null) { - return yieldCall.Block == null && AreScopeFree(yieldCall.Arguments, blockLiterals); + return (yieldCall.Block == null || Refuse(why, "yield with a block")) && AreScopeFree(yieldCall.Arguments, blockLiterals, why); } // `(yield self; self)', the body of an endless def written with several statements var block = node as BlockExpression; if (block != null) { foreach (var statement in block.Statements) { - if (!IsScopeFree(statement, blockLiterals)) { + if (!IsScopeFree(statement, blockLiterals, why)) { return false; } } @@ -775,9 +809,12 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals) { } var jump = node as ReturnStatement; if (jump != null) { - return AreScopeFree(jump.Arguments, blockLiterals); + return AreScopeFree(jump.Arguments, blockLiterals, why); } - return false; + // a node of a kind not taken at all: its type says what it is, and a Hash says whether it is a call's keywords + var hash = node as HashConstructor; + return Refuse(why, node.GetType().Name + (hash != null && hash.IsKeywordArguments ? " (keyword arguments)" : "") + + (LightCallsEnabled ? "" : " (IR_LIGHT_CALLS=0)")); } // A, ::A, A::B::C - the reads ConstantVariable.MakeCachedRead makes - not {expr}::A @@ -786,10 +823,10 @@ private static bool IsCachedConstant(ConstantVariable/*!*/ constant) { return qualifier == null || (qualifier is ConstantVariable && IsCachedConstant((ConstantVariable)qualifier)); } - private static bool AreScopeFree(Statements statements, bool blockLiterals) { + private static bool AreScopeFree(Statements statements, bool blockLiterals, StrongBox why) { if (statements != null) { foreach (var statement in statements) { - if (!IsScopeFree(statement, blockLiterals)) { + if (!IsScopeFree(statement, blockLiterals, why)) { return false; } } @@ -797,10 +834,10 @@ private static bool AreScopeFree(Statements statements, bool blockLiterals) { return true; } - private static bool AreScopeFree(Arguments arguments, bool blockLiterals) { + private static bool AreScopeFree(Arguments arguments, bool blockLiterals, StrongBox why) { if (arguments != null) { foreach (var argument in arguments.Expressions) { - if (!IsScopeFree(argument, blockLiterals)) { + if (!IsScopeFree(argument, blockLiterals, why)) { return false; } } diff --git a/Util/perf/census.rb b/Util/perf/census.rb index 5bcb8b45..f5875275 100644 --- a/Util/perf/census.rb +++ b/Util/perf/census.rb @@ -60,8 +60,11 @@ pct = ->(n, of) { of.zero? ? '-' : format('%5.1f%%', 100.0 * n / of) } jit_text = ->(m) { m[:jit] == 'compiles' ? "compiles#{m[:jit_types].to_s.empty? ? '' : " (#{m[:jit_types]})"}" : "#{m[:jit]} (#{m[:jit_why]})" } light_text = ->(m) { m[:light] ? 'light' : "scope (#{m[:light_why]})" } -# "MethodCall at line 12" and "MethodCall each (self)" count as one reason; the rest is the detail -light_kind = ->(m) { m[:light_why].to_s.sub(/ at line \d+\z/, '') } +# "MethodCall at line 12: GlobalVariable" is the statement (MethodCall), where it is, and the construct inside it that +# IsScopeFree refused (GlobalVariable): the first table counts statements, the second constructs, whatever statement +# holds them - the statement alone did not say what to teach the frames next +light_kind = ->(m) { m[:light_why].to_s.sub(/ at line \d+(: .*)?\z/m, '') } +light_construct = ->(m) { m[:light_why].to_s[/ at line \d+: (.*)\z/m, 1] } jit_detail = lambda do |m| why, kind = m[:jit_why].to_s, m[:jit_kind].to_s why.start_with?(kind) ? why[kind.size..].sub(/\A[: ]+/, '') : why @@ -98,6 +101,7 @@ table.('JIT never: what refused the method', never, ->(m) { "#{m[:jit_kind]} (#{m[:jit_stage]})" }, jit_detail) table.('JIT maybe: where the typed body stopped without a profile', by_jit.fetch('maybe', []), ->(m) { m[:jit_kind].to_s }, jit_detail) table.('not a light frame: why', methods.reject { |m| m[:light] }, light_kind) +table.('not a light frame: the construct', methods.reject { |m| m[:light] || light_construct.(m).nil? }, light_construct, light_kind) if list puts '', 'methods' From 52bafec4df27e9f98513841eb0a54bff7277c01d Mon Sep 17 00:00:00 2001 From: Largo Date: Thu, 19 Nov 2026 15:58:46 +0100 Subject: [PATCH 02/10] Light frames: build a refusal's reason only when it was asked for 16728eb6 made IsScopeFree record why it refuses a method, through Refuse(why, reason), for IR_LIGHT_FRAMES=why and the census. Three refusals concatenated their reason before calling Refuse, so the normal path, where why is null and which runs for every method definition, allocated a string on each of them: the final fall-through (node type, keyword arguments, IR_LIGHT_CALLS=0), the assignment to a target that is not an element, and a splat with IR_LIGHT_SPLAT=0. Each now returns false at once when why is null and builds the same text otherwise. The other refusals pass constant strings or pick between constants. Checked with a Release build: Util/perf/census.rb over Src/StdLib and the AWFY benchmarks writes a TSV byte-identical to the one from the build before this change, so the verdicts and the reason texts are the same. Co-Authored-By: Claude Opus 5.5 --- Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs b/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs index f8c23cf6..5081b08f 100644 --- a/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs +++ b/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs @@ -677,7 +677,8 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals, Strong } var item = assignment.Left as ArrayItemAccess; if (item == null || !LightCallsEnabled || item.Block != null) { - return Refuse(why, item == null ? "assignment to " + assignment.Left.GetType().Name : + // the reason is built only when it was asked for: this path runs for every method definition + return why != null && Refuse(why, item == null ? "assignment to " + assignment.Left.GetType().Name : LightCallsEnabled ? "assignment to an element with a block" : "assignment to an element (IR_LIGHT_CALLS=0)"); } return IsScopeFree(item.Array, blockLiterals, why) && AreScopeFree(item.Arguments, blockLiterals, why); @@ -739,7 +740,7 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals, Strong // is given (LightFrameRewriter.LightScopeCall) var splat = node as SplattedArgument; if (splat != null) { - return (LightSplatEnabled || Refuse(why, node.GetType().Name + " (IR_LIGHT_SPLAT=0)")) && IsScopeFree(splat.Argument, blockLiterals, why); + return (LightSplatEnabled || why != null && Refuse(why, node.GetType().Name + " (IR_LIGHT_SPLAT=0)")) && IsScopeFree(splat.Argument, blockLiterals, why); } // `inner(&block)': the Proc is the caller's own, or made by #to_proc through a conversion site that takes no // scope, and the call passes it as an argument (LightFrameRewriter.LightScopeCall) @@ -812,6 +813,9 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals, Strong return AreScopeFree(jump.Arguments, blockLiterals, why); } // a node of a kind not taken at all: its type says what it is, and a Hash says whether it is a call's keywords + if (why == null) { + return false; + } var hash = node as HashConstructor; return Refuse(why, node.GetType().Name + (hash != null && hash.IsKeywordArguments ? " (keyword arguments)" : "") + (LightCallsEnabled ? "" : " (IR_LIGHT_CALLS=0)")); From cea4d9a41edc1c44a0f117c1039db0afa00d7519 Mon Sep 17 00:00:00 2001 From: Largo Date: Thu, 19 Nov 2026 16:06:08 +0100 Subject: [PATCH 03/10] perf ledger: what keeps methods out of light frames, by construct Records perf/why, a find-out step: the census now names the construct inside the statement that IsScopeFree refused. Nothing moved, by design: 0 of 28,335 rows differ outside the reason text. Gap 9 gets the ranking that came out of it (super 1,013, a regex literal 851, a Hash of keyword arguments 805, a Hash literal 789...), what the 1,525 methods refused for their parameters are (1,243 have keywords), and where the prelude and AWFY stand, so the next step can pick a construct instead of a statement name. Co-Authored-By: Claude Fable 5.2 --- Util/perf/LEDGER.md | 47 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/Util/perf/LEDGER.md b/Util/perf/LEDGER.md index 923c28b2..b498a908 100644 --- a/Util/perf/LEDGER.md +++ b/Util/perf/LEDGER.md @@ -395,11 +395,34 @@ Profile (`Util/perf/profile.sh`, self time): Landed, perf/interp; a block that interpolates still makes its scope); Util/block-given-matrix.rb, Util/light-rest-matrix.rb, Util/block-pass-matrix.rb, Util/splat-pass-matrix.rb and Util/interpolation-matrix.rb are their matrices. - What the census names next (2026-11-19, 28,335 methods): "optional, splat or post - parameters" 1,525, `super` 755, `rescue`/`ensure` 475, and 167 candidates that still - need their scope after the rewrite (in the prelude `Enumerable#zip`, - `Enumerator::Lazy#zip`, `Enumerator#__size_from_target__`, `Process#detach`, - `Process.waitall`; nobody has looked at what refuses them). A method with both a literal block and a passed one still makes its scope + What the census names next (2026-11-19, 28,335 methods, 8,017 of them not light). + By its first table: "optional, splat or post parameters" 1,525, `rescue`/`ensure` 475, + and 167 candidates that still need their scope after the rewrite (in the prelude + `Enumerable#zip`, `Enumerator::Lazy#zip`, `Enumerator#__size_from_target__`, + `Process#detach`, `Process.waitall`; nobody has looked at what refuses them). The + parameter line is keywords: 1,243 of the 1,525 have keyword parameters (20 of them the + prelude's, none AWFY's), 248 an optional parameter defaulting to `{}`, 2 a post + parameter (counted by parsing the 1,525 with Prism). + The other 5,704 are refused by a statement of the body, and since perf/why (Landed) + the census's second table says for which construct inside it: `super` 1,013, a regex + literal 851, a Hash of keyword arguments (`f(a: 1)`) 805, a Hash literal 789, a + parallel assignment 617, a `begin ... end` block 492, `break` 261 (255 of them + in a `while`), `defined?` 184, a global variable 147, a Range literal in a body with + literal blocks 132, a class variable 81, a qualified constant 75. Before that these + were spread over "MethodCall" 1,106, "IfExpression" 1,022, "SimpleAssignmentExpression" + 993 and five more statement names, which said nothing. + Where it matters: the prelude's 233 are a Hash literal 33 (`tally`, `group_by`, + `uniq`), a `begin` block 25, keyword parameters 24 (and 20 more under the parameter + line), a parallel assignment 23, `defined?` + 22, a Hash of keyword arguments 17, `super` 16, a regex 11; AWFY's 21 are a Range + literal that receives `each` with a block 12 (`Vector#each`, `Sieve#sieve`; the scope + is made for the block anyway, so letting the Range in buys nothing by itself), `super` + 6 (five DeltaBlue constraints' `initialize`, Richards' TaskControlBlock), a + `begin ... end while` 2 (Json's `read_object`, `read_array`) and a regex 1. Not looked into, from reading + `IsScopeFree`: a Hash literal is refused outright where an Array literal is taken + element by element, with no comment saying why; a regex literal is refused by itself + although `$~` is written by the call that matches; `defined?(@x)` is refused. + A method with both a literal block and a passed one still makes its scope for the passed one's call (LightScopeCall leaves calls with a block to BlockLiterals). 10. **`!=` outside the method JIT is two dispatches**: `BasicObject#!=` calls `==` @@ -552,6 +575,20 @@ Profile (`Util/perf/profile.sh`, self time): ## Landed +- **perf/why (/perf-step, 2026-11-19) - a find-out step: the census names the construct + that keeps a method out of light frames.** `IR_LIGHT_FRAMES=why` and the census said + only which statement of a body was not scope-free (`IfExpression at line 12`), so its + three largest lines after the parameters were statement names, and nobody could say + which decision of `IsScopeFree` kept 5,704 methods out. `IsScopeFree` now takes a box + for the reason, null on the normal path, and a refusal records what it refused + (`IfExpression at line 12: GlobalVariable`); the reason's text is built only when asked + for. `census.rb` has a second table, "not a light frame: the construct". Nothing was + meant to move and nothing did: of the 28,335 rows of the census 0 differ outside + `light_why`, and the 5,704 that had a line number are the old text plus the construct + (my own comparison of the two TSVs; the merged build's census is the worktree's byte + for byte). The interpolation, block-pass and splat-pass matrices print CRuby's output + on the merged build. Not run: IronRuby.Tests, a sweep, any timing; the texts that only + appear with a switch off. What it found is under gap 9. - **perf/interp (/perf-step, 2026-11-19) - a method that interpolates runs without a scope** (`"a#{x}"`, `raise ArgumentError, "negative size (#{n})"`; `IR_LIGHT_INTERPOLATION=0` turns it off). The filter (`IsScopeFree`) has taken an From 4ea82a6fa5872a319a34e97a72722823f3393c79 Mon Sep 17 00:00:00 2001 From: Largo Date: Thu, 19 Nov 2026 23:08:34 +0100 Subject: [PATCH 04/10] Light frames: let in a method with a Hash literal A method whose body has a Hash literal (`{a: x}', `{"k" => v}', `{}') made a RubyMethodScope on every call: IsScopeFree had no case for HashConstructor, so the literal reached its final refusal. The census counted 789 methods kept out of light frames by it, 33 of them in the prelude (group_by, uniq, to_h, transform_keys, deconstruct_keys...). The helpers a literal compiles to (RubyOps.MakeHash, MakeSymbolHash1..3) take the scope only for its runtime's equality comparer, and a String key's frozen copy and the keys' #hash and #eql? need nothing else of it. IsScopeFree now takes a literal whose keys and values are scope-free, and the rewriter passes those helpers the declaring scope, as it does for instance variables and constants. `**h' in a literal is already a call of Hash#merge. The hash a call site builds for its keywords (`f(a: x)') is still refused. IR_LIGHT_HASH=0 restores the old behaviour. Checked with Util/hash-literal-matrix.rb (new): the same output under CRuby 4.0, the unchanged build, this build, IR_LIGHT_HASH=0 and -X:NoAdaptiveCompilation. The interpolation, splat-pass, block-pass, light-rest and block-given matrices print the same on all three. Census over the standard library and AWFY: light frames 20318 -> 21125 (+807, none lost), light blocks 7389 -> 7436, JIT verdicts unchanged; a Richards run agrees with it. Instructions per call (ab.rb -i): `{a: x}' 1663 -> 1360, `{}' filled 2191 -> 1964, a String key 2007 -> 1797, a control without a Hash unchanged. Co-Authored-By: Claude Opus 5.5 --- .../Ast/Definitions/MethodDefinition.cs | 33 +- Util/hash-literal-matrix.rb | 318 ++++++++++++++++++ 2 files changed, 348 insertions(+), 3 deletions(-) create mode 100644 Util/hash-literal-matrix.rb diff --git a/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs b/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs index 5081b08f..6ed2d450 100644 --- a/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs +++ b/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs @@ -427,6 +427,12 @@ public MethodDefinition(LexicalScope/*!*/ definedScope, Expression target, strin /// internal static readonly bool LightInterpolationEnabled = Environment.GetEnvironmentVariable("IR_LIGHT_INTERPOLATION") != "0"; + /// + /// IR_LIGHT_HASH=0 keeps every method with a Hash literal (`{a: x}', `{}') out of light frames (see IsScopeFree and + /// LightFrameRewriter.ScopeOperandIndex). + /// + internal static readonly bool LightHashEnabled = Environment.GetEnvironmentVariable("IR_LIGHT_HASH") != "0"; + private static readonly bool LightFramesWhy = Environment.GetEnvironmentVariable("IR_LIGHT_FRAMES") == "why"; private static readonly bool LightFramesVerbose = LightFramesWhy || Environment.GetEnvironmentVariable("IR_LIGHT_FRAMES") == "verbose"; @@ -692,6 +698,22 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals, Strong if (array != null) { return AreScopeFree(array.Arguments, blockLiterals, why); } + // `{a: x}', `{"k" => v}', `{}': the helper a literal is made by takes the scope only for its runtime + // (LightFrameRewriter.ScopeOperandIndex), and a String key's frozen copy and the keys' #hash and #eql? are + // the runtime's too. `**h' in a literal is a call of Hash#merge on it. The hash a call site builds for its + // keywords (`f(a: x)') is not taken. + var hash = node as HashConstructor; + if (hash != null && !hash.IsKeywordArguments) { + if (!LightHashEnabled) { + return Refuse(why, "HashConstructor (IR_LIGHT_HASH=0)"); + } + foreach (var maplet in hash.Maplets) { + if (!IsScopeFree(maplet.Key, blockLiterals, why) || !IsScopeFree(maplet.Value, blockLiterals, why)) { + return false; + } + } + return true; + } // `(a..b)': the Range is made from its bounds and the literal's comparison storage (RubyOps.CreateInclusiveRange); // Range#initialize checks the stack before the <=> it calls for bounds that are not two Integers. Not in a body // with literal blocks: its light frame makes the scope before the Proc all the same, and SOM's @@ -816,8 +838,8 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals, Strong if (why == null) { return false; } - var hash = node as HashConstructor; - return Refuse(why, node.GetType().Name + (hash != null && hash.IsKeywordArguments ? " (keyword arguments)" : "") + + var keywords = node as HashConstructor; + return Refuse(why, node.GetType().Name + (keywords != null && keywords.IsKeywordArguments ? " (keyword arguments)" : "") + (LightCallsEnabled ? "" : " (IR_LIGHT_CALLS=0)")); } @@ -950,7 +972,8 @@ internal LightFrameRewriter(MSA.ParameterExpression/*!*/ scope, MSA.Expression/* /// The operand of a helper that takes the scope only for what a method scope shares with the scope the method /// was defined in, so the declaring scope can be passed instead: the runtime (instance variables), and the /// lexical chain a constant is looked up in - a method scope has no module of its own, so the lookup of one - /// starts at its parent, which is the declaring scope. -1 for any other helper. + /// starts at its parent, which is the declaring scope. And the helpers that make a Hash literal (HashConstructor), + /// which read only the runtime's equality comparer from it. -1 for any other helper. /// private static int ScopeOperandIndex(MethodInfo/*!*/ method) { if (method == Methods.GetInstanceVariable || method == Methods.IsDefinedInstanceVariable || @@ -963,6 +986,10 @@ private static int ScopeOperandIndex(MethodInfo/*!*/ method) { if (method == Methods.SetInstanceVariable) { return 2; } + if (LightHashEnabled && (method == Methods.MakeHash || method == Methods.MakeSymbolHash1 || + method == Methods.MakeSymbolHash2 || method == Methods.MakeSymbolHash3)) { + return 0; + } return -1; } diff --git a/Util/hash-literal-matrix.rb b/Util/hash-literal-matrix.rb new file mode 100644 index 00000000..39f3346f --- /dev/null +++ b/Util/hash-literal-matrix.rb @@ -0,0 +1,318 @@ +# Differential matrix for methods whose body has a Hash literal ({a: x}, {"k" => v}, {}) and run without a +# scope (a light frame, MethodDefinition.IsLightFrameCandidate). Run it under CRuby and under ./ir.sh with such +# frames on and off, and with the JIT off, and diff: +# +# ruby Util/hash-literal-matrix.rb > /tmp/mri.txt +# ./ir.sh Util/hash-literal-matrix.rb > /tmp/on.txt +# IR_LIGHT_HASH=0 ./ir.sh Util/hash-literal-matrix.rb > /tmp/off.txt +# ./ir.sh -X:NoAdaptiveCompilation Util/hash-literal-matrix.rb > /tmp/interp.txt +# diff /tmp/mri.txt /tmp/on.txt && diff /tmp/mri.txt /tmp/off.txt && diff /tmp/mri.txt /tmp/interp.txt +# +# Every probe runs N times and reports each distinct answer once, so the interpreted body, the compiled one and +# a site whose rule is already bound all meet the case: each way a literal is compiled (empty, Symbol keys up to +# three and more, String keys, other keys, `**h'), the order keys and values are evaluated and hashed in, +# duplicate keys, the String key's frozen copy, the result being a new plain Hash on every call (no default, +# no compare_by_identity, mutation not seen by the next call), literals in blocks, with yields, in defaults, in +# loops, recursion, exceptions and their backtraces, defined?, tracing, threads, and the keyword hash a call +# site builds (`f(a: x)') next to the literal it is not. +# +# IR_LIGHT_FRAMES=why ./ir.sh Util/hash-literal-matrix.rb 2>&1 >/dev/null | grep -a '^\[light\]' +# +# says which of the methods below are light frames. + +N = 3000 + +def t(label) + print label.ljust(52), ' => ' + seen = [] + begin + N.times do |i| + v = yield i + seen << v unless seen.include?(v) + end + puts seen.map(&:inspect).join(', ') + rescue Exception => e + puts "#{seen.map(&:inspect).join(', ')} then #{e.class}: #{e.message[0, 70]}" + end +end + +def label(line) = line[/'(?:[^']*[#.])?([^']*)'/, 1] + +# ---------------------------------------------------------------- 1. each way a literal is compiled + +def h_empty = {} +def h_sym1(x) = { a: x } +def h_sym2(x, y) = { a: x, b: y } +def h_sym3(x, y, z) = { a: x, b: y, c: z } +def h_sym4(x) = { a: x, b: x, c: x, d: x } +def h_str(x) = { "k" => x } +def h_str2(x) = { "k" => x, "l" => x } +def h_int(x) = { 1 => x, 2.5 => x, nil => x } +def h_mixed(x) = { a: x, "b" => x, 3 => x } +def h_key(k, v) = { k => v } +def h_keys(k1, k2) = { k1 => 1, k2 => 2 } +def h_short(a, b) = { a:, b: } +def h_nested(x) = { a: { b: { c: x } }, d: [x, { e: x }] } +def h_dsplat(o) = { a: 1, **o, b: 2 } +def h_dsplat_only(o) = { **o } +def h_dsym(x) = { "a#{x}": 1 } +def h_ivar(x) = (@h = { v: x }; @h) +H_CONST = { k: 1 } +def h_const(x) = { c: H_CONST, x: x } + +class ToHash; def to_hash = { th: 1 }; end +class NotHash; end + +t('{}') { h_empty } +t('{} is a Hash') { h_empty.class } +t('{a: x}') { h_sym1(1) } +t('{a: x, b: y}') { h_sym2(1, nil) } +t('{a:, b:, c:}') { h_sym3(1, 2, 3) } +t('four Symbol keys') { h_sym4(:v) } +t('a String key') { h_str(1) } +t('two String keys') { h_str2(1) } +t('Integer, Float and nil keys') { h_int(1) } +t('mixed keys') { h_mixed(1) } +t('{a:, b:} shorthand') { h_short(1, 2) } +t('nested') { h_nested(1) } +t('**h') { h_dsplat({ c: 3 }) } +t('**h overrides and is overridden') { h_dsplat({ a: 9, b: 9 }) } +t('**nil') { h_dsplat(nil) } +t('**to_hash') { h_dsplat(ToHash.new) } +t('**not a hash') { begin; h_dsplat(NotHash.new); rescue TypeError => e; e.message.sub(/0x\h+/, 'X'); end } +t('{**h} is a copy') { h = { a: 1 }; r = h_dsplat_only(h); [r, r.equal?(h)] } +t('dynamic Symbol key') { h_dsym(1) } +t('stored in an ivar') { h_ivar(1) } +t('a constant value') { h_const(1) } + +# ---------------------------------------------------------------- 2. order and keys + +$log = [] +def logged(x) = ($log << x; x) +class LoudKey + attr_reader :n + def initialize(n) = @n = n + def hash = ($log << "hash#{@n}"; @n.hash) + def eql?(o) = ($log << "eql#{@n}"; o.is_a?(LoudKey) && o.n == @n) + def inspect = "K#{@n}" +end +def o_order = { logged(:k1) => logged(1), logged(:k2) => logged(2), logged(:k3) => logged(3) } +def o_order4 = { logged("k1") => logged(1), logged(:k2) => logged(2), logged(3) => logged(3), logged(:k4) => logged(4) } +def o_loud(a, b) = { a => logged(:va), b => logged(:vb) } +def o_dup(k) = { k => 1, :x => 2, k => 3 } +def o_dup_str(a, b) = { a => 1, b => 2 } + +# the log is copied and the Hash inspected right after the call: comparing answers calls #hash and #eql? again +t('evaluation order, three') { $log = []; r = o_order; [r, $log.dup] } +t('evaluation order, four') { $log = []; r = o_order4; [r, $log.dup] } +t('hash and eql? order') { $log = []; r = o_loud(LoudKey.new(1), LoudKey.new(2)); [r.inspect, $log.dup] } +t('equal keys: hash and eql?') { $log = []; r = o_loud(LoudKey.new(1), LoudKey.new(1)); [r.inspect, $log.dup] } +t('duplicate keys keep the last value') { o_dup(:x) } +t('duplicate keys keep the first key') { a = +"k"; b = +"k"; r = o_dup_str(a, b); [r, r.keys[0].equal?(a)] } +t('a String key is a frozen copy') { s = +"key"; r = h_key(s, 1); k = r.keys[0]; [k.frozen?, k.equal?(s), s.frozen?, (s << "!")] } +t('a frozen String key is kept') { s = "fk".freeze; h_key(s, 1).keys[0].equal?(s) } +# (not whether it is frozen: IronRuby freezes a String subclass key, CRuby 4.0 does not, light frame or not) +t('a String subclass key') { c = Class.new(String); h_key(c.new("x"), 1).keys[0].class == c } +t('an Array key') { a = [1]; r = h_key(a, 2); [r, r.keys[0].equal?(a)] } +t('a NaN key') { r = h_key(Float::NAN, 1); [r.size, r.key?(Float::NAN)] } +t('0 and 0.0 keys') { h_keys(0, 0.0) } +t('1 and 1r keys') { h_keys(1, 1r) } + +# ---------------------------------------------------------------- 3. the result + +def r_fill(n); h = {}; i = 0; while i < n; h[i] = i * i; i += 1; end; h; end +def r_mutate; h = { a: 1 }; h[:b] = 2; h; end +def r_cbi; h = {}; h.compare_by_identity; h; end +def r_cbi_after; {} ; end + +t('a new Hash on every call') { h_sym1(1).equal?(h_sym1(1)) } +t('{} is new on every call') { h_empty.equal?(h_empty) } +t('mutation does not leak') { r = h_sym1(1); r[:z] = 1; h_sym1(1) } +t('mutation of {} does not leak') { r = h_empty; r[:z] = 1; h_empty } +t('filled in a loop') { r_fill(5) } +t('mutated before return') { r_mutate } +t('compare_by_identity not inherited') { r_cbi.compare_by_identity?; r_cbi_after.compare_by_identity? } +t('no default, no default_proc') { r = h_sym1(1); [r.default, r.default_proc, r[:missing]] } +t('not frozen') { [h_sym1(1).frozen?, h_empty.frozen?, h_str(1).frozen?] } +t('ordering') { h_sym3(3, 2, 1).to_a } +t('Symbol keys look up') { r = h_sym4(1); [r[:a], r[:d], r.key?(:c), r.fetch(:b)] } +t('String keys look up') { r = h_str2(1); [r["k"], r["l"], r.key?("k")] } +t('rehash after mutating an Array key') { a = [1]; r = h_key(a, 1); a << 2; [r[a], r.rehash[a]] } +t('instance_variables of the result') { h_sym1(1).instance_variables } +t('singleton class of the result') { r = h_sym1(1); def r.x = 1; [r.x, h_sym1(1).respond_to?(:x)] } + +# ---------------------------------------------------------------- 4. where it is written + +def w_block(a) = a.map { |x| { x => x } } +def w_block_out(a, y) = a.map { |x| { x => y } } +def w_block_and_hash(a) = [{ n: a.size }, a.map { |x| x * 2 }] +def w_yield(x) = { a: yield(x) } +def w_yield2(x) = (h = { a: x }; yield h; h) +def w_default(x, h = {}) = (h[:x] = x; h) +def w_default_sym(x, h = { d: 1 }) = (h[:x] = x; h) +def w_while(n); r = []; i = 0; while i < n; r << { i: i }; i += 1; end; r; end +def w_case(x) + case x + when Integer then { int: x } + when String then { str: x } + else {} + end +end +def w_if(x) = x ? { t: x } : { f: x } +def w_and(x) = x && { v: x } +def w_return(x); return { early: x } if x > 0; { late: x }; end +def w_arg(x) = w_take({ a: x }) +def w_take(h) = h +def w_recv(x) = { a: x }.merge({ b: x }) +def w_index(x) = { a: x }[:a] +def w_send(x) = send(:h_sym1, x) +def w_attr(o, x) = (o.v = { v: x }; o.v) +S = Struct.new(:v) +def w_elem(h, x) = (h[:k] = { v: x }; h) +def w_range(x) = { r: (1..x) } +def w_str(x) = { s: "a#{x}" } +def w_arr(x) = [{ a: x }, [{ b: x }]] +def w_recur(n) = n == 0 ? {} : { n => w_recur(n - 1) } +def w_proc(x) = proc { { p: x } } +def w_lambda(x) = -> { { l: x } } +define_method(:w_dm) { |x| { dm: x } } +def w_binding(x) = ({ b: binding.local_variable_get(:x) }) +def w_lvars(x) = { l: local_variables } +def w_method(x) = { m: __method__ } +def w_bg(x) = { bg: block_given? } +def w_caller(x) = { c: caller(0, 1).map { |l| label(l) } } +def w_eval(x) = eval("{ e: x }") +def w_inst_eval(x) = instance_eval { { ie: x } } + +t('in a block') { w_block([1, 2]) } +t('in a block, reading an outer local') { w_block_out([1, 2], :y) } +t('beside a literal block') { w_block_and_hash([1, 2]) } +t('with yield') { w_yield(2) { |v| v * 3 } } +t('yields the hash it returns') { w_yield2(1) { |h| h[:y] = 1 } } +t('{} default') { [w_default(1), w_default(2)] } +t('{} default given') { h = {}; [w_default(1, h).equal?(h), h] } +t('{d: 1} default') { [w_default_sym(1), w_default_sym(2)] } +t('in a while loop') { w_while(3) } +t('in case/when') { [w_case(1), w_case("s"), w_case(nil)] } +t('in ?:') { [w_if(1), w_if(nil)] } +t('in &&') { [w_and(1), w_and(nil)] } +t('early return') { [w_return(1), w_return(-1)] } +t('as an argument') { w_arg(1) } +t('as a receiver') { w_recv(1) } +t('indexed') { w_index(1) } +t('through send') { w_send(1) } +t('attribute assignment') { w_attr(S.new, 1) } +t('element assignment') { w_elem({}, 1) } +t('with a Range') { w_range(2) } +t('with an interpolation') { w_str(2) } +t('in an Array literal') { w_arr(1) } +t('recursion') { w_recur(3) } +t('in a proc called later') { w_proc(1).call } +t('in a lambda called later') { w_lambda(1).call } +t('define_method') { w_dm(1) } +t('with binding') { w_binding(1) } +t('with local_variables') { w_lvars(1) } +t('with __method__') { w_method(1) } +t('with block_given?') { [w_bg(1), w_bg(1) {}] } +t('with caller') { w_caller(1) } +t('with eval') { w_eval(1) } +t('with instance_eval') { w_inst_eval(1) } + +# ---------------------------------------------------------------- 5. errors, defined?, tracing, threads + +class HashRaises; def hash = raise(IndexError, "hash raised"); end +def e_value(x) = { a: x.fetch(9) } +def e_key(x) = { x.fetch(9) => 1 } +def e_loud(k) = { k => 1 } +def e_four(x) = { a: 1, b: 2, c: 3, d: x.fetch(9) } +def e_str(x) = { "s" => x.fetch(9) } +# (not of a literal whose value calls a missing method: CRuby answers nil, IronRuby "expression", light frame or not) +def d_lit(x) = defined?({ a: x }) +def d_empty = defined?({}) +def traced(x) = { tr: x } + +t('raise in a value') { begin; e_value([]); rescue IndexError => e; [e.message, e.backtrace[0, 3].map { |l| label(l) }]; end } +t('raise in a key') { begin; e_key([]); rescue IndexError => e; [e.message, e.backtrace[0, 3].map { |l| label(l) }]; end } +t('raise in #hash of a key') { begin; e_loud(HashRaises.new); rescue IndexError => e; [e.message, e.backtrace[1, 2].map { |l| label(l) }]; end } +t('raise in a fourth value') { begin; e_four([]); rescue IndexError => e; [e.message, e.backtrace[1, 2].map { |l| label(l) }]; end } +t('raise in a String-keyed value') { begin; e_str([]); rescue IndexError => e; [e.message, e.backtrace[1, 2].map { |l| label(l) }]; end } +t('raise line') { begin; e_value([]); rescue IndexError => e; e.backtrace_locations[1].lineno - __LINE__; end } +t('defined? of a literal') { d_lit(1) } +t('defined? {}') { d_empty } +t('TracePoint :call sees it') do + seen = [] + tp = TracePoint.new(:call) { |e| seen << e.method_id if e.method_id == :traced } + tp.enable { traced(1) } + seen +end +t('TracePoint :call, binding') do + seen = [] + tp = TracePoint.new(:call) { |e| seen << e.binding.local_variable_get(:x) if e.method_id == :traced } + tp.enable { traced(7) } + seen +end +t('TracePoint :c_call in a value') do + seen = [] + tp = TracePoint.new(:c_call) { |e| seen << e.method_id if e.method_id == :fdiv } + tp.enable { h_sym1(1.fdiv(2)) } + seen +end +t('after the TracePoint') { traced(-5) } +t('threads') do |i| + i % 100 == 0 ? Array.new(4) { |k| Thread.new { Array.new(50) { |j| h_sym2(k, j) }.last } }.map(&:value) : true +end + +# ---------------------------------------------------------------- 6. the keyword hash of a call site + +def kw(a:, b: 2) = [a, b] +def kw_rest(**o) = o +def kw_opt(a: 1) = a +def pos(h) = h +def pos_rest(*r) = r +def pos_opt(x = :none, h = {}) = [x, h] +ruby2_keywords def r2k(*args) = args +def k_call(x) = kw(a: x) +def k_call2(x) = kw(a: x, b: x) +def k_rest(x) = kw_rest(a: x, "s" => x) +def k_pos(x) = pos(a: x) +def k_pos_rest(x) = pos_rest(1, a: x) +def k_opt(x) = pos_opt(a: x) +def k_dsplat(h) = kw(**h) +def k_dsplat_more(h) = kw_rest(z: 1, **h) +def k_empty_dsplat(h) = pos_rest(**h) +def k_r2k(x) = r2k(a: x) +def k_lit_vs_kw(x) = [pos_rest({ a: x }), pos_rest(a: x)] +def k_kw_opt(x) = kw_opt(a: x) +def k_unknown(x) = kw(a: x, c: x) +def k_send(x) = send(:kw, a: x) +def k_yield(x) = yield(a: x) +def k_block(x) = [x].map { |v| kw(a: v) } +def k_lib(s) = s.encode("US-ASCII", undef: :replace, invalid: :replace) +def k_struct(x) = KS.new(a: x) +KS = Struct.new(:a, keyword_init: true) +def k_dsplat_nil(x) = kw_rest(**nil) +class KMM; def method_missing(n, *a, **k) = [n, a, k]; def respond_to_missing?(*) = true; end +def k_mm(o, x) = o.anything(a: x) + +t('f(a: x)') { k_call(1) } +t('f(a: x, b: y)') { k_call2(1) } +t('f(a: x, "s" => x) to **o') { k_rest(1) } +t('f(a: x) to a positional') { r = k_pos(1); [r, r.frozen?] } +t('f(1, a: x) to *r') { k_pos_rest(1) } +t('f(a: x) to optionals') { k_opt(1) } +t('f(**h)') { k_dsplat({ a: 1 }) } +t('f(**h) does not change h') { h = { a: 1 }; k_dsplat(h); h } +t('f(z: 1, **h)') { h = { y: 2 }; [k_dsplat_more(h), h] } +t('f(**{}) passes nothing') { k_empty_dsplat({}) } +t('ruby2_keywords') { r = k_r2k(1); [r, Hash.ruby2_keywords_hash?(r.last)] } +t('literal and keywords to *r') { k_lit_vs_kw(1) } +t('optional keyword') { k_kw_opt(5) } +t('unknown keyword') { begin; k_unknown(1); rescue ArgumentError => e; e.message; end } +t('send with keywords') { k_send(1) } +t('yield with keywords') { k_yield(1) { |**k| k } } +t('yield keywords to a positional') { k_yield(1) { |h| h } } +t('keywords in a block') { k_block(1) } +t('keywords to a library method') { k_lib("aƩ") } +t('keyword_init Struct') { k_struct(1).a } +t('**nil') { k_dsplat_nil(1) } +t('keywords to method_missing') { k_mm(KMM.new, 1) } From 63c0a9421504d2e37b38a7fdc091333ca8fa0a6f Mon Sep 17 00:00:00 2001 From: Largo Date: Thu, 19 Nov 2026 23:18:06 +0100 Subject: [PATCH 05/10] Light frames: let in a method that passes keywords A method that calls with keywords (`f(a: x)') made a RubyMethodScope on every call: the hash the call site builds for them is a HashConstructor marked as keyword arguments, which IsScopeFree refused. The census counted 805 methods kept out of light frames by it, 17 in the prelude (tally, uniq!, fetch, transform_keys!, the frozen checks that raise FrozenError with `receiver:'). That hash is made by helpers (RubyOps.MakeKeywordArgumentsHash, ...Hash0..3) that, like the literal's, take the scope only for its runtime's equality comparer, and it is an argument of the call like any other: a call from a light frame evaluates it once, and makes it again through the ordinary site only if the rule asked for the scope before calling anything. IsScopeFree now takes it and the rewriter passes those helpers the declaring scope. `f(**h)' (a KeywordSplatMerge) and the defaults of keyword parameters are not taken. IR_LIGHT_KEYWORD_HASH=0 restores the old behaviour. Checked with Util/hash-literal-matrix.rb, whose last section calls with keywords (to required, optional and ** keywords, a positional, *rest, ruby2_keywords, send, yield, method_missing, a library method, a keyword_init Struct): the same output under CRuby 4.0, this build, IR_LIGHT_KEYWORD_HASH=0 and -X:NoAdaptiveCompilation; the five other light-frame matrices too. Census over the standard library and AWFY: light frames 21125 -> 21793 (+668, none lost), light blocks 7436 -> 7724, JIT verdicts unchanged; a Richards run agrees with it. Instructions per call (ab.rb -i): `f(a: x)' 1164 -> 893, a call with two keywords to **o 2689 -> 2466. Co-Authored-By: Claude Opus 5.5 --- .../Ast/Definitions/MethodDefinition.cs | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs b/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs index 6ed2d450..88dda451 100644 --- a/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs +++ b/Src/Ruby/Compiler/Ast/Definitions/MethodDefinition.cs @@ -433,6 +433,12 @@ public MethodDefinition(LexicalScope/*!*/ definedScope, Expression target, strin /// internal static readonly bool LightHashEnabled = Environment.GetEnvironmentVariable("IR_LIGHT_HASH") != "0"; + /// + /// IR_LIGHT_KEYWORD_HASH=0 keeps every method that passes keywords (`f(a: x)') out of light frames (see IsScopeFree and + /// LightFrameRewriter.ScopeOperandIndex). + /// + internal static readonly bool LightKeywordHashEnabled = Environment.GetEnvironmentVariable("IR_LIGHT_KEYWORD_HASH") != "0"; + private static readonly bool LightFramesWhy = Environment.GetEnvironmentVariable("IR_LIGHT_FRAMES") == "why"; private static readonly bool LightFramesVerbose = LightFramesWhy || Environment.GetEnvironmentVariable("IR_LIGHT_FRAMES") == "verbose"; @@ -701,11 +707,12 @@ private static bool IsScopeFree(Expression/*!*/ node, bool blockLiterals, Strong // `{a: x}', `{"k" => v}', `{}': the helper a literal is made by takes the scope only for its runtime // (LightFrameRewriter.ScopeOperandIndex), and a String key's frozen copy and the keys' #hash and #eql? are // the runtime's too. `**h' in a literal is a call of Hash#merge on it. The hash a call site builds for its - // keywords (`f(a: x)') is not taken. + // keywords (`f(a: x)') is made the same way, only marked as keywords, and is an argument of the call like + // any other; `f(**h)' is a KeywordSplatMerge, not taken. var hash = node as HashConstructor; - if (hash != null && !hash.IsKeywordArguments) { - if (!LightHashEnabled) { - return Refuse(why, "HashConstructor (IR_LIGHT_HASH=0)"); + if (hash != null) { + if (hash.IsKeywordArguments ? !LightKeywordHashEnabled : !LightHashEnabled) { + return Refuse(why, hash.IsKeywordArguments ? "HashConstructor (keyword arguments, IR_LIGHT_KEYWORD_HASH=0)" : "HashConstructor (IR_LIGHT_HASH=0)"); } foreach (var maplet in hash.Maplets) { if (!IsScopeFree(maplet.Key, blockLiterals, why) || !IsScopeFree(maplet.Value, blockLiterals, why)) { @@ -972,8 +979,8 @@ internal LightFrameRewriter(MSA.ParameterExpression/*!*/ scope, MSA.Expression/* /// The operand of a helper that takes the scope only for what a method scope shares with the scope the method /// was defined in, so the declaring scope can be passed instead: the runtime (instance variables), and the /// lexical chain a constant is looked up in - a method scope has no module of its own, so the lookup of one - /// starts at its parent, which is the declaring scope. And the helpers that make a Hash literal (HashConstructor), - /// which read only the runtime's equality comparer from it. -1 for any other helper. + /// starts at its parent, which is the declaring scope. And the helpers that make a Hash literal or a call's keyword + /// hash (HashConstructor), which read only the runtime's equality comparer from it. -1 for any other helper. /// private static int ScopeOperandIndex(MethodInfo/*!*/ method) { if (method == Methods.GetInstanceVariable || method == Methods.IsDefinedInstanceVariable || @@ -990,6 +997,11 @@ private static int ScopeOperandIndex(MethodInfo/*!*/ method) { method == Methods.MakeSymbolHash2 || method == Methods.MakeSymbolHash3)) { return 0; } + if (LightKeywordHashEnabled && (method == Methods.MakeKeywordArgumentsHash || method == Methods.MakeKeywordArgumentsHash0 || + method == Methods.MakeKeywordArgumentsHash1 || method == Methods.MakeKeywordArgumentsHash2 || + method == Methods.MakeKeywordArgumentsHash3)) { + return 0; + } return -1; } From 80301591023e52afd490154a5eb7b3e2b72442de Mon Sep 17 00:00:00 2001 From: Largo Date: Thu, 19 Nov 2026 23:33:01 +0100 Subject: [PATCH 06/10] perf ledger: Hash literals and a call's keywords in light frames Records the perf/hashlit step under Landed, as census only and not gated: light frames 20,318 -> 21,793 of 28,335 methods, 1,475 moved up, none down. Gap 9 no longer lists the Hash literal and has the census's ranking of what keeps the remaining 6,542 methods out. Gap 22 is new: two differences from CRuby 4.0 that the step's probes found on the build before it (a String subclass instance as a Hash key is copied and frozen; defined? of a Hash literal with an undefined method in it). Co-Authored-By: Claude Fable 5.2 --- Util/perf/LEDGER.md | 91 ++++++++++++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 26 deletions(-) diff --git a/Util/perf/LEDGER.md b/Util/perf/LEDGER.md index b498a908..efe72e0f 100644 --- a/Util/perf/LEDGER.md +++ b/Util/perf/LEDGER.md @@ -380,8 +380,7 @@ Profile (`Util/perf/profile.sh`, self time): SOM's `Vector#each` over 4 elements ran at 0.98-1.05 of before and over 100 at 1.00-1.15 (round nine). Closed. What DeltaBlue pays is in gap 14. 9. **Light frames still exclude** a `while` with a `break` in it (`Array#take_while`), - an empty Hash literal `{}` (found on `Array#to_h`; - `::Hash.new` is scope-free but calls a redefined Hash.new), `super` (6 hot `initialize` + `super` (6 hot `initialize` methods; also `Array#sum` without a block, 5.3x CRuby, whose rare path is `super`), `rescue`, keywords, `$~` and regex literals. `IR_LIGHT_FRAMES=why` lists them. Also a Range per `(a..b).each`, and @@ -392,35 +391,35 @@ Profile (`Util/perf/profile.sh`, self time): (`inner(&block)`, `map(&:sym)`; `IR_LIGHT_BLOCK_PASS=0`, see Landed, perf/blockpass), nor one that splats a value (`inner(*args)`, `[*a]`, `yield(*a)`; `IR_LIGHT_SPLAT=0`, see Landed, perf/splat), nor an interpolation (`"a#{x}"`; `IR_LIGHT_INTERPOLATION=0`, see - Landed, perf/interp; a block that interpolates still makes its scope); + Landed, perf/interp; a block that interpolates still makes its scope), nor a Hash + literal or the keywords of a call (`{a: x}`, `{}`, `f(a: x)`; `IR_LIGHT_HASH=0`, + `IR_LIGHT_KEYWORD_HASH=0`, see Landed, perf/hashlit; `f(**h)` is still out); Util/block-given-matrix.rb, Util/light-rest-matrix.rb, Util/block-pass-matrix.rb, - Util/splat-pass-matrix.rb and Util/interpolation-matrix.rb are their matrices. - What the census names next (2026-11-19, 28,335 methods, 8,017 of them not light). - By its first table: "optional, splat or post parameters" 1,525, `rescue`/`ensure` 475, - and 167 candidates that still need their scope after the rewrite (in the prelude - `Enumerable#zip`, `Enumerator::Lazy#zip`, `Enumerator#__size_from_target__`, - `Process#detach`, `Process.waitall`; nobody has looked at what refuses them). The - parameter line is keywords: 1,243 of the 1,525 have keyword parameters (20 of them the - prelude's, none AWFY's), 248 an optional parameter defaulting to `{}`, 2 a post - parameter (counted by parsing the 1,525 with Prism). - The other 5,704 are refused by a statement of the body, and since perf/why (Landed) - the census's second table says for which construct inside it: `super` 1,013, a regex - literal 851, a Hash of keyword arguments (`f(a: 1)`) 805, a Hash literal 789, a - parallel assignment 617, a `begin ... end` block 492, `break` 261 (255 of them - in a `while`), `defined?` 184, a global variable 147, a Range literal in a body with - literal blocks 132, a class variable 81, a qualified constant 75. Before that these - were spread over "MethodCall" 1,106, "IfExpression" 1,022, "SimpleAssignmentExpression" - 993 and five more statement names, which said nothing. - Where it matters: the prelude's 233 are a Hash literal 33 (`tally`, `group_by`, - `uniq`), a `begin` block 25, keyword parameters 24 (and 20 more under the parameter - line), a parallel assignment 23, `defined?` - 22, a Hash of keyword arguments 17, `super` 16, a regex 11; AWFY's 21 are a Range + Util/splat-pass-matrix.rb, Util/interpolation-matrix.rb and Util/hash-literal-matrix.rb + are their matrices. + What the census names next (2026-11-19 after perf/hashlit, 28,335 methods, 6,542 of + them not light). By its first table: keyword parameters 1,257 (37 of them the + prelude's, none AWFY's), `rescue`/`ensure` 478, 185 candidates that still need their + scope after the rewrite (in the prelude `Enumerable#zip`, `Enumerator::Lazy#zip`, + `Enumerator#__size_from_target__`, `Process#detach`, `Process.waitall`, + `__errno_class__`; nine of them were looked at in perf/hashlit and are a `rescue` + inside a literal block, `a.map { |x| begin; x; rescue; end }`, by the subagent's probe), and other optional, + splat or post parameters 176. + The rest are refused by a statement of the body, and the census's second table says + for which construct inside it: `super` 1,054, a regex literal 1,008, a parallel + assignment 661, a `begin ... end` block 523 (the table calls it Body), `break` 268 + (258 of them in a `while`), `defined?` 190, a global variable 158, a Range literal in a + body with literal blocks 138, a class variable 86, a qualified constant 76, a lambda + literal 47, a `rescue` modifier 43. (The lines grew when Hash literals left: a method + is counted under the first construct that refuses it.) + Where it matters: the prelude's 191 are keyword parameters 37, a `begin` block 27, a + parallel assignment 24, `defined?` 23, `super` 16, a regex 12, a `rescue` modifier 10; + AWFY's 21 are a Range literal that receives `each` with a block 12 (`Vector#each`, `Sieve#sieve`; the scope is made for the block anyway, so letting the Range in buys nothing by itself), `super` 6 (five DeltaBlue constraints' `initialize`, Richards' TaskControlBlock), a `begin ... end while` 2 (Json's `read_object`, `read_array`) and a regex 1. Not looked into, from reading - `IsScopeFree`: a Hash literal is refused outright where an Array literal is taken - element by element, with no comment saying why; a regex literal is refused by itself + `IsScopeFree`: a regex literal is refused by itself although `$~` is written by the call that matches; `defined?(@x)` is refused. A method with both a literal block and a passed one still makes its scope for the passed one's call (LightScopeCall leaves calls with a block to BlockLiterals). @@ -572,9 +571,49 @@ Profile (`Util/perf/profile.sh`, self time): without a scope. A TracePoint `:line` event is reported for the body of an endless `def`, CRuby reports none. And with a refinement of `Symbol#to_s` in use, `"#{:s}"` asks the refined `to_s`; CRuby answers `"s"` without asking it. +22. **Two differences from CRuby in Hash literals**, found by the probes of + `Util/hash-literal-matrix.rb` on the build before perf/hashlit and left as they were + (the matrix leaves them out, with a comment); not speed gaps. A key that is an + instance of a String subclass is copied and frozen (`k = MyS.new("k"); h = {k => 1}`: + `h.keys[0].frozen?` is true and it is not `k`); CRuby 4.0 keeps `k` itself, unfrozen, + and copies only a plain String. The same with `h[k] = 1`. And `defined?({a: 1.nope})` + is `"expression"` where CRuby says nil, as both say for `defined?([1.nope])`. ## Landed +- **perf/hashlit (/perf-step, 2026-11-19) - a method with a Hash literal, or one that + passes keywords, runs without a scope** (`{a: x}`, `{"k" => v}`, `{}`, `f(a: x)`; + `IR_LIGHT_HASH=0` and `IR_LIGHT_KEYWORD_HASH=0` turn each off). `IsScopeFree` had no + case for a HashConstructor, so one anywhere in a body sent the method to the final + refusal, while an Array literal was taken element by element. The helpers a literal + and a call's keyword hash compile to (`RubyOps.MakeHash`, `MakeSymbolHash1..3`, + `MakeKeywordArgumentsHash`, `MakeKeywordArgumentsHash0..3`) take the scope only for + the runtime's equality comparer, so the rewriter now passes them the declaring scope, + as it does for instance variables and constants (`ScopeOperandIndex`), and the filter + takes a Hash whose keys and values are scope-free. `**h` in a literal already was a + call of `Hash#merge`; `f(**h)` (KeywordSplatMerge) and the defaults of keyword + parameters are not taken. Two commits, the keywords second, so either reverts alone. + light frames 71.7% -> 76.9% (20,318 -> 21,793), light blocks 7,389 -> 7,724, JIT + compiles, maybe and never unchanged (Src/StdLib + AWFY, 28,335 methods; 1,475 methods + moved up, none down: 807 for the literal, 148 of those a `h = {}` default, and 668 for + the keywords) - **census only, not gated**. 42 of them are the prelude's, among them + `group_by`, `uniq`, `tally`, `uniq!`, six `to_h`, `transform_keys`, `slice`, `fetch`, + `to_set`, `transform_values!`, `__check_frozen__`; none is AWFY's, whose methods + write no Hash literal, so the AWFY table is not expected to move. + 131 of the 789 the literal kept out did not move with the first commit: another + construct keeps them (parallel assignment, a regex, `super`, a global variable). + Checked, by the subagent and again by me on its build: `Util/hash-literal-matrix.rb` + (new, 111 probes) prints CRuby 4.0's output on the build before, the new one, with + each switch off and both, with `-X:NoAdaptiveCompilation` and with `IR_LIGHT_FRAMES=0`; + the interpolation, splat-pass, block-pass, light-rest and block-given matrices print + CRuby's on both builds; `census.rb -c` against traced runs of Richards and Json says + 0 differ; the merged build's census is the worktree's row for row. + Instructions per call (`ab.rb -i`, the subagent's run, not repeated): `{a: x}` 1,663 + -> 1,360, `{a: x, b: x}` 1,761 -> 1,449, `{}` filled with two keys 2,220 -> 1,964, + `{"k" => x}` 2,010 -> 1,797, `f(a: x)` to a keyword 1,161 -> 893, two keywords to + `**o` 2,692 -> 2,466, a control without a Hash 910 -> 872 (within its spread). + Not run: IronRuby.Tests, a sweep, any timing. Found on the way: gap 22. + - **perf/why (/perf-step, 2026-11-19) - a find-out step: the census names the construct that keeps a method out of light frames.** `IR_LIGHT_FRAMES=why` and the census said only which statement of a body was not scope-free (`IfExpression at line 12`), so its From fc1ecfb5c5ad0046a1e3b8c262bdd73d677c8d4f Mon Sep 17 00:00:00 2001 From: Largo Date: Fri, 20 Nov 2026 12:10:37 +0100 Subject: [PATCH 07/10] Hash-literal matrix: visibility, a callee that changes, refinements The matrix had every way a Hash literal and a keyword call are compiled, but nothing about what the call a light frame makes must still respect. A frame that passes keywords calls with the frame in place of its scope, and such a call is the source's call only if it sees what the scope would have shown it. Sections 7 to 11 add 31 probes: private and protected callees from the same and from an unrelated class, method_missing, callees that read the caller's scope (eval with a binding, local_variables, block_given?, send, public_send, Method#call, a keyword_init Struct), a callee that is redefined, made private, given a singleton method, changed to take a positional Hash, removed, or replaced half way through the calls, a key whose #hash or #eql? changes half way, String#freeze and Hash.new redefined, and a caller defined where a refinement is in use. The refinement section is last: a method defined after `using' is never a light frame, so it would hide every probe after it. CRuby 4.0 and IronRuby print the same 148 lines, with light frames on and off, with IR_LIGHT_HASH=0 IR_LIGHT_KEYWORD_HASH=0, with -X:NoJIT -X:NoOSR and with -X:NoAdaptiveCompilation. IR_LIGHT_FRAMES=why names the new callers as light frames, so the probes run the path they are about. Co-Authored-By: Claude Fable 5.2 --- Util/hash-literal-matrix.rb | 151 +++++++++++++++++++++++++++++++++++- 1 file changed, 150 insertions(+), 1 deletion(-) diff --git a/Util/hash-literal-matrix.rb b/Util/hash-literal-matrix.rb index 39f3346f..480c82f6 100644 --- a/Util/hash-literal-matrix.rb +++ b/Util/hash-literal-matrix.rb @@ -14,7 +14,9 @@ # duplicate keys, the String key's frozen copy, the result being a new plain Hash on every call (no default, # no compare_by_identity, mutation not seen by the next call), literals in blocks, with yields, in defaults, in # loops, recursion, exceptions and their backtraces, defined?, tracing, threads, and the keyword hash a call -# site builds (`f(a: x)') next to the literal it is not. +# site builds (`f(a: x)') next to the literal it is not. Sections 7 to 11 are about the call a light frame makes +# with keywords: the callee's visibility, a callee that reads the caller's scope, the callee or a key's #hash +# changing half way through the N calls, and a refinement in use where the caller is defined. # # IR_LIGHT_FRAMES=why ./ir.sh Util/hash-literal-matrix.rb 2>&1 >/dev/null | grep -a '^\[light\]' # @@ -316,3 +318,150 @@ def k_mm(o, x) = o.anything(a: x) t('keyword_init Struct') { k_struct(1).a } t('**nil') { k_dsplat_nil(1) } t('keywords to method_missing') { k_mm(KMM.new, 1) } + +def guard + yield +rescue Exception => e + "#{e.class}: #{e.message[0, 60]}" +end + +# ---------------------------------------------------------------- 7. the callee's visibility +# A call a light frame makes with the frame in place of its scope must still be the source's call: private +# and protected methods, method_missing. +class Vis + def pub(a:) = [:pub, a] + def call_priv(x) = priv(a: x) + def call_priv_self(x) = self.priv(a: x) + def call_prot(o, x) = o.prot(a: x) + private def priv(a:) = [:priv, a] + protected def prot(a:) = [:prot, a] +end +class Other + def call_prot(o, x) = o.prot(a: x) + def call_priv(o, x) = o.priv(a: x) +end +class Missing + def method_missing(name, *args, **kw) = [name, args, kw] + def respond_to_missing?(*) = true + def go(x) = nope(1, a: x) + def go_self(x) = self.nope(a: x) +end + +t('private, implicit self') { |i| Vis.new.call_priv(i % 2) } +t('private, self.') { |i| Vis.new.call_priv_self(i % 2) } +t('protected, same class') { |i| Vis.new.call_prot(Vis.new, i % 2) } +t('protected, other class') { |i| guard { Other.new.call_prot(Vis.new, i % 2) } } +t('private, other class') { |i| guard { Other.new.call_priv(Vis.new, i % 2) } } +t('method_missing') { |i| Missing.new.go(i % 2) } +t('method_missing, self.') { |i| Missing.new.go_self(i % 2) } + +# ---------------------------------------------------------------- 8. a callee that reads the caller's scope +def scope_reader(x) = eval("x + 1", binding, __FILE__, __LINE__).then { |v| { v: v } } +def lvars(x, y = 2) = { names: local_variables, sum: x + y } +def blk_kw(x) = { given: block_given?, v: Integer(x.to_s, exception: false) } +def warn_kw(x) = (warn("w", uplevel: 0) if x < 0) +def raise_kw(x) = raise(ArgumentError, "m#{x}", cause: nil) +def send_kw(o, x) = o.send(:pub, a: x) +def psend_kw(o, x) = o.public_send(:pub, a: x) +def meth_kw(o, x) = o.method(:pub).call(a: x) +def struct_kw(x) = KS2.new(a: x, b: { c: x }) +KS2 = Struct.new(:a, :b, keyword_init: true) +t('eval with binding, then a literal') { |i| scope_reader(i % 2) } +t('local_variables in a literal') { |i| lvars(i % 2) } +t('block_given? and a keyword call') { |i| [blk_kw(i % 2), blk_kw(i % 2) { }] } +t('warn with uplevel, not reached') { |i| warn_kw(i % 2) } +t('raise with cause:') { |i| guard { raise_kw(i % 2) } } +t('send with keywords') { |i| send_kw(Vis.new, i % 2) } +t('public_send with keywords') { |i| psend_kw(Vis.new, i % 2) } +t('Method#call with keywords') { |i| meth_kw(Vis.new, i % 2) } +t('keyword_init Struct') { |i| struct_kw(i % 2).to_h } + +# ---------------------------------------------------------------- 9. the callee changes in the middle +# The site's rule is bound by then: each change must be seen by the next call. +class Chg + def f(a:) = [:v1, a] + def go(x) = f(a: x) + def go_lit(x) = f(**{ a: x }) +end +c = Chg.new +t('callee redefined half way') do |i| + Chg.class_eval { def f(a:, b: 9) = [:v2, a, b] } if i == N / 2 + c.go(i % 2) +end +t('callee made private half way') do |i| + Chg.send(:private, :f) if i == N / 2 + c.go(i % 2) +end +t('singleton method half way') do |i| + c.define_singleton_method(:f) { |a:| [:single, a] } if i == N / 2 + c.go(i % 2) +end +t('callee takes a positional Hash half way') do |i| + Chg.class_eval { def f(h = nil, **k) = [:v3, h, k] } if i == N / 2 + Chg.new.go(i % 2) +end +t('callee removed half way') do |i| + Chg.send(:remove_method, :f) if i == N / 2 + guard { Chg.new.go(i % 2) } +end +class Chg2 + def f(a:) = a + def go(x) = f(a: x) +end +t('callee replaced by one that reads the scope') do |i| + Chg2.class_eval { def f(a:) = block_given? ? [:blk, a] : [:none, a] } if i == N / 2 + [Chg2.new.go(i % 2), Chg2.new.go(i % 2) { }] +end +t('unknown keyword after a redefinition') do |i| + Chg2.class_eval { def f(b:) = b } if i == N / 2 + guard { Chg2.new.go(i % 2) } +end + + +# ---------------------------------------------------------------- 10. a literal whose pieces change under it +class HK + attr_reader :v + def initialize(v) = @v = v + def hash = @v.hash + def eql?(o) = o.is_a?(HK) && @v == o.v +end +def two(a, b) = { a => 1, b => 2 } +t('keys that are eql? or not') { |i| two(HK.new(1), HK.new(i % 2)).size } +t('#hash redefined half way') do |i| + HK.class_eval { def hash = 7 } if i == N / 2 + two(HK.new(1), HK.new(i % 2)).size +end +t('#eql? redefined half way') do |i| + HK.class_eval { def eql?(o) = false } if i == N / 2 + two(HK.new(1), HK.new(1)).size +end +t('String#freeze redefined') do |i| + String.class_eval { alias_method :__f, :freeze; def freeze = __f } if i == N / 2 + s = +"k" + h = { s => i % 2 } + [h.keys[0].frozen?, s.frozen?, h.keys[0].equal?(s)] +end +t('Hash.new redefined, a literal does not call it') do |i| + Hash.singleton_class.class_eval { alias_method :__n, :new; def new(*) = raise("called") } if i == N / 2 + r = guard { { a: i % 2 } } + Hash.singleton_class.class_eval { alias_method :new, :__n } if i == N - 1 + r +end + +# ---------------------------------------------------------------- 11. a refinement where the caller is defined +# Last in the file: a method defined where a refinement is in use is never a light frame, so every +# method after `using' is one with a scope. +module Loud + refine String do + def center(width, pad = " ", loud: false) = loud ? "LOUD" : super(width, pad) + def tag(a:) = "refined #{a}" + end +end +class String; def tag(a:) = "plain #{a}"; end +def unrefined_tag(s, x) = s.tag(a: x) +using Loud +def refined_tag(s, x) = s.tag(a: x) +def refined_center(s) = s.center(5, loud: true) +t('unrefined caller') { |i| unrefined_tag("s", i % 2) } +t('refined caller') { |i| refined_tag("s", i % 2) } +t('refined, keyword only it knows') { |_| refined_center("s") } From 2f0ef5d218b627e3379880b71ecfc9db44e5675f Mon Sep 17 00:00:00 2001 From: Largo Date: Fri, 20 Nov 2026 12:47:06 +0100 Subject: [PATCH 08/10] perf ledger: the gate for perf/why and perf/hashlit Both steps were merged on the census alone and marked "not gated". The gate has now been run on the branch against master, Release net10.0, and passes: IronRuby.Tests has the same one failure on both trees, seventeen matrices print master's output in four modes, a full ruby/spec sweep has no failure outside the known list, AWFY is not slower in both column orders on any benchmark, and start-up is within 2.5%. The entry records the numbers, including the times of the shapes the step is about ({a: x} 162 -> 118 ns, f(a: x) 104 -> 72 ns), and the one thing that needed a second look: the micro table's `empty' line was 6 to 8% slower in three runs and is the same, in instructions and in time, once the shape has a process of its own. Co-Authored-By: Claude Fable 5.2 --- Util/perf/LEDGER.md | 57 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/Util/perf/LEDGER.md b/Util/perf/LEDGER.md index efe72e0f..6f40fbce 100644 --- a/Util/perf/LEDGER.md +++ b/Util/perf/LEDGER.md @@ -596,7 +596,7 @@ Profile (`Util/perf/profile.sh`, self time): light frames 71.7% -> 76.9% (20,318 -> 21,793), light blocks 7,389 -> 7,724, JIT compiles, maybe and never unchanged (Src/StdLib + AWFY, 28,335 methods; 1,475 methods moved up, none down: 807 for the literal, 148 of those a `h = {}` default, and 668 for - the keywords) - **census only, not gated**. 42 of them are the prelude's, among them + the keywords). 42 of them are the prelude's, among them `group_by`, `uniq`, `tally`, `uniq!`, six `to_h`, `transform_keys`, `slice`, `fetch`, `to_set`, `transform_values!`, `__check_frozen__`; none is AWFY's, whose methods write no Hash literal, so the AWFY table is not expected to move. @@ -612,7 +612,56 @@ Profile (`Util/perf/profile.sh`, self time): -> 1,360, `{a: x, b: x}` 1,761 -> 1,449, `{}` filled with two keys 2,220 -> 1,964, `{"k" => x}` 2,010 -> 1,797, `f(a: x)` to a keyword 1,161 -> 893, two keywords to `**o` 2,692 -> 2,466, a control without a Hash 910 -> 872 (within its spread). - Not run: IronRuby.Tests, a sweep, any timing. Found on the way: gap 22. + Found on the way: gap 22. + + Gated on 2026-11-20 together with perf/why, the branch at 9f8bc1a7 against master at + 084b09cf, both Release net10.0, 8 cores, load 1.1 to 1.7 (`/perf-gate`): **PASS**. + - IronRuby.Tests: the same one failure on both trees (`Exception_CauseRaisedInClause1`, + master's, Release only). + - Seventeen matrices (hash-literal, interpolation, block-given, light-rest, block-pass, + splat-pass, hot-shapes, hash-order, hash-struct, callable-parameters, raise, + full-message, refinement, refinement-binding, jit, jit-calls, set-backtrace), each as + built, with `IR_LIGHT_HASH=0 IR_LIGHT_KEYWORD_HASH=0`, with `IR_LIGHT_FRAMES=0` and + with `-X:NoJIT -X:NoOSR`: the branch prints what master prints in all 68 runs (the + one line of callable-parameters that differs is a `source_location` with the tree's + own path in it), and CRuby's output wherever master does. + - The four questions. A million deep through a literal's value, its String key, a key's + `#hash`, a keyword call to a required keyword, to `**o`, from a block and inside a + literal: SystemStackError each time and the process alive, as on master. Visibility, + a callee that reads the caller's scope, a callee and a key's `#hash` changing half + way, and a refinement in use where the caller is defined are sections 7 to 11 of + `Util/hash-literal-matrix.rb` (31 probes, 148 lines in all): CRuby's output on both + trees and in every mode above and with `-X:NoAdaptiveCompilation`; `IR_LIGHT_FRAMES=why` + names 113 of the file's methods as light frames on the branch and 21 on master, the + new callers among them. The first version of these probes tested less than it + said: a `using` in the middle of the file kept every later method out of light + frames, and the methods that recursed through a keyword call had keyword parameters + of their own. The refinement section is last for that reason. + - ruby/spec, all five suites (`Util/ci-sweep.sh`, then `sweep-check.rb`): 32,680 + examples, 45 failing, every one listed, none new, no file without a summary. + - Time on the shapes it is about (`ab.rb -r 5`, on-CPU ns per call, the branch with + both switches off -> on; CRuby 4.0.9 in the same rounds): `{a: x}` 162 -> 118 (CRuby + 92), `{a: x, b: x}` 213 -> 140 (108), `{}` filled with two keys 214 -> 172 (133), + `{"k" => x}` 244 -> 175 (97), `f(a: x)` to a keyword 104 -> 72 (60), two keywords to + `**o` 288 -> 242 (155), `to_h` with a block 528 -> 480 (123). `tally`, `group_by`, + `uniq` with a block and the two controls overlap: the prelude's methods that moved + spend their time elsewhere. Instructions per call agree (`ab.rb -i`: 0.82, 0.74, + 0.92, 0.78, 0.79, 0.86 on the first six; master counts what the switches-off build + counts). + - AWFY, `awfy.rb 12` in both column orders, no FAIL: geomean branch/master 1.002 and + 1.014; nothing is more than 5% slower in both orders (Richards and NBody are 5.5% + and 5.9% slower in the order where the branch ran first and 1.8% and 0.2% faster in + the other). As expected: none of AWFY's methods moved. + - `micro-table.rb` three times, once with the columns the other way round: `empty` was + 6 to 8% slower on the branch in all three, `while10` and `selfcall` in two. Each in + a process of its own (`ab.rb ... -- NAME`): the same instructions per call on both + trees to two parts in a thousand (342.4 and 341.8, 791.2 and 790.9, 596.2 and 595.8) + and the same time over eight alternating rounds (27.5 and 26.7 ns, 68.0 and 68.0, + 46.3 and 46.4). So it is the state the table's earlier shapes leave the process in, + as in round eight, and not a cost of the change, which decides only what is + compiled. + - Start-up, `-e 1`, best of ten: 1,148 ms on master, 1,177 on the branch (+2.5%). + Not measured: TruffleRuby (not installed here), net11.0, Debug. - **perf/why (/perf-step, 2026-11-19) - a find-out step: the census names the construct that keeps a method out of light frames.** `IR_LIGHT_FRAMES=why` and the census said @@ -626,8 +675,8 @@ Profile (`Util/perf/profile.sh`, self time): `light_why`, and the 5,704 that had a line number are the old text plus the construct (my own comparison of the two TSVs; the merged build's census is the worktree's byte for byte). The interpolation, block-pass and splat-pass matrices print CRuby's output - on the merged build. Not run: IronRuby.Tests, a sweep, any timing; the texts that only - appear with a switch off. What it found is under gap 9. + on the merged build. Gated on 2026-11-20 with perf/hashlit (above). Not checked: the + texts that only appear with a switch off. What it found is under gap 9. - **perf/interp (/perf-step, 2026-11-19) - a method that interpolates runs without a scope** (`"a#{x}"`, `raise ArgumentError, "negative size (#{n})"`; `IR_LIGHT_INTERPOLATION=0` turns it off). The filter (`IsScopeFree`) has taken an From b2c1329f56d5690786431a956cdf3b48444f672d Mon Sep 17 00:00:00 2001 From: Largo Date: Fri, 20 Nov 2026 13:20:55 +0100 Subject: [PATCH 09/10] Hash-literal matrix: a literal in a method whose scope is parked Master's parked scopes keep the scope of a method whose literal blocks all reuse their Procs for its next call. With a Hash literal or keywords in it such a method is now a light frame that parks, which the matrix did not have: section 11 adds 15 probes of what one call may leave for the next (the literal in a local the block changes, in the block, as a default, yielded, a binding kept, a raise, a throw and a return from the block, recursion, threads). The probes use `3.times' and not a Range: a Range literal in a body with literal blocks keeps the method out of light frames, and the first version of these probes therefore tested a method with a scope. IR_PARKED_SCOPES=verbose names eleven of the section's methods as parked light frames. CRuby 4.0 and IronRuby print the same, with the parking, the light frames and the JIT each on and off. Co-Authored-By: Claude Fable 5.2 --- Util/hash-literal-matrix.rb | 55 ++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/Util/hash-literal-matrix.rb b/Util/hash-literal-matrix.rb index 480c82f6..b67e09ed 100644 --- a/Util/hash-literal-matrix.rb +++ b/Util/hash-literal-matrix.rb @@ -14,9 +14,10 @@ # duplicate keys, the String key's frozen copy, the result being a new plain Hash on every call (no default, # no compare_by_identity, mutation not seen by the next call), literals in blocks, with yields, in defaults, in # loops, recursion, exceptions and their backtraces, defined?, tracing, threads, and the keyword hash a call -# site builds (`f(a: x)') next to the literal it is not. Sections 7 to 11 are about the call a light frame makes -# with keywords: the callee's visibility, a callee that reads the caller's scope, the callee or a key's #hash -# changing half way through the N calls, and a refinement in use where the caller is defined. +# site builds (`f(a: x)') next to the literal it is not. Sections 7 to 12 are about the call a light frame makes +# with keywords and about what may outlive it: the callee's visibility, a callee that reads the caller's scope, the +# callee or a key's #hash changing half way through the N calls, a literal in a method whose scope is parked for +# its next call, and a refinement in use where the caller is defined. # # IR_LIGHT_FRAMES=why ./ir.sh Util/hash-literal-matrix.rb 2>&1 >/dev/null | grep -a '^\[light\]' # @@ -448,7 +449,53 @@ def two(a, b) = { a => 1, b => 2 } r end -# ---------------------------------------------------------------- 11. a refinement where the caller is defined +# ---------------------------------------------------------------- 11. a literal in a method whose scope is parked +# A method whose literal blocks all reuse their Procs keeps its scope for its next call (IR_PARKED_SCOPES, +# Util/parked-scope-matrix.rb). With a Hash literal or keywords in it, it is a light frame that parks: nothing of +# one call may show in the next, whether the call returned, raised, threw or returned from the block. +class ParkedLit + def initialize = @s = [1, 2, 3] + def each = 3.times { |i| yield @s[i] } + def each_lit = 3.times { |i| yield({ i: i, v: @s[i] }) } + def each_kw = 3.times { |i| yield(i: i, v: @s[i]) } + def lit_local(x); h = { n: x }; 3.times { |i| h[:n] += @s[i] }; h; end + def lit_in_block(x); r = nil; 3.times { |i| r = { x => i } }; r; end + def lit_default(x, h = {}); 3.times { |i| h[i] = x }; h; end + def kw_out(x); r = 0; 3.times { |i| r += kw(a: x, b: i) }; r; end + def kw(a:, b: 0) = a + b + def keep(x); h = { x: x }; 3.times { |i| h[i] = binding if i == 1 }; h; end + def lam(x) = { f: [0, 1, 2].map { |i| -> { i + x } } } + def raise_mid(x); h = { x: x }; 3.times { |i| raise IndexError, h.inspect if i == x }; h; end + def thrown(x); h = { x: x }; 3.times { |i| throw :out, h if i == x }; h; end + def early(x); h = { x: x }; 3.times { |i| return h.merge(i: i) if i == x }; h; end + def rec(n); h = { n: n }; 1.times { |_| h[:d] = n > 0 ? rec(n - 1) : nil }; h; end +end + +v = ParkedLit.new +t('yield a literal from a parked block') { |i| a = []; v.each_lit { |h| a << h }; [a, a.map(&:object_id).uniq.size] } +t('yield keywords from a parked block') { |i| a = []; v.each_kw { |**h| a << h }; a } +t('a literal in a local, changed by the block') { |i| v.lit_local(i % 2) } +t('the result is not the next call\'s') { |i| r = v.lit_local(0); r[:n] += 100; [r, v.lit_local(0)] } +t('a literal made in the block') { |i| v.lit_in_block(i % 2) } +t('a `{}\' default') { |i| [v.lit_default(i % 2), v.lit_default(1).equal?(v.lit_default(1))] } +t('a default given by the caller') { |i| h = { z: 1 }; v.lit_default(i % 2, h).equal?(h) } +t('keywords from a parked block') { |i| v.kw_out(i % 2) } +t('binding kept, each call its own literal') do |i| + b1 = v.keep(1)[1]; b2 = v.keep(2)[1] + [b1.local_variable_get(:h)[:x], b2.local_variable_get(:h)[:x], b1.local_variable_get(:h).equal?(b2.local_variable_get(:h)), b1.local_variable_get(:x)] +end +t('lambdas in a literal') { |i| a = v.lam(i % 2)[:f]; b = v.lam(10)[:f]; [a.map(&:call), b.map(&:call)] } +t('raise in the block, then a normal call') { |i| r = (v.raise_mid(i % 4) rescue $!.message); [r, v.raise_mid(9)] } +t('throw from the block') { |i| [catch(:out) { v.thrown(i % 4) }, v.thrown(9)] } +t('return from the block') { |i| [v.early(i % 4), v.early(9)] } +t('recursion through the block') { |i| v.rec(3) } +t('threads') do |i| + next :skipped unless i % 500 == 0 + pvt_r = Array.new(4) { |pvt_k| Thread.new { Array.new(50) { v.lit_local(pvt_k) } } }.map(&:value) + pvt_r.map(&:uniq) +end + +# ---------------------------------------------------------------- 12. a refinement where the caller is defined # Last in the file: a method defined where a refinement is in use is never a light frame, so every # method after `using' is one with a scope. module Loud From f826a32490b4407e35971c4cbf1ccd83e3ffc684 Mon Sep 17 00:00:00 2001 From: Largo Date: Fri, 20 Nov 2026 19:03:37 +0100 Subject: [PATCH 10/10] perf ledger: the gate on the branch with master's parked scopes merged in The gate block for perf/hashlit and perf/why described the branch before master's parked scopes (perf/park) were merged into it. Both changes decide in MethodDefinition.cs how a method is entered, so the merged commit was gated again: 2a516b4d against master 1522386b, Release net10.0. It passes. The entry records the tests, the 21 matrices in five modes, the probes for a literal in a method whose scope is parked, the census, the sweep and the timing. IronRuby.Tests ran without MutableString_CopyOnWrite3 on both trees: its reader starves against the writer on a Release build on the machine used, on master as well. Co-Authored-By: Claude Fable 5.2 --- Util/perf/LEDGER.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Util/perf/LEDGER.md b/Util/perf/LEDGER.md index 637e7680..122a2bdd 100644 --- a/Util/perf/LEDGER.md +++ b/Util/perf/LEDGER.md @@ -1250,6 +1250,44 @@ loop of its own): `b.local_variable_set(:x, 1)` 0.1 -> 436, `erb.result_with_has - Start-up, `-e 1`, best of ten: 1,148 ms on master, 1,177 on the branch (+2.5%). Not measured: TruffleRuby (not installed here), net11.0, Debug. + Gated again the same day, after master's parked scopes (perf/park) were merged into the + branch: the branch at 2a516b4d against master at 1522386b, both Release net10.0, load + 1.0 to 1.6 during the timing: **PASS**. The two changes meet in `MethodDefinition.cs`: a + method that is a light frame only because of this change may now also park its scope. + - IronRuby.Tests: `PASSED` on both trees, 1,290 tests each, with + `MutableString_CopyOnWrite3` left out on both. Its reader starves against the writer + on a Release build on this machine (past 100 s in 8 of 8 runs alone, four on each + tree, and on Release builds of two days before): master's, and none of this code. + - Twenty-one matrices, the seventeen above and parked-scope, block-scope-reuse, + block-loop and binding-lvar, each as built, with `IR_LIGHT_HASH=0 + IR_LIGHT_KEYWORD_HASH=0`, with `IR_LIGHT_FRAMES=0`, with `IR_PARKED_SCOPES=0` and with + `-X:NoJIT -X:NoOSR`: the branch prints what master prints in all 105 runs. + - A literal in a method whose scope is parked is section 11 of + `Util/hash-literal-matrix.rb` (15 probes): CRuby's output on both trees and in the + five modes. `IR_PARKED_SCOPES=verbose` on them: 11 methods park as light frames and 1 + with a scope on the branch, 4 and 8 on master, so the probes run the path they are + about. The first version of them did not: `(0..2).each` in a body with literal blocks + keeps the method out of light frames, so they use `3.times`. The million-deep probes + and the 31 probes of visibility, scope readers and callees changing half way print + what master prints in the same modes. + - Census on the merged trees, 28,337 methods: light frames 20,318 -> 21,793 (+1,475), + JIT compiles 2,979, maybe 2,771 and never 22,587 on both. + - ruby/spec, all five suites: 32,680 examples, 45 failing, every one listed, none new, + no file without a summary. + - Time on the shapes (`ab.rb -r 5`, switches off -> on; CRuby 4.0.10): `{a: x}` 160 -> + 120 (CRuby 92), `{a: x, b: x}` 214 -> 142 (108), `{}` filled 218 -> 175 (134), + `{"k" => x}` 240 -> 174 (96), `f(a: x)` 104 -> 70 (60), two keywords to `**o` 278 -> + 244 (154), `tally` 368 -> 336 (250), `uniq` with a block 768 -> 656 (320); `to_h`, + `group_by` and the two controls overlap. Instructions per call: 0.82, 0.75, 0.93, + 0.80, 0.77, 0.89 on the first six, 0.89 on `tally`. + - AWFY, `awfy.rb 12` in both column orders, no FAIL: geomean branch/master 1.005 and + 0.995. The largest single difference is Bounce, 3.6% slower in one order and 0.6% in + the other. + - `micro-table.rb` three times, once reversed: no line is more than 5% slower in all + three; `nil?` is in two (14% and 9%). In a process of its own it counts 502.1 and + 501.8 instructions per call and takes 41.3 and 40.0 ns over eight alternating rounds. + - Start-up, best of ten: 1,167 ms on master, 1,204 on the branch (+3.2%). + - **perf/why (/perf-step, 2026-11-19) - a find-out step: the census names the construct that keeps a method out of light frames.** `IR_LIGHT_FRAMES=why` and the census said only which statement of a body was not scope-free (`IfExpression at line 12`), so its