Skip to content

A reader written as a method takes attr_reader's arm - #335

Merged
284km merged 2 commits into
mainfrom
stage10-reader-fast-arm
Nov 26, 2026
Merged

284km merged 2 commits into
mainfrom
stage10-reader-fast-arm

Conversation

@284km

@284km 284km commented Nov 26, 2026

Copy link
Copy Markdown
Owner

def x = @x and def x; @x; end now take the frameless reader arm that attr_reader already takes. They used to cost about 10× more than attr_reader: about 21,900 instructions a call on the evaluator and 23,200 on the VM, against attr_reader's 2,000 and 3,700. Now they cost what attr_reader costs on both engines. The record is loop/2026-11-26-stage10-reader-fast-arm.md.

Instructions per call

Main is 7a733b4. Both sides are built with Mere 9dec2ff6, CI's. Each figure is the median of 3 alternating runs of 300,000 calls, minus the same loop with no call.

reader evaluator: main here VM: main here
attr_reader :x 2,040 2,067 3,687 3,677
def x = @x 21,900 2,037 23,243 3,707
def x; @x; end 21,947 2,033 23,263 3,717

The change

A new function, triv_reader_iv, recognizes three bodies:

  • attr_reader's;
  • the parser's, after any number of line and file marks;
  • the ISeq VM's two-instruction program getinstancevariable; leave, only if it is still loaded and is a method's.

The arm keeps attr_reader's conditions. They are reordered so the body check runs first: a call that is not a reader stops there, before the tracing, Coverage and Ractor checks.

What other calls pay

Every other zero-argument call on an object now runs the body check, which costs 30 to 95 instructions:

program evaluator VM
self-call loop +0.1% +0.23%
method calling another object's method +0.26% +0.31%
app_fib +0.06% +0.03%
so_object +0.04% +0.03%

Rails' warm boot is unchanged at 49.1 G on the evaluator and 48.3 G on the VM. Under MERE_RUBY_GC_TIGHT=1, max RSS (median of 3) went from 647 to 643 MB on the evaluator and from 740 to 702 MB on the VM.

The review's two points

  1. Line marks. The fast arm makes no frame and passes no mark, so the caller's cur_line and cur_file stay as they were. That is what the full path restores on return. Corpus 834 checks it:

    • a raise on the line after a reader, and on the same line (l.w; raise);
    • it compares backtrace_locations lineno and path, __LINE__ and caller_locations against ruby, on both engines, with and without MERE_RUBY_ATTR_IC_CHECK=1.
  2. Tracing. The conditions are the attr_reader arm's, unchanged:

    • hook_tp_on () covers TracePoint and set_trace_func;
    • cov_st 0 != 2 covers running Coverage, lines and methods.

    ObjectSpace's allocation trace is not a condition, because a reader allocates nothing. An Array literal made after a; b; c inside a method, and one made after o.a inside trace_object_allocations, report the same allocation_sourceline, allocation_method_id and allocation_sourcefile as ruby on both engines.

Also: the #332 Ractor comment

vm_ivar_ok's comment now covers a method still running on an object that Ractor#send(move: true) took, after every Ractor has ended. Ruby raises MovedError at the next call on that self. mere-ruby does not:

  • moved_guard refuses only explicit moved receivers;
  • the evaluator reads and writes a moved self's ivars, and so does main's VM.

So the cache adds no gap of its own. The older gap is not closed here.

Checked

  • run_corpus.sh 981/981, plain and with MERE_RUBY_ATTR_IC_CHECK=1.
  • vm_corpus.sh: all 981 on the VM, 0 fallback, 0 FAIL.
  • vm_check 246/246.
  • The collector's root check, bracket_depth_check, iv_door_check, meth_writes_check, and the defs and names checks.

🤖 Generated with Claude Code

`def x = @x` and `def x; @x; end` read one instance variable and answer it,
as attr_reader does, but made a frame of their own: about 21,900
instructions a call on the evaluator and 23,200 on the VM, against
attr_reader's 2,000 and 3,700. Their bodies are the parser's (after line and
file marks) or the ISeq VM's (`getinstancevariable; leave`), not
attr_reader's, so run_meth_result's frameless reader arm did not match.

triv_reader_iv recognises the three bodies, and the arm now takes any of
them under attr_reader's conditions (no block, no TracePoint or
set_trace_func, no running Coverage, no Ractor), with the body check first
so a call that is not a reader stops there. Both reader forms now cost what
attr_reader costs on both engines; other zero-argument calls pay 30 to 95
instructions (+0.1% to +0.3% on call loops, 0.06% or less on app_fib and
so_object, nothing on Rails's boot).

Corpus 834 covers the forms, private/alias/redefinition/singleton/frozen/
refinement cases, and the line and file after a reader (backtrace, __LINE__,
caller_locations). vm_ivar_ok's comment now says what a moved self does
after all Ractors end (#332's review).

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of dfb88a29.

The fast arm is right. Skipping the line/file marks is safe because nothing in the body can raise or run Ruby. Corpus 834 checks backtrace_locations, __LINE__ and caller_locations around a reader call. The SVm case checks both the program length and the generation before trusting getinstancevariable; leave. And the @@ exclusion and the hook/Coverage/Ractor/block conditions are the attr_reader arm's own. The vm_ivar_ok comment answers #332's question, and finding that the evaluator has the same moved-self gap is useful.

Minor (cost on every other zero-argument call): triv_reader_iv body9 is now evaluated in the guard for every zero-argument call on an object, reader or not, and then a second time in the arm for readers. You measured 30–95 instructions a call, +0.1–0.3% on call loops. Since the answer depends only on the method's body (and, for SVm, its generation), it can be decided once per definition. One way is a field in the method record from #321 (mrec_build / mrec_top_build), so a non-reader pays one comparison and a reader reads its ivar name without walking the body again. Please try it. If moving the decision costs the reader path more than it saves elsewhere, keep this form and say so in the PR, and I'll merge it as a recorded small exception.

Everything else (corpus 981/981 with and without ATTR_IC_CHECK, the VM corpus, vm_check 246/246, and the RSS medians going down) is posted, and the test merge with current main is clean.

@284km

284km commented Nov 26, 2026

Copy link
Copy Markdown
Owner Author

On the Minor (deciding "is this body a reader" once per definition, in the method's record): tried, and kept the current form. The reasoning is in loop/2026-11-26-stage10-reader-fast-arm.md under "Why the answer is not kept in the method's record". In short:

  • The record cannot answer "reader". It is keyed by Class#name and describes the method defined under that name now. A Method or UnboundMethod keeps the body it was taken from. If x is redefined as a reader after mm = o.method(:x), the record would send mm.call down the fast arm and skip the old body's @y = 2. Corpus 834 now has this case, and it matches ruby on both engines, with and without the attr_ic check. So the record could only say "not a reader", as a filter.
  • As a filter, it moves the cost instead of removing it. The frameless arms fetch no record today; only the full path does, further down. Fetching it before them would cost every reader call a key built at run time plus a lookup: about 190 instructions for the hash alone (measured in step 1 of note 560 B), against a reader's ~2,000. The saving on a call that is not a reader is 30 to 95 instructions.

I also tried checking the reader shapes inline in the guard, without calling triv_reader_iv. A self call came out at +0.18% / +0.23%, against +0.1% / +0.23% as it stands, which is within noise, so I dropped it.

I tried to time the lookup directly, but the compiler removed the probe both times, so I am not quoting a number for it.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review at 7645e85c. Agreed on keeping the per-call check. A method record describes the current definition, but a Method/UnboundMethod keeps the body it was taken with, so the record cannot answer "this is a reader". The new corpus 834 case (an old mm.call whose body also writes @y after x is redefined as a reader) shows exactly that. Moving a "not a reader" bit earlier would only move the cost onto readers through the record lookup. I accept the 30–95 instructions on non-reader zero-argument calls (+0.1–0.3% on call loops) as a recorded small exception. The code is unchanged since my review, the reader arm reads through the door (ivs_get_or) so it is unaffected by #336, and the test merge with current main is clean.

@284km
284km merged commit 8b9907b into main Nov 26, 2026
7 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant