Repository navigation
A String's, an Array's and a Hash's instance variables in the shape and the slab (note 559, step 2b') - #336
Merged
Merged
Conversation
…nd the slab (note 559, step 2b') Step 2b put an object's variables in a shape and one slab. A String's, an Array's and a Hash's stayed in the table under "str:<id>@<name>" with no name index, so once any had one, every -str, dup, clone and $/ = of any String, Array or Hash walked the whole table. Under activerecord that was about a third of a sqlite3 execute (its C asks -@ of each column name). - The door takes an owner key (id * 8 + kind: 0 object, 1 String, 2 Array, 3 Hash), as the collector's live set spells it; obj_iv is keyed by it. iv_k_get_or / iv_k_set / iv_k_has / iv_k_del take it; hiv_ok gives a String's, an Array's or a Hash's. - ivs_*, instance_variable_get / set / defined? / remove / instance_variables, dup and clone (iv_k_copy_all), -str and $/ = go through it. The walk of the table is left to an owner in the table. - pack 'P' / 'p' keeps the Strings it points at under __pk_assoc behind the door, in the packed String's own area (pack_assoc_key is gone). - The collector marks a String's, an Array's or a Hash's area through gc_obj_pend (its key negated), and the sweep gives a dead one's back: on main they outlived it (loop_rss_check "a String's variables": 2.16 on main, 1.00 here). iv_hng, the owner keys of those with a record, keeps the mark's check off obj_iv. - tools/iv_door_check.sh also looks for "str:" / "arr:" / "hsh:" keys and a name joined to "@" built by hand, and says it reads the spelling only. - The VM's inline cache (#332) wrote an object's record under its bare id (map_set obj_iv oid), some other owner's key now: the object kept its old shape and later objects read wrong values. It goes through iv_rec_put (oid * 8), and iv_door_check fails on map_set obj_iv outside m_state. corpus/1067 is the case. - corpus/1066: their variables' order, removal, dup / clone, -str, a subclass's initialize, Strings dropped and collected, and pack's pointers. Against main 018c056 (Mere 9dec2ff6, CPU time a call): -("c"+"ol") 0.030 -> 0.003 ms, dup and clone 0.037-0.041 -> 0.009-0.011 ms, $/ = 0.105 -> 0.069 ms, select_value 4.7-5.0 -> 4.1-4.2 ms. The objects' loops are within 1%; a loop of GC.start is +0.5% / +1.0% (evaluator / VM).
The conflict: ruby2_keywords_hash's copy, which here copies the variables through the door (iv_k_copy_all) and on main no longer calls mark_kw.
#242 copies a ruby2_keywords Hash for Method#call (r2k_hash_copy) and took its instance variables from table keys "hsh:<id>@<name>", listed per Hash in hash_ivar_names. Since step 2b' a Hash's variables are in its area, so the copy came back with none. It goes through iv_k_copy_all, which also covers a Hash still in the table, and hash_ivar_names -- kept by iv_put, the sweep and remove_instance_variable for that copy alone -- is gone.
284km
commented
Nov 26, 2026
284km
left a comment
Owner
Author
There was a problem hiding this comment.
Review of 7bdb2de2 (note 559 step 2b′). No Critical/Major/Minor findings.
What I checked:
- Owner keys: id×8 plus a kind (0 object, 1 String, 2 Array, 3 Hash), matching the collector's live-set key.
iv_rec_putis now the only writer ofobj_iv, so the #332 cache's direct write is fixed (corpus 1067), andiv_door_check.shwould catch a new one. The object API (iv_get/iv_set/ …) is a thinoid * 8wrapper over theiv_k_*door. - Collector: a marked String/Array/Hash with variables is queued as a negated owner key and its area walked. The sweep tests
gc_live_has live oon the owner key directly, frees the dead owner's area and drops it fromiv_hng. That closes the handle-reuse hole the comment describes. - Copies:
iv_k_copy_allreplaces the three prefix walks (String/Arraydup/clone, Hashdup,r2k_hash_copy) andhash_ivar_names.-@and$/ =askiv_k_has_anyinstead of building a Map and walking the table. - Readers of the table: I grepped the head for any remaining
"str:" / "arr:" / "hsh:" ++ id ++ "@"orhng_key … ++ "@"construction outsideivar_self_key(whose String/Array/Hash arms are no longer reached) and the door. There are none, including in the C API and tools. - Listings and removal:
instance_variables,remove_instance_variable,instance_variable_get/set/defined?on a shaped String/Array/Hash go through the door and hide internal names.
Recorded small exception: the GC-heavy ivperf loops are +0.5% (evaluator) / +1.0% (VM). In exchange, -@ goes from 0.030 to 0.003 ms, dup/clone of String/Array/Hash from 0.037–0.041 to 0.009–0.011 ms, and select_value from 4.7–5.0 to 4.1–4.2 ms. I accept it and will report it.
The test merge with current main is clean. With corpus 982/982 on both engines on top of main 7a733b48 (no writes around the door), and the six-mode results on 89ad241c posted, this is ready.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Note 559, step 2b'. Step 2b (#322) put an object's instance variables in a shape and one slab. A String's, an Array's and a Hash's stayed in the table under
"str:<id>@<name>"with no name index, so once any String, Array or Hash had one, every-str,dup,cloneand$/ =of any of them walked the whole table. Under activerecord (sqlite3), whose C asks-@of each column name, that was about a third of anexecute(bc's measurement).id * 8 + kind(0 object, 1 String, 2 Array, 3 Hash), as the collector's live set spells it;obj_ivis keyed by it.iv_k_get_or/iv_k_set/iv_k_has/iv_k_deltake it, andhiv_ok vgives a String's, an Array's or a Hash's.ivs_*,instance_variable_get/set/defined?/remove_instance_variable/instance_variables,dup/clone(iv_k_copy_all),-strand$/ =go through the door. Walking the table is left to owners in the table (too complex).P/pkeep the Strings they point at under__pk_assoc, behind the door, in the packed String's own area.pack_assoc_keyis gone.gc_obj_pend(its key negated), and the sweep gives a dead one's back. On main their variables outlived them.iv_hng(the owner keys of those with a record, kept byiv_rec_put) keeps the mark's check offobj_iv.map_set obj_iv oid, the bare id, which is some other owner's key here. On the VM an object kept its old shape and later objects read wrong values. It goes throughiv_rec_put (oid * 8)now. Corpus 1067 is the case: with the old line, 50 objects'xsummed to 40349 (ruby 1275).Method#call(r2k_hash_copy) took the Hash's variables from table keys listed inhash_ivar_names; here they are in the Hash's area, so the copy got none. It goes throughiv_k_copy_all, andhash_ivar_namesis gone (it existed for that copy alone). The branch merges main rather than being rebased, since it was already pushed.tools/iv_door_check.shalso fails on a"str:"/"arr:"/"hsh:"key or a name joined to"@"built by hand, and on anymap_set obj_ivoutside m_state. ⚠ It reads the spelling only (An object's instance variables in a shape and one slab (note 559, step 2b) #322's review), and says so.-str, a String subclass'sinitialize, Strings dropped and collected, and pack's pointers. It matches ruby in all six modes below. 1067 is the cache case above.Measured
Both sides built with the Mere CI uses (9dec2ff6).
Per call, against main 018c056. activerecord's test helper and
models/post(sqlite3) are loaded; the figure is the process's CPU time a call, the least of 5 rounds. Main and this ran in turn, twice; both runs shown.-("c"+"ol")"abc".dup{}.dup[].dup"abc".clone{}.clone[].clone$/ = "\n"raw_connection.execute(5 rows)connection.select_valuePost.first(Against main 89ad241, before the rebase, by the wall clock:
-@0.0335 → 0.0045 ms,"abc".dup0.0475 → 0.0127,{}.dup0.0432 → 0.0105,[].dup0.0466 → 0.0092,$/ =0.1225 → 0.0793,raw.execute0.107 → 0.081,select_value6.16 → 5.72,Post.first11.60 → 10.65.)Instructions retired, against main 89ad241. The median of 3 runs taken in turn,
MERE_RUBY_GC_TIGHT=1, using 2b's object loops:dupandcloneamong 20,000 objectss += @x@x = is += a.x(attr_accessor)GC.startover 100,000 objects of 4 variablesThe GC loop's cost is one check for each String, Array or Hash marked: does it have an area? Asking
obj_ivcost +1.2% on the VM;iv_hngbrought it to +1.0%.tools/loop_rss_check.rb's "a String's variables" ends at 2.16× its starting RSS on main (FAIL) and 1.00× here.tools/shape_class_tables_check.rbleaves 3 and 32 keys.Corpus
On main 89ad241 (before the rebase), 973 files:
MERE_RUBY_SHAPES_CHECK=1, on the evaluator and the VM, with the hash index on and off: 973/973, no write around the door.MERE_RUBY_SHAPES=0andMERE_RUBY_SHAPES_MAX=8: 973/973.MERE_RUBY_GC_STRESS=50: 7 wrong on the evaluator and 8 on the VM, each the same as on main:On main 018c056, and again with main 7a733b4 merged (982 files):
MERE_RUBY_SHAPES_CHECK=1, on the evaluator and the VM, with the hash index on and off: all files match, no write around the door, 1066 and 1067 included. #242's 987 (ruby2_keywords copies) matches ruby on both engines.The record is
loop/2026-11-26-rails-ar-ivar-hng-shapes.md.🤖 Generated with Claude Code