Skip to content

ZJIT: Avoid accumulating send counters during specialization - #19485

Merged
XrXr merged 4 commits into
ruby:masterfrom
thomasklemm:thomasklemm/zjit-fallback-counter-once
Oct 29, 2026
Merged

XrXr merged 4 commits into
ruby:masterfrom
thomasklemm:thomasklemm/zjit-fallback-counter-once

Conversation

@thomasklemm

@thomasklemm thomasklemm commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

type_specialize revisits dynamic sends after inlining. Each visit currently appends another copy of the send's fallback counters, so --zjit-stats overcounts executions.

Track each counter instruction by its original send and remove it before retrying a send that remains dynamic. Keep counters for successful replacements, including when an inlined return aliases another dynamic send.

Run this with ruby --zjit --zjit-stats-quiet:

def helper(n) = n + 1
def target(**kw) = kw
def test = (helper(1); target)
100.times { test }
RubyVM::ZJIT.reset_stats!
9.times { test }
p RubyVM::ZJIT.stats.values_at(:complex_arg_pass_param_kwrest,
  :send_direct_fallback_context_send, :send_fallback_one_or_more_complex_arg_pass)

This prints [18, 18, 9] on the merge base and [9, 9, 9] with this change. The third counter is emitted by codegen and stays unchanged.

In the earlier railsbench comparison at 4fea886, the first two counters each fell from 749,107 to 250,038; the control stayed at 2,614,189. Both runs used 25 harness iterations: 15 warmup and 10 measured.

The Rust regression checks retry cleanup and preservation of successful splat counters. make zjit-check passes.

This is a counter-correctness fix, not a speedup claim. Three earlier stats-off chunky-png comparisons measured 2.3–5.7% longer times; normalized HIR and main-body instructions matched, and the cause of the timing difference remains unestablished.

The fixed-point optimizer can revisit a dynamic send after inlining.
Discard runtime counters from its previous specialization attempt before
retrying, while preserving counters for successful replacements.

Cover argument features, caller-splat profiles, send/super contexts and
unannotated C methods. Update snapshots containing duplicate counters and
add a runtime stats regression that also preserves optimized splat counts.
@matzbot
matzbot requested a review from a team October 22, 2026 19:14
@github-actions github-actions Bot added the jit YJIT, ZJIT, ... label Oct 22, 2026
Comment thread test/ruby/test_zjit_cli.rb Resolved
Comment thread zjit/src/hir.rs Resolved
Map each counter instruction directly to its original send and discard stale counters during the existing specialization traversal. This avoids rebuilding a set and sweeping the CFG before each pass.

Move the regression to the Rust optimizer tests, including the successful splat replacement whose return aliases another dynamic send.
Comment thread zjit/src/hir.rs Outdated
// Keep counters for successful replacements, even if an inlined call's
// return value resolves to another dynamic send.
if send_counters.get(&insn_id).is_some_and(|&send|
self.find_id(send) == send && matches!(self.insns[send], Insn::Send { .. } | Insn::InvokeSuper { .. })

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insn::Send { .. } | Insn::InvokeSuper { .. }

Having this only here seems a little fragile. Maybe introduce a function like .is_send() for insn and then assert send.is_send() in count_send?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Insn::is_send() (Send and InvokeSuper, the only kinds that get send counters), used it in this check, and added a debug_assert! in count_send and count_not_annotated_cfunc: d96bc24

Add Insn::is_send and use it both where counters are attached and where
type_specialize drops counters of sends that stay dynamic.
@XrXr
XrXr enabled auto-merge (squash) October 29, 2026 21:14
@XrXr
XrXr merged commit 8258233 into ruby:master Oct 29, 2026
135 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit YJIT, ZJIT, ...

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants