Skip to content

ZJIT: Find caller-saved survivors without scanning every interval per CCall - #19681

Open
thomasklemm wants to merge 1 commit into
ruby:masterfrom
thomasklemm:thomasklemm/zjit-caller-saved-regs-stopgap
Open

thomasklemm wants to merge 1 commit into
ruby:masterfrom
thomasklemm:thomasklemm/zjit-caller-saved-regs-stopgap

Conversation

@thomasklemm

@thomasklemm thomasklemm commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

This change halves the time ZJIT spends compiling one method with 2,000 or 4,000 calls.

handle_caller_saved_regs() filtered every interval at every C call to find the registers live across it, so its cost is the number of C calls times the number of intervals. This PR maps each register interval's live ranges onto the sorted C call positions once, with a binary search per range, into one flat table. Each C call reads its slice of that table; there is no intermediate Vec per C call, and the survivor buffers are reused. Survivors keep the same VRegId order, so the generated code is unchanged.

This is a stopgap; it doesn't split intervals.

Fixes Shopify#1065

--zjit-stats at --zjit-call-threshold=2 for this script, which calls one method of N s ^= o.hash calls three times:

n = Integer(ARGV[0])
src = +"def big(o)\n  s = 0\n"
n.times { src << "  s ^= o.hash\n" }
src << "  s\nend\n"
eval(src)
o = Object.new
3.times { big(o) }

N=2000:

-compile_time:                                                    957ms
+compile_time:                                                    457ms
-compile_lir_time:                                                933ms
+compile_lir_time:                                                434ms

N=4000:

-compile_time:                                                  3,227ms
+compile_time:                                                 1,538ms
-compile_lir_time:                                              3,188ms
+compile_lir_time:                                             1,488ms

code_region_bytes doesn't change. On railsbench, lobsters and shipit, compile_time moved by less than run-to-run noise, so I left those diffs out.

@matzbot
matzbot requested a review from a team November 3, 2026 16:26
@github-actions github-actions Bot added the jit YJIT, ZJIT, ... label Nov 3, 2026
@thomasklemm
thomasklemm force-pushed the thomasklemm/zjit-caller-saved-regs-stopgap branch from dea97c3 to bb2c44d Compare November 5, 2026 00:27
thomasklemm added a commit to thomasklemm/lowrail that referenced this pull request Nov 5, 2026
Move ruby-bench #569-#572 from Merged to Open (ready), drop the duplicate
with_advisory_lock#150 entry, add ruby/date#199 and the closed ferrum#697,
mark ransack#1795 and tzinfo#175 ready, refresh heads (#59614, #59668,
ruby-bench#568, ruby/ruby#19681, tzinfo#171), add missing merge dates, and
record the 2026-11-05 open counts. RA1 item synced to ready.
@thomasklemm
thomasklemm force-pushed the thomasklemm/zjit-caller-saved-regs-stopgap branch 2 times, most recently from 5dedcb0 to 2e5b708 Compare November 15, 2026 11:20
… CCall

handle_caller_saved_regs() filtered all intervals for each CCall, which
is O(CCalls x intervals) and showed up in compile-time profiles of large
methods. Map each register interval's live ranges onto the sorted CCall
positions once, with a binary search per range, into one flat table of
covering intervals per position. Each CCall then reads its slice of that
table into reused survivor buffers, giving the same survivors in the
same VRegId order as before.

This is a stopgap for Shopify#1065; it
does not split intervals.
@thomasklemm
thomasklemm force-pushed the thomasklemm/zjit-caller-saved-regs-stopgap branch from 2e5b708 to 5df7fef Compare November 18, 2026 11:52
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.

ZJIT: Fix quadratic caller-saved regs in backend

1 participant