From: "runlevel5 (Trung Le) via ruby-core" Date: 2026-11-27T01:40:41+00:00 Subject: [ruby-core:127743] [Ruby Bug#22649] ZJIT: compile time is quadratic in method size, in handle_caller_saved_regs Issue #22649 has been reported by runlevel5 (Trung Le). ---------------------------------------- Bug #22649: ZJIT: compile time is quadratic in method size, in handle_caller_saved_regs https://bugs.ruby-lang.org/issues/22649 * Author: runlevel5 (Trung Le) * Status: Open * ruby -v: ruby 4.1.0dev (2026-11-27) +ZJIT +PRISM [aarch64-linux] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- ## Summary While fuzzing ZJIT with generated programs I noticed that long methods take a surprisingly long time to compile. I tracked it down to `Assembler::handle_caller_saved_regs`: for each `CCall` it looks at every interval of the function to find the registers that have to be saved around the call, so compile time is proportional to the number of C calls times the number of virtual registers. In a release build, a method of 4,000 statements takes 5.6 seconds to compile for me, almost all of it in that function. One of my generated programs, 2,700 lines long, runs in 0.04s in the interpreter and takes 6.8s with `--zjit-call-threshold=1`. ## Environment I used master as of 40abfd3322 on arm64 Linux (Ubuntu 24.04 in Docker, Rust 1.96.1), configured with `--enable-zjit`: ``` ruby 4.1.0dev (2026-11-27) +ZJIT +PRISM [aarch64-linux] ``` My builds also have the fixes I proposed in [Bug #22641] and [Bug #22648], which do not touch the backend. The timings are from the release build; I ran the tests mentioned at the end in a `--enable-zjit=dev` build. ## Steps to reproduce ```ruby # slow.rb n = (ARGV[0] || 2000).to_i eval "def big(a)\n x = 0\n#{" x += a.to_s.size\n" * n} x\nend" t = Process.clock_gettime(Process::CLOCK_MONOTONIC) 10.times { big(1) } printf "N=%d %.2fs\n", n, Process.clock_gettime(Process::CLOCK_MONOTONIC) - t ``` ``` $ ./miniruby slow.rb 4000 N=4000 0.00s $ for n in 1000 2000 4000 8000; do ./miniruby --zjit-call-threshold=2 slow.rb $n; done N=1000 0.27s N=2000 1.14s N=4000 5.64s N=8000 24.21s ``` Each time I double `N`, it takes four to five times as long. With `--zjit-stats`, `N=8000` reports `compile_time: 26,772ms` and `compile_lir_time: 26,683ms`. ## Where the time goes I ran `callgrind` on `slow.rb 1000`. Self cost by function: ``` 48.26% for &mut F>::call_mut 27.68% core::hash::BuildHasher::hash_one 9.16% as core::hash::Hasher>::write 3.08% as SpecFromIterNested>::from_iter ``` As far as I can tell, `call_mut` is the closure that picks the survivors of a `CCall` in `zjit/src/backend/lir.rs`, and the hashing is the set lookup it makes: ```rust let survivors: Vec = intervals.iter() .filter(|interval| { let live_across_call = Some(interval.vreg_id) != out_vreg_id && interval.covers(insn_number); let stack_map_reg = stack_vreg_ids.contains(&interval.vreg_id); let is_register = interval.assigned.get().and_then(|alloc| alloc.alloc_pool_index(alloc_regs)).is_some(); is_register && (live_across_call || stack_map_reg) }) ``` It runs once per `CCall` over all intervals, and for each interval walks its ranges and does a hash lookup. ## Possible fix `CCall`s are visited in the order instructions are numbered, so I tried keeping the intervals that can be live at a call with a sweep: I sort the intervals that are in an allocatable register by start, add those that have started as the calls go by, and drop those that have ended. Only a handful of intervals are active at any time. I add the stack map entries from the stack map itself, and sort the result by `VRegId` so the survivors come out in the same order as before. With that change (patch attached) I get: | | before | after | |---|---|---| | `slow.rb 1000` | 0.27s | 0.08s | | `slow.rb 2000` | 1.14s | 0.19s | | `slow.rb 4000` | 5.64s | 0.49s | | `slow.rb 8000` | 24.21s | 1.52s | | generated program, 2,700 lines | 6.75s | 0.92s | For my generated program `compile_lir_time` goes from 6,511ms to 437ms, and `code_region_bytes` is the same before and after (4,063,232), so I believe the generated code is unchanged. With the change, `make zjit-test` passes for me with no snapshot changed, and so does `make btest RUN_OPTS=--zjit-call-threshold=1`. What is left still grows faster than linearly, but it is much smaller. I'm happy to rework the patch if you would prefer a different approach. -- 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/