Repository navigation
VM: a loop turn's end moves the collector's watermark (one-line loops' RSS) - #297
Merged
Merged
Conversation
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
commented
Nov 25, 2026
284km
left a comment
Owner
Author
There was a problem hiding this comment.
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), asvm_stepdoes 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=1underMERE_RUBY_GC_TIGHT=1,MERE_RUBY_GC_STRESS=1andMERE_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
--both955/955 plain and VM, vm_check 245/245, and the 8 gates after the rebase. git merge-treeagainst current main: clean. No mere-pr-review-watch inline comments.
Merging.
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.
#260's review found the VM's memory growing in a loop with no formatting in it at all:
Under
MERE_RUBY_GC_TIGHT=1the 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_stepwinds 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_stmtends 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 (mkstrfromvm_step) and the copiessetlocalmade into the frame's env.Neither the top level nor
whilewas the point: a one-line loop in a method or a block grew the same, and anInteger#timesblock 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:x + "c"not assigneds = [1]s = i.to_sWithout the tight setting, the median of three runs alternating main and this branch, 800,000 turns:
i += 1aloneThe 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) underMERE_RUBY_GC_TIGHT=1on 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.run_corpus.sh --bothandMERE_RUBY_VM=1 run_corpus.sh --both: 955/955 each, with the hash index on and off.tools/iseq/vm_check.sh: 245/245.🤖 Generated with Claude Code