Repository navigation
ZJIT: Avoid accumulating send counters during specialization - #19485
Merged
XrXr merged 4 commits intoOct 29, 2026
Merged
Conversation
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.
k0kubun
reviewed
Oct 23, 2026
k0kubun
reviewed
Oct 23, 2026
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.
k0kubun
reviewed
Oct 26, 2026
| // 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 { .. }) |
Member
There was a problem hiding this comment.
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?
Contributor
Author
There was a problem hiding this comment.
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
k0kubun
approved these changes
Oct 26, 2026
Add Insn::is_send and use it both where counters are attached and where type_specialize drops counters of sends that stay dynamic.
XrXr
approved these changes
Oct 29, 2026
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.
type_specializerevisits dynamic sends after inlining. Each visit currently appends another copy of the send's fallback counters, so--zjit-statsovercounts 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: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-checkpasses.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.