Repository navigation
An object's variables: the rest of the way through the door (note 559, step 2a) - #299
Conversation
…, step 2a) What still read an object's key "<id>@<name>" itself asks the door now (iv_key_owner, iv_key_name_at, iv_name_eq, iv_name, iv_del_at): the collector's marking and sweep, flush_files_at_exit, ivar_names_walk and iv_copy. exc_cause_link_to answers the exception's id rather than a key, and the thread-locals' base is an id (fb_tl_owner) whose variables go through iv_*. What is kept is unchanged. The walks call map_iter themselves: a curried callback of the door's, called per variable, made a collection 33-43% dearer in instructions.
284km
left a comment
There was a problem hiding this comment.
Review of step 2a (note 559). Head 5ac43f11. Reference: ruby 4.0.10.
Verdict: no Critical, Major or Minor finding in this diff. Two Suggestions and two pre-existing bugs (not caused by this PR) are below.
What I read
I read the old and new code side by side for every changed site.
- Mark (
gc_fixpoint).iv_key_ownerisgc_owner_ofmoved to m_state, character for character. The owner of every key is the same. So the owner chains, and what is marked, are the same. - Sweep (
gc_collect). The set of deleted keys is the same: owner >= 0 and owner not live.iv_del_atismap_deleteof the same key, asgc_drop_swas. The iv_idx sweep still uses the same owner parse. - Key shapes. "@name" gives the same owner and name as before. Keys that do not start with a digit ("cls:..", "str:..", "arr:..", "hsh:..", "prc:..", "bnd:..", "$exc") give -1, as before, and still go to
gc_owned_meta_add. Thread-locals are "@_tl..", the same Strings as before, becausefb_tl_ownerreturns the same id as a number. A name that contains "@" ("12@a@b") gives owner 12 and name "a@b" in both versions, becauseiv_key_name_atuses the first "@". iv_copy. Same prefix test and sameiv_putwrites asivars_copy_pfx. The only change is the order of writes (walk order instead of a second Map), and the probes show the sameinstance_variablesorder after dup and clone.- Thread-locals.
iv_has,iv_get,iv_set,iv_delbuild the same keys.iv_setgoes throughiv_put, as the old writes did, so the name index sees the same writes. exc_cause_link_to. It returns the id instead of the key. The caller reads and writes the same "@__cause" key.
Two small changes in behaviour (both for the better)
"Behaviour" here means only what a Ruby program can see.
-
The sweep now checks the exact name
__f_fd. Before, it checked that the key ends with "@__f_fd".flush_files_at_exitchanged the same way for__f_buf. These differ only for a name that contains "@". Ruby rejects such names, but mere-ruby accepts them today (see pre-existing bug A). I ran this probe:f = File.open(path, "w"); fd = f.fileno o = Object.new; o.instance_variable_set("@x@__f_fd", fd); o = nil GC.start; f.syswrite("hello\n")
Main gives
Errno::EBADF, because the sweep closed the descriptor of a live File. This PR gives:ok, the same as ruby. So the change fixes a corner case. It is worth one line in the PR text, because the PR says nothing changes. -
In
ivar_names_walk(the fallback listing), a key "@" with an empty name used to be skipped and is now listed as "". I could not create such a key from Ruby:instance_variable_set("@")raises NameError. So this is reading only.
Probes (run, both engines, ruby 4.0.10)
I built head and its merge base (8c6699f) with CI's Mere (9dec2ff6). The emitted C of my head build is byte-identical to the author's build.
Each probe ran with MERE_RUBY_VM unset and =1, under plain, MERE_RUBY_GC_TIGHT=1, MERE_RUBY_GC_STRESS=50 and MERE_RUBY_GC_STRESS=1. For STRESS=1 I used smaller copies of the same programs. That is 48 runs per build. All exited 0, and head output = base output in all 48.
- Objects with 4 ivars, most dropped across rounds of
GC.start. Survivors keep their values. Also covered: classes with ivars, Strings, Arrays and Hashes with ivars, a Struct with an extra ivar, dup/clone/frozen clone of survivors, andinstance_variablesorder after remove and re-add. All the same as ruby. - A cause chain 4 deep (
raiseinrescue, 3 levels), checked after GC. Also:raise a, cause: b(circular causes),cause: nil, and#exception(msg)keeping ivars. All the same as ruby. - The at_exit path that uses
exc_cause_link_to: anat_exitthat raises "outer" with cause "inner" after anotherat_exitraised "previous". The report is the same as ruby, plain and with STRESS=1. Thread#[],[]=,key?,keys,fetch,thread_variable_*in 4 threads plus a Fiber, with GC in each thread. Head = base. Both differ from ruby in one pre-existing way (bug B).- Files: 30 Files written without close, 29 dropped, then GC, a write to the kept one, an unflushed
print, andexit. All file contents are the same as ruby, plain and with STRESS=1, on both engines. - One object with 400 ivars. This passes the 4 KB index limit, so
instance_variablesusesivar_names_walk. I ran it withMERE_RUBY_IVAR_INDEX_CHECK=1: no mismatch, and the same as ruby after remove/re-add and dup.
Instructions retired (run)
Median of 3, alternating base and head, MERE_RUBY_GC_TIGHT=1, both built with Mere 9dec2ff6.
| loop | evaluator | VM |
|---|---|---|
300 dup + clone among 20,000 objects of 4 ivars |
+0.68% | +0.77% |
20 GC.start over 100,000 objects of 4 ivars |
+0.08% | +0.25% |
| control (same dup/clone loop on Arrays, no ivars) | +0.09% | +0.15% |
The dup/clone cost is close to your +0.6%. My GC loop shows +0.1/+0.25%, while you measured -0.3/-0.2%. Both are within about 0.3% and look like noise to me. The ranges overlap for the evaluator, and nearly overlap for the VM.
Suggestions
- main.mere, about line 2412: the comment line "(...and a dead owner's names in ivar_names_of's index, iv_idx)" is now there twice (inline).
- The sweep comment says deleting dead keys "allocates nothing". The sweep now pushes three Vecs for each dead key instead of one. This is not a problem, but the comment may need a word.
Pre-existing bugs (the same on main, not caused by this PR)
- A.
instance_variable_set("@a@b", 1)is accepted and listed as:"@a@b". Ruby raises NameError. This is what makes the__f_fdcorner above reachable. - B. Fiber-locals and thread variables share one list.
Thread.new { Thread.current[:x] = 1; Thread.current.thread_variable_set(:z, 2); [Thread.current.keys, Thread.current.thread_variables] }.valuegives[[:x, :z], [:x, :z]]in mere-ruby and[[:x], [:z]]in ruby. The same happens on the main thread. Also,thread_variable_set(:tv, nil)removes:tvfromthread_variables, but ruby keeps it. Both values offb_tl_ownerare the thread's id when the root fiber runs, so the two kinds share "__tlk". 2b might be a good place to fix this.
Scope
- Note 559 §8 put the name index (
iv_idx) and the Prism loader (iv_put_new/iv_file_new) in 2a. This PR leaves them for 2b and says so in the loop record. That is fine with me. I checked the other whole-table walks in main.mere: they are all over String, Array, Hash, class, Proc or Binding keys, as the PR says.
Not checked
The full corpus, the gates, RSS, and Linux. I ran only small probes.
Merge state
git merge-tree --write-tree origin/main with this head: clean, no conflicts. CI at this head: 2 jobs passed, 1 running (Rake suite), 5 queued (CI, Gem suites, and others).
| let _ = drop_iv 0 in | ||
| let _ = (if nc then () else map_compact ivars) in | ||
| // (...and a dead owner's names in ivar_names_of's index, iv_idx) | ||
| // (...and a dead owner's names in ivar_names_of's index, iv_idx) |
There was a problem hiding this comment.
Suggestion: this comment line is the same as the line above it. One of the two can go.
284km
left a comment
There was a problem hiding this comment.
Follow-up on exact head 5ac43f11e855d04410ecc5c4703e936b2281eddb. No additional actionable code finding established. The existing same-head review covers the semantic probes and suggestions; this update records newly completed CI evidence rather than repeating those comments.
The Additional upstream gem tests / ubuntu-24.04 job has actually failed, before the original gem files ran. Its O2 build succeeded, using that workflow's pinned compiler 408b03d9. The failing step is Check anonymous Method-copy warning-state lifetime. I downloaded the method-copy-memory-Linux artifact and verified that its check SHA-256 matches the unchanged checker in this head:
- Reference: all 21 RSS samples and completion marker present; peak post-GC/initial ratio 1.080593, healthy.
- Mere: all 21 samples and completion marker present, exit 0 with no outer limit event; initial RSS 86,848 KiB, maximum collected sample 184,876 KiB, ratio 2.128731, exceeding the unchanged 2.0 bound. The checker therefore fails correctly. Original 600-second and 2-GiB family limits remain unchanged.
This is observed hosted execution, not a local sandbox restriction or solely runner queueing. It does not by itself establish that this PR caused the growth. The corresponding current-main workflow is pending with zero exposed jobs, so there is no completed same-main comparison here. Before merging, compare this unchanged check on current main and the prospective integration on the affected platform; retain this failure and the original bounds. No allocator/GC cause or repair is established.
Independent source checks in a fresh temporary clone: dead definitions, duplicate definitions, method writes, name lengths and namespace names pass; diff whitespace passes. I read the changed functions, helpers and callers against fetched main 1a3380b0e5738ee3ed4436c1eb94843e94ac9676. Neither fetched main nor head contains tracked AGENTS.md/CLAUDE.md instructions. The prospective merge is conflict-free (tree 4700b722b6f76842c91a914d182ab90fc3e9b01f), but it was not built or executed here. I did not independently repeat runtime probes, the full corpus, whole upstream files or performance/resource measurements.
No merge: the actual failed check, executing Linux build and incomplete downstream/macOS coverage do not qualify for the queue-only exception. Main CI run 45663174315 was inspected and exposes zero jobs while pending; it clears no recorded failure. The recorded hold_all_merges and new_failure_review_required conditions remain active. Already-reviewed ready PRs #254, #272 and #290 also retain failed checks; #290 has an unresolved Major/P2 code finding, and #272 retains its review blockers. The remaining open PRs are drafts; merelang/mere has no open PRs. COMMENT because the authenticated account owns this PR.
284km
left a comment
There was a problem hiding this comment.
Owner decision on the one number above main: dup+clone is +0.68% (evaluator) / +0.77% (VM) because iv_copy still walks the whole table. This is accepted as temporary for step 2a. Step 2b (shape + slab, note 559 §8) must show dup+clone at or below main. Everything else meets the merge criteria (no Critical/Major/Minor; corpus 955/955 plain and VM; gates; merge-tree clean; no bot inline comments). Merging.
Step 2a of note 559 (aidocs): #295 put every reader and writer of one object's variable behind
iv_get/iv_setand the rest. What still read an object's key"<id>@<name>"itself now asks the door too, so that step 2b (shapes, the values in one slab) changes what the door does and not who calls it. What is kept is unchanged: the keys are the same Strings.What moved
iv_key_owner(was the collector'sgc_owner_of),iv_key_name_at,iv_name_eq,iv_name,iv_del_at, in m_state besideiv_key.gc_fixpoint) and sweep (gc_collect) ask it. The sweep deletes a dead object's variables withiv_del_atand finds a File's descriptor byiv_name_eq .. "__f_fd".flush_files_at_exit,ivar_names_walkandiv_copy(dup, clone) ask it too;iv_copyno longer goes throughivars_copy_pfx, which is left to a Proc's and a Binding's keys.exc_cause_link_toanswers the exception's id (-1 for none) instead of a key, and its caller usesiv_get/iv_set .. "__cause".fb_tl_baseanswered a Thread's or a Fiber's id as text, andThread#[],#[]=,#key?,#keys,#fetch,thread_variable_*built<id>@__tl_<key>from it. It isfb_tl_ownernow (the id as a number), and those 11 sites go throughiv_*.The other walks of the table are of other owners' keys (a String's, an Array's, a Hash's, a class's, a Proc's, a Binding's), which step 2b leaves where they are.
A callback per variable is too dear
The first version had the door walk the table and call back with owner, name and value (a curried closure of four arguments). On 30 collections of 100,000 objects with four variables, that was +33% instructions, and +43% without the name's substring: each call of a curried closure through a parameter makes its partial applications. So the walks call
map_iterthemselves and ask the door only about a key.Measured
Instructions retired, median of 3 runs taken in turn,
MERE_RUBY_GC_TIGHT=1. Both binaries built with CI's Mere (9dec2ff6); the base is main with #295 (7554912).GC.startover 100,000 objects of 4 variablesdupandcloneamong 20,000 objectss += @x, 300,000 times@x = is += a.x(attr_accessor)The copy still walks the whole table, as before; step 2b makes it the object's own variables.
Checked
./run_corpus.sh --both: 955/955, plain and withMERE_RUBY_VM=1. The branch merges with main 1a3380b without a conflict, and vm_check.sh: the ruby on PATH when the rbenv build is not there (fixes #261's CI step) #296-vm_check.sh takes the reference ruby through tools/ref_ruby.sh (#296's review) #298 add no key of an object's.The record is
loop/2026-11-26-rails-ar-ivar-walks.md.🤖 Generated with Claude Code