Skip to content

VM: a loop turn's end moves the collector's watermark (one-line loops' RSS) - #297

Merged
284km merged 1 commit into
mainfrom
stage10-vm-loop-watermark
Nov 25, 2026
Merged

284km merged 1 commit into
mainfrom
stage10-vm-loop-watermark

Conversation

@284km

@284km 284km commented Nov 25, 2026

Copy link
Copy Markdown
Owner

#260's review found the VM's memory growing in a loop with no formatting in it at all:

n = ARGV[0].to_i; x = "ab"; i = 0; while i < n; s = x + "c"; i += 1; end; p s

Under MERE_RUBY_GC_TIGHT=1 the VM's maximum RSS was 101 MB at 100,000 turns and 301 MB at 800,000, about 300 bytes a turn; the evaluator's was flat. The same loop written over several lines was flat on the VM too.

Why the line mattered

The collector does not take what was made since the current statement of a frame began: such a value may still be held by a C frame of the statement (the statement watermark, fb_wm). A statement's start moves the mark (fb_wm_stmt). On the VM, an instruction that starts a statement (a line event) with nothing on the stack is that start: vm_step winds the stack back, moves the mark and lets a collection run.

A loop on one line has no statement start inside it. For that, vm_run_stmt ends a turn at one backward jump in 64 and lets a collection run there, but it did not move the mark. So everything every turn made stayed younger than the mark, and nothing of it was collected. The allocations that stayed were the Strings the turns made (mkstr from vm_step) and the copies setlocal made into the frame's env.

Neither the top level nor while was the point: a one-line loop in a method or a block grew the same, and an Integer#times block does not, since each call of the block starts a statement.

The change

At a turn's end, with nothing on the stack, the mark moves too (fb_wm_stmt), as at a statement's start: the turn's values are on the VM's stack, which the collector reads, or in the env.

Numbers

Maximum RSS under MERE_RUBY_GC_TIGHT=1, on the VM:

program turns main here
the loop above 100,000 101.0 MB 86.5 MB
800,000 301.1 MB 86.6 MB
the same, x + "c" not assigned 800,000 301.0 MB 88.5 MB
s = [1] 800,000 263.2 MB 85.0 MB
s = i.to_s 800,000 219.0 MB 84.3 MB
the loop in a method 800,000 282.4 MB 88.8 MB
the loop in a block 800,000 285.8 MB 92.9 MB
the loop over several lines 800,000 86.3 MB 86.4 MB

Without the tight setting, the median of three runs alternating main and this branch, 800,000 turns:

program main here
the loop above 225.5 MB, 12.71 G instructions 106.7 MB, 12.95 G
the same over several lines 106.5 MB, 14.00 G 106.5 MB, 14.00 G
the loop in a method 277.9 MB, 12.92 G 103.5 MB, 12.68 G
i += 1 alone 92.0 MB, 2.42 G 92.1 MB, 2.42 G

The instructions move by the collector's own work, which now finds something to take.

Recorded

A loop that makes nothing, while i < n; i += 1; end, still grows a little on the VM, and as much over several lines: 70 MB at 100,000 turns, 91.5 MB at 800,000, 99.6 MB at 1,600,000, under the tight setting (the evaluator: 69.7, 74.6, 74.7). It slows as it goes; not this round's.

Checked

Against ruby 4.0.10, built with Mere 9dec2ff6, on main 8c6699f (#294 and #295 in):

  • ctl.rb (the loop above) under MERE_RUBY_GC_TIGHT=1 on the VM, measured again after the rebase: main 101.0 MB at 100,000 turns and 301.1 MB at 800,000; here 86.3 MB and 86.5 MB.
  • Instructions at 800,000 turns, without the tight setting, median of three runs alternating main 8c6699f and this branch: the loop above 12.703 G -> 12.947 G (+1.9%), the same over several lines 13.994 G -> 13.995 G, the loop in a method 12.897 G -> 12.673 G (-1.7%). The one-line loop now does the collector's work the multi-line loop already did.
  • run_corpus.sh --both and MERE_RUBY_VM=1 run_corpus.sh --both: 955/955 each, with the hash index on and off.
  • tools/iseq/vm_check.sh: 245/245.
  • The source gates (dead_defs, dup_defs, gc_roots, meth_writes, name_len, ns_names, bracket_depth, record_hygiene) each exit 0.

🤖 Generated with Claude Code

A loop on one line has no statement start inside it. vm_run_stmt ended a
turn at one backward jump in 64 and let a collection run, but did not move
the statement watermark (fb_wm_stmt), so everything the turns made stayed
younger than the mark and was not collected: about 300 bytes a turn,
`while i < n; s = x + "c"; i += 1; end` at 301 MB for 800,000 turns under
MERE_RUBY_GC_TIGHT=1. With nothing on the stack the mark moves there too:
86.6 MB, as at 100,000 turns and as the same loop over several lines.

@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.

Reviewed da19493. No Critical / Major / Minor findings.

  • At the VM's 64-turn boundary of a loop, when the operand stack is empty, the turn now also advances the statement watermark (fb_wm_stmt), as vm_step does at a line. A loop on one line had made every value of every turn look live to the collector (ctl.rb: 101 MB at 100k turns, 301 MB at 800k); now both are about 86 MB.
  • GC safety: the mark only moves when the stack is empty, and locals stay rooted through the environment. I ran a one-line-loop probe on the author's build with MERE_RUBY_VM=1 under MERE_RUBY_GC_TIGHT=1, MERE_RUBY_GC_STRESS=1 and MERE_RUBY_GC_STRESS=50. Strings and arrays built in the loop and kept in an Array, a Hash (as key and value), a growing String, and a method's local Array all match ruby 4.0.10.
  • Instructions (median of 3 alternating): the one-line loop +1.9%, the multi-line loop unchanged, the one-line loop in a method −1.7%. The description says why: collection now actually runs in the one-line loop.
  • The description has the local results our merge policy asks for: corpus --both 955/955 plain and VM, vm_check 245/245, and the 8 gates after the rebase.
  • git merge-tree against current main: clean. No mere-pr-review-watch inline comments.

Merging.

@284km
284km merged commit f965e63 into main Nov 25, 2026
5 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