Repository navigation
A reader written as a method takes attr_reader's arm - #335
Conversation
`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
left a comment
There was a problem hiding this comment.
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.
|
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
I also tried checking the reader shapes inline in the guard, without calling 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
left a comment
There was a problem hiding this comment.
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.
def x = @xanddef x; @x; endnow take the frameless reader arm thatattr_readeralready takes. They used to cost about 10× more thanattr_reader: about 21,900 instructions a call on the evaluator and 23,200 on the VM, againstattr_reader's 2,000 and 3,700. Now they cost whatattr_readercosts on both engines. The record isloop/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.
attr_reader :xdef x = @xdef x; @x; endThe change
A new function,
triv_reader_iv, recognizes three bodies:attr_reader's;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:
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
Line marks. The fast arm makes no frame and passes no mark, so the caller's
cur_lineandcur_filestay as they were. That is what the full path restores on return. Corpus 834 checks it:l.w; raise);backtrace_locationslineno and path,__LINE__andcaller_locationsagainst ruby, on both engines, with and withoutMERE_RUBY_ATTR_IC_CHECK=1.Tracing. The conditions are the
attr_readerarm's, unchanged:hook_tp_on ()covers TracePoint andset_trace_func;cov_st 0 != 2covers 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; cinside a method, and one made aftero.ainsidetrace_object_allocations, report the sameallocation_sourceline,allocation_method_idandallocation_sourcefileas ruby on both engines.Also: the #332 Ractor comment
vm_ivar_ok's comment now covers a method still running on an object thatRactor#send(move: true)took, after every Ractor has ended. Ruby raisesMovedErrorat the next call on that self. mere-ruby does not:moved_guardrefuses only explicit moved receivers;So the cache adds no gap of its own. The older gap is not closed here.
Checked
run_corpus.sh981/981, plain and withMERE_RUBY_ATTR_IC_CHECK=1.vm_corpus.sh: all 981 on the VM, 0 fallback, 0 FAIL.bracket_depth_check,iv_door_check,meth_writes_check, and the defs and names checks.🤖 Generated with Claude Code