GitNexus/gitnexus/bench/ruby-block-param-stop/README.md
Abhinav Pandey 704ec678b9
fix(ruby): a block parameter stops the receiver type walk — it no longer takes a same-named outer local's type (RUBY-03) (#3905)
* fix(ruby): a block parameter stops the receiver type walk — it no longer takes a same-named outer local's type (RUBY-03)

The shared receiver walk stops at the nearest scope that owns the
receiver's name, and a scope owns a name it holds no type for only through
Scope.lexicalNames. Ruby published none, so an untyped parameter was
invisible to the walk, which climbed past it to a same-named outer binding:
`user = User.new; items.each { |user| user.save }` resolved the block's
`user.save` to User#save.

- emitRubyScopeCaptures attaches @scope.lexical-names to every scope that
  declares parameters: a do/end or brace block (its |...| names, block
  locals included; this covers lambda { |x| }, proc { |x| } and
  Proc.new { |x| }), the body block of a stabby lambda (its parameter list
  is a sibling of the body), and a method or singleton method. Blocks
  already had scopes; none is added. Numbered and `it` parameters are not
  declared names and are not listed.
- The YARD @param decline (RUBY-01) no longer fires for a block, lambda or
  nested-def parameter of the tagged name: the shadow now stops the walk on
  its own scope, so the tagged use beside it keeps its type. A stabby lambda
  whose parameter default reads the name still declines, because the
  parameter list sits outside the lambda body.

Fixed shapes, each pinned against exact owner IDs with a decoy: block and
lambda/proc parameters shadowing a typed local or a YARD-typed parameter,
nested blocks, every block-parameter form, an untyped method parameter named
like a typed reader, an attr_reader @return type or a top-level local, a
nested def parameter under a tagged method, and a bare read of a parameter
(a read, not a call). Recall cost: an explicit target() / target(1) /
target 1 beside a same-named block parameter now resolves to nothing, as it
already did beside a same-named method parameter.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* test(bench): gate Ruby block-parameter stop correctness and scaling (RUBY-03)

Block, lambda and method parameters are published as lexical names on the
scope that declares them, so the receiver walk stops at a block parameter
instead of taking a same-named outer local's type.
bench/ruby-block-param-stop pins what that produces and checks how its
cost grows, in the format of bench/ruby-gem-resolution.

Each file holds class Svc<i> with m(items): `user = User.new`, K blocks
rotating `|x| user.run` (the nearest `user`), `|user| user.save` and
`|user, i| user.save` (no edge), side by side or nested, then `user.save`.
Each method's CALLS must equal the expected multiset; main gives the
rebinding blocks wrong User#save edges. One class per file, because Ruby
resolution on main is already super-linear in the classes of ONE file
(measured with `--shape crowded`, not gated).

--check runs a CI profile (about 15 s locally); --full keeps min of 15
after 2 warmups and a fresh process per shape. Every budget is 1.5.
Re-running the scope query over the method for every block scope fails
the nested block gate (2.242 > 1.5).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* test(bench): ruby-block-param-stop — fixed-width indent, input bytes per cell, 16x side-by-side blocks

Three defects the scaling-bench review found in the other benches apply
here too:
- the nested generator indented each level two spaces deeper, so the
  input grew 6.1x for 4x depth; every line now has a fixed-width indent
  and each shape pins its input_bytes beside its groups and edges;
- the gate could not see a known O(blocks^2) edit: walking the enclosing
  method once per block scope measured 1.410 on the 4x side-by-side
  range and passed. Side-by-side blocks now grow 16x (6 -> 96), where
  the same edit fails sibling_capture_block_scaling_ratio 3.596 > 1.5
  (nested 1.778) with unchanged fingerprints;
- the ratio divides by each shape's own growth factor.

Nesting depth stays 4x: over 6 -> 96 it measures 2.57 (capture) and 2.46
(resolution) from two terms that predate #3905 (the shared callable-flow
fallback that searches a block-taking call's subtree for an argument
list, and the shared lexical lookup climbing from each reference to its
binder); baseline.json `_nested_depth_note` records both. Three CI-profile
runs and one --full run pass, every ratio at most 1.202.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(ruby): resolve PR #3905 feedback

Preserve outer YARD types across lambda defaults and explicit self calls beside parameters. Add parameter-order regressions and a four-size scaling sweep.

Note: pre-existing failure in scenario-map/consolidation.test.ts (jscpd 5.4.2 installed; 5.4.3 expected) not addressed by this PR.

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-13 17:47:07 +00:00

72 lines
3.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Ruby parameter scope benchmarks
Run from `gitnexus/`:
```sh
npm run build
node --import tsx bench/ruby-block-param-stop/measure.mjs --check
node --import tsx bench/ruby-block-param-stop/scaling.mjs --check
```
`measure.mjs` checks exact call targets, decline counts, fingerprints, and the
CI scaling gates: 4× method count, 16× sibling blocks, and 4× nesting depth.
Its fixed-width indentation keeps input size linear in depth; each shape pins
`input_bytes`. `scaling.mjs` adds four-size sweeps:
| Workload | Sizes | Held constant | Timed phases |
| --- | --- | --- | --- |
| Sibling/nested blocks, method count | 64, 128, 256, 512 | 6 blocks per method | Capture, resolution |
| Sibling/nested blocks, block count/depth | 3, 6, 12, 24 | 64 methods | Capture, resolution |
| Lambda default parameters | 64, 128, 256, 512 | One lambda; each default reads an earlier parameter | Capture |
| Explicit calls beside parameters | 64, 128, 256, 512 methods | 3 calls per method | Capture |
| Wide parameter list and explicit calls | 64, 128, 256, 512 | Parameters and calls grow together in one method | Capture |
Each size runs in a fresh process, with two warmups and seven measured samples.
The reported time is the minimum sample. Capture includes parsing; resolution
replays worker output into a fresh graph before starting its timer. The resolver
sweeps assert zero missing and unexpected edges. The capture workloads assert
the corrected YARD binding, default scopes, explicit self calls, and absence of
false callable-cell invocations. The default workload also asserts that the
number of emitted lexical names grows linearly.
The JSON report includes work counts, timings, adjacent normalized growth, and
a fitted log-log exponent. Exponent 1 means linear measured growth; below 1 is
sublinear. `--check` allows scheduling/GC noise up to exponent 1.2 and normalized
endpoint growth 1.5. Passing those limits is evidence over the measured sizes,
not an asymptotic complexity proof.
The resolver sweeps keep one class/method per file. They do not cover the known
cost of putting many classes in one file. That layout can be measured separately:
```sh
node --import tsx bench/ruby-block-param-stop/measure.mjs --shape crowded 512 6 --reps 7 --warmup 2
```
Capture-only workloads use one file and include a wide parameter list,
exercising repeated parameter-name lookups at explicit calls.
The depth sweep stops at 24. Pre-existing shared capture and lookup costs
become superlinear at 96 levels; `baseline.json` records that boundary in
`_nested_depth_note`.
## Recorded run
2026-11-13, Linux x64, Node v22.23.3, Intel Core i9-12900K. Two complete
`scaling.mjs --check` runs passed on a shared host during the graph refresh,
using the benchmark harness from `35db3d7a6`. Ranges retain the variation
between runs:
| Workload | Capture exponent | Resolution exponent |
| --- | --- | --- |
| Sibling blocks, method count | 0.988–1.018 | 0.880–0.943 |
| Sibling blocks, block count | 0.677 | 0.594–0.613 |
| Nested blocks, method count | 0.998–1.014 | 0.920–1.006 |
| Nested blocks, depth | 0.885–0.966 | 0.638–0.673 |
| Lambda defaults | 0.839–0.848 | — |
| Explicit calls beside parameters | 0.917–1.048 | — |
| Wide parameter list and calls | 0.883–0.948 | — |
The largest normalized endpoint growth was 1.108 for capture and 0.995 for
resolution. Every resolver size had zero missing and unexpected edges. These
measurements support approximately linear or sublinear growth for both phases
over the tested sizes; they do not establish a strict O(N) bound.