From: "dkam (Dan Milne) via ruby-core" Date: 2026-11-21T06:25:05+00:00 Subject: [ruby-core:127658] [Ruby Bug#22617] YJIT: method keeps exiting to the interpreter after a constant cache in it is filled or cleared Issue #22617 has been reported by dkam (Dan Milne). ---------------------------------------- Bug #22617: YJIT: method keeps exiting to the interpreter after a constant cache in it is filled or cleared https://bugs.ruby-lang.org/issues/22617 * Author: dkam (Dan Milne) * Status: Open * ruby -v: ruby 4.1.0dev (2026-11-21T06:18:13Z master b84532add7) +YJIT +PRISM [x86_64-linux] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- A method compiled by YJIT can get stuck exiting to the interpreter on every call, once an inline constant cache it reads is filled after compilation or cleared by a constant of the same name being defined elsewhere. Everything after that point in the method then runs in the interpreter, permanently. In the script below a call goes from 1.2 �s to 18.5 �s, which is the interpreter's speed. It happens on 4.0.10 and on master (b84532add7), and `--yjit-stats` hides it. The cause appears to be the adjacency check in `invalidate_block_version`. Padding between a fall-through branch and the block being invalidated makes the check fail, so the branch keeps falling through into the invalidated block's exit. Details are below. ### Reproduction `ruby --yjit yjit_dead_block_fallthrough.rb` (attached) ```ruby class Scanner SEP = " " def run(str, detour) state = detour ? 1 : 2 i = 0 len = str.bytesize while i < len if state == 1 i += SEP.bytesize state = 2 end i += 1 end i end end STR = "x" * 1_000 def per_call_us(scanner) n = 20_000 t = Process.clock_gettime(Process::CLOCK_MONOTONIC) n.times { scanner.run(STR, true) } (Process.clock_gettime(Process::CLOCK_MONOTONIC) - t) / n * 1e6 end puts RUBY_DESCRIPTION unless defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled? printf "interpreter: %.2f us per detour call\n", per_call_us(Scanner.new) exit end # Each scenario runs in a fresh process. vm_insns_count is only counted on # stats and dev builds. def scenario(label) pid = fork do scanner = Scanner.new yield scanner before = RubyVM::YJIT.runtime_stats[:vm_insns_count].to_i us = per_call_us(scanner) stats = RubyVM::YJIT.runtime_stats interp = (stats[:vm_insns_count].to_i - before) / 20_000.0 printf "%-52s %6.2f us per detour call invalidations: %d%s\n", label, us, stats[:invalidation_count].to_i, stats[:vm_insns_count] ? format(" interpreter insns per call: %.0f", interp) : "" end Process.wait(pid) end scenario("detour calls first (cache filled before compiling)") do |s| 100.times { s.run(STR, true) } end scenario("plain calls first (cache filled after compiling)") do |s| 100.times { s.run(STR, false) } 100.times { s.run(STR, true) } end scenario("detour calls first, then another SEP is defined") do |s| 100.times { s.run(STR, true) } Module.new.const_set(:SEP, 1) # clears every cache for the name SEP 100.times { s.run(STR, true) } end scenario("detour calls first, then an unrelated constant") do |s| 100.times { s.run(STR, true) } Module.new.const_set(:OTHER, 1) 100.times { s.run(STR, true) } end ``` ### Result x86_64 Linux, release builds: ``` ruby 4.0.10 (2026-11-19 revision a704b7cd95) +YJIT +PRISM [x86_64-linux] detour calls first (cache filled before compiling) 1.21 us per detour call invalidations: 0 plain calls first (cache filled after compiling) 18.54 us per detour call invalidations: 1 detour calls first, then another SEP is defined 18.58 us per detour call invalidations: 2 detour calls first, then an unrelated constant 1.22 us per detour call invalidations: 0 ruby 4.1.0dev (2026-11-21T06:18:13Z master b84532add7) +YJIT +PRISM [x86_64-linux] detour calls first (cache filled before compiling) 1.20 us per detour call invalidations: 0 plain calls first (cache filled after compiling) 18.52 us per detour call invalidations: 1 detour calls first, then another SEP is defined 18.57 us per detour call invalidations: 2 detour calls first, then an unrelated constant 1.19 us per detour call invalidations: 0 ruby 4.0.10 (2026-11-19 revision a704b7cd95) +PRISM [x86_64-linux] (without --yjit) interpreter: 18.53 us per detour call ``` A `--enable-yjit=dev_nodebug` build of b84532add7 counts the interpreter's instructions. After either trigger, about 12,000 instructions per call run in the interpreter, against 10 otherwise: ``` ruby 4.1.0dev (2026-11-21) +YJIT dev_nodebug +PRISM [x86_64-linux] detour calls first (cache filled before compiling) 1.19 us per detour call invalidations: 0 interpreter insns per call: 10 plain calls first (cache filled after compiling) 21.34 us per detour call invalidations: 1 interpreter insns per call: 12005 detour calls first, then another SEP is defined 21.35 us per detour call invalidations: 2 interpreter insns per call: 12005 detour calls first, then an unrelated constant 1.20 us per detour call invalidations: 0 interpreter insns per call: 10 ``` With `--yjit-stats`, all four scenarios take about 25 �s and the invalidation counts are the same, so the problem can't be seen in stats mode. ### Expected result All four scenarios should take about 1.2 �s per call. After the cache is filled or cleared, YJIT should compile a new version of the invalidated block and link the block before it to the new one, as it does in the first scenario. ### Cause The relevant part of `Scanner#run`: ``` 0033 getlocal_WC_0 i@3 ( 10)[Li] 0035 opt_getconstant_path 0037 opt_send_without_block ``` In the second and third scenarios, the block for instruction 35 is compiled with SEP's cache empty, or is invalidated when the cache is cleared. The block before it, `[33,35)`, ends in a zero-length `Next0` branch followed by 2 bytes of padding. So the invalidated block starts 2 bytes after `branch.end_addr`. Logging added to `invalidate_block_version` shows this: ``` invalidate iseq_range=[35,37) start=0x564f4856c33b end=0x564f4856c379 incoming=1 incoming #0 from block iseq_range=[33,35) branch=[0x564f4856c339,0x564f4856c339) adjacent=false shape=Next0 targets_pointing_at_this_block=1 -> stub=0x564f4856ebbc (is entry exit: false) shape now Next0 branch now [0x564f4856c339,0x564f4856c339) ``` `target_next` (`block.start_addr == branch.end_addr.get()`) is false, so the branch keeps the `Next0` shape. Regenerating it emits nothing, and the new stub is never jumped to. Execution falls over the padding into the start of the invalidated block, which was patched to jump to its entry exit, so every call exits to the interpreter at instruction 35. `RubyVM::YJIT.disasm` agrees: afterwards there's no live block for instruction 35, and the bytes after `[33,35)` are the old block's code with a `jmp` to an exit written over its start. As an experiment, treating a branch whose shape falls through to the target as adjacent fixes both triggers. With it, the second and third scenarios take 1.42 �s and 1.19 �s, with 10 interpreter instructions per call. It also fixed the larger code where we first saw the problem (below). We haven't run YJIT's test suite with it, so it's offered as a pointer rather than a patch: ```diff --- a/yjit/src/core.rs +++ b/yjit/src/core.rs @@ -4268,8 +4268,13 @@ ctx: block.ctx, }))))); - // Check if the invalidated block immediately follows - let target_next = block.start_addr == branch.end_addr.get(); + // Check if the invalidated block immediately follows. Padding can sit + // between a fall-through branch and its target, so check the shape too. + let falls_through = matches!( + (branch.gen_fn.get_shape(), target_idx), + (BranchShape::Next0, 0) | (BranchShape::Next1, 1) + ); + let target_next = block.start_addr == branch.end_addr.get() || falls_through; if target_next { if stub_addr != block.start_addr { ``` This may be related to #22225. There, a branch can be regenerated while one of its targets still points at an invalidated block, and the patched-in exit keeps that correct. Here the branch has only one target, and the exit is correct too, but it's taken on every call and never relinked. ### Where we found it It came up in a pure-Ruby HTTP request parser we're writing for Puma: a 340-line `while`/`case` state machine with constants throughout. If YJIT compiled `#execute` before any request with a query string had been parsed, each later request with one left JIT code at the first constant read in the query-string branch. That was about 6,000 of the roughly 11,000 instructions in a parse running in the interpreter, and a typical browser GET went from 17 �s to 32 �s. Lazily loaded code that defines a constant with the same name as one of the parser's (for example `SPACE`) had the same effect. Whether it appears depends heavily on code layout: removing an unrelated state from the state machine could make it disappear. Investigated and drafted with Claude Code (Claude Opus 5.5). -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/