Skip to content

A String's, an Array's and a Hash's instance variables in the shape and the slab (note 559, step 2b') - #336

Merged
284km merged 3 commits into
mainfrom
rails-ar-ivar-hng-shapes
Nov 26, 2026
Merged

284km merged 3 commits into
mainfrom
rails-ar-ivar-hng-shapes

Conversation

@284km

@284km 284km commented Nov 26, 2026

Copy link
Copy Markdown
Owner

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, clone and $/ = of any of them walked the whole table. Under activerecord (sqlite3), whose C asks -@ of each column name, that was about a third of an execute (bc's measurement).

  • The owner key. The door takes 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, and hiv_ok v gives 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), -str and $/ = go through the door. Walking the table is left to owners in the table (too complex).
  • pack. P / p keep the Strings they point at under __pk_assoc, behind the door, in the packed String's own area. pack_assoc_key is gone.
  • The collector. A String's, an Array's or a Hash's area is marked through 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 by iv_rec_put) keeps the mark's check off obj_iv.
  • VM: an instance variable's inline cache (note 559, step 2c) #332 (step 2c), merged while this was in review. The VM's inline cache wrote an object's record as 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 through iv_rec_put (oid * 8) now. Corpus 1067 is the case: with the old line, 50 objects' x summed to 40349 (ruby 1275).
  • Preserve Hash state and keyword flags across callable boundaries #242, merged after the branch was pushed. Its ruby2_keywords Hash copy for Method#call (r2k_hash_copy) took the Hash's variables from table keys listed in hash_ivar_names; here they are in the Hash's area, so the copy got none. It goes through iv_k_copy_all, and hash_ivar_names is gone (it existed for that copy alone). The branch merges main rather than being rebased, since it was already pushed.
  • tools/iv_door_check.sh also fails on a "str:" / "arr:" / "hsh:" key or a name joined to "@" built by hand, and on any map_set obj_iv outside 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.
  • Corpus. 1066 covers order, removal, dup / clone, -str, a String subclass's initialize, 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.

call main 018c056 this
-("c"+"ol") 0.0309 / 0.0294 ms 0.0027 / 0.0028 ms
"abc".dup 0.0368 / 0.0386 ms 0.0086 / 0.0098 ms
{}.dup 0.0394 / 0.0387 ms 0.0093 / 0.0107 ms
[].dup 0.0402 / 0.0393 ms 0.0090 / 0.0094 ms
"abc".clone 0.0411 / 0.0375 ms 0.0093 / 0.0097 ms
{}.clone 0.0395 / 0.0368 ms 0.0092 / 0.0098 ms
[].clone 0.0412 / 0.0378 ms 0.0086 / 0.0093 ms
$/ = "\n" 0.1060 / 0.1049 ms 0.0678 / 0.0698 ms
raw_connection.execute (5 rows) 0.0840 / 0.0793 ms 0.0761 / 0.0845 ms
connection.select_value 4.72 / 5.00 ms 4.09 / 4.21 ms
Post.first 8.74 / 8.78 ms 8.56 / 7.90 ms

(Against main 89ad241, before the rebase, by the wall clock: -@ 0.0335 → 0.0045 ms, "abc".dup 0.0475 → 0.0127, {}.dup 0.0432 → 0.0105, [].dup 0.0466 → 0.0092, $/ = 0.1225 → 0.0793, raw.execute 0.107 → 0.081, select_value 6.16 → 5.72, Post.first 11.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:

loop evaluator VM
300 dup and clone among 20,000 objects -0.0% +0.0%
so_object +0.1% +0.1%
s += @x -0.5% -0.6%
@x = i -0.4% -0.7%
s += a.x (attr_accessor) -0.6% -0.7%
control ±0 ±0
30 GC.start over 100,000 objects of 4 variables +0.5% +1.0%

The GC loop's cost is one check for each String, Array or Hash marked: does it have an area? Asking obj_iv cost +1.2% on the VM; iv_hng brought 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.rb leaves 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=0 and MERE_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:
    • 526 prints the same wrong line as on main.
    • 214, 512, 524, 656, 659, 676 and 988 run past 600 s on main as well. 512 takes 695 s on main, and its output matches ruby's.
    • 917 (VM) counted 118 of its 120 connections, because each worker stops after 3 s. Run alone 3 times: 120, 120, 120 here and 120, 115, 120 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

…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 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 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_put is now the only writer of obj_iv, so the #332 cache's direct write is fixed (corpus 1067), and iv_door_check.sh would catch a new one. The object API (iv_get / iv_set / …) is a thin oid * 8 wrapper over the iv_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 o on the owner key directly, frees the dead owner's area and drops it from iv_hng. That closes the handle-reuse hole the comment describes.
  • Copies: iv_k_copy_all replaces the three prefix walks (String/Array dup/clone, Hash dup, r2k_hash_copy) and hash_ivar_names. -@ and $/ = ask iv_k_has_any instead of building a Map and walking the table.
  • Readers of the table: I grepped the head for any remaining "str:" / "arr:" / "hsh:" ++ id ++ "@" or hng_key … ++ "@" construction outside ivar_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.

@284km
284km merged commit 097b8ec into main Nov 26, 2026
9 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