From de85acc33bd099e3f3477ab447bcf3975ea56029 Mon Sep 17 00:00:00 2001 From: Claude Code Date: Wed, 30 Sep 2026 20:09:20 +0000 Subject: [PATCH 1/6] zjit_diff: compare iteration timings with GC time removed Benchmark iterations are supposed to do the same work, but GC work throws a ranch into that. For changes like https://github.com/ruby/ruby/pull/18960 that don't modify the GC's workload at all, it's useful to evaluate the GC time separately. --- misc/zjit_diff.rb | 88 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 14 deletions(-) diff --git a/misc/zjit_diff.rb b/misc/zjit_diff.rb index 1b4acda5..9151f65f 100755 --- a/misc/zjit_diff.rb +++ b/misc/zjit_diff.rb @@ -6,6 +6,9 @@ # By default some categories limit how many entries are shown # and some results only appear when the comparison passes a threshold. # Pass --help to see options. +# +# If the results were recorded with the GC harness (--harness=gc), iteration +# timings are also compared with GC time removed. require 'json' require_relative '../lib/ractor_breakdown' @@ -15,6 +18,9 @@ class ZjitDiff DEFAULT_MINIMUM_DIFF = 100 # Minimum absolute difference to report DEFAULT_LIMIT = nil # Show all by default (nil = no limit) + # Per-iteration GC times (ms) recorded by harness-gc + GC_TIME_KEYS = %w[gc_marking_time_bench gc_sweeping_time_bench].freeze + # Categories matching zjit.rb stats_string output order STAT_CATEGORIES = [ { prefix: 'not_inlined_cfuncs_', prompt: 'not inlined C methods', limit: 10 }, @@ -102,6 +108,7 @@ def run print_header print_benchmark_timings + print_benchmark_timings_without_gc print_memory_usage print_send_counters print_summary_counters @@ -213,26 +220,79 @@ def print_benchmark_timings next if stats.empty? - baseline = stats.first - fastest = stats.min_by { |s| s[:avg] } + print_timing_rows(stats) + end + puts + end - stats.each do |s| - diff_pct = ((s[:avg] - baseline[:avg]) / baseline[:avg] * 100) - is_fastest = s == fastest && stats.size > 1 - marker = is_fastest ? '★' : ' ' - if s == baseline - printf " %-20s avg: %7.3fs min: %7.3fs %s (baseline)\n", - s[:ruby], s[:avg], s[:min], marker - else - slower_faster = diff_pct > 0 ? 'slower' : 'faster' - printf " %-20s avg: %7.3fs min: %7.3fs %s %+7.1f%% (%s)\n", - s[:ruby], s[:avg], s[:min], marker, diff_pct, slower_faster - end + # Iterations are supposed to do the same work, but GC heuristics move work + # between them: garbage left by earlier iterations is marked and (lazily) + # swept during later ones. Subtracting each iteration's GC time compares + # the time spent outside the GC, which doesn't depend on where the GC + # happened to run. + def print_benchmark_timings_without_gc + any_printed = false + + benchmarks.each do |bench_name| + stats = @ruby_names.map do |ruby| + times = @raw_data.dig(ruby, bench_name, 'bench') || [] + next nil if times.empty? + gc_times = gc_seconds_per_iteration(ruby, bench_name, times.size) + next nil unless gc_times + without_gc = times.zip(gc_times).map { |time, gc| time - gc } + gc_avg = gc_times.sum / gc_times.size + { ruby: ruby, avg: without_gc.sum / without_gc.size, min: without_gc.min, + gc_avg: gc_avg, gc_pct: gc_avg / (times.sum / times.size) * 100 } + end.compact + + # Without the baseline's GC times there is nothing to compare against + next unless stats.size > 1 && stats.first[:ruby] == @ruby_names.first + + unless any_printed + puts "BENCHMARK TIMINGS WITHOUT GC TIME (lower is better)" + puts "-" * 80 + any_printed = true end + + puts " #{bench_name}:" + print_timing_rows(stats) end + + return unless any_printed + puts " gc: average GC (marking + sweeping) time per iteration, and its share of the iteration." + puts " GC.stat reports whole milliseconds, so min is approximate; avg is not affected." puts end + # Seconds of GC time (marking + sweeping) in each benchmark iteration, + # or nil if the results were not recorded with harness-gc. + def gc_seconds_per_iteration(ruby, bench_name, size) + bench_data = @raw_data.dig(ruby, bench_name) || {} + series = GC_TIME_KEYS.map { |key| bench_data[key] } + return nil unless series.all? { |ms| ms.is_a?(Array) && ms.size == size } + series.transpose.map { |ms| ms.sum / 1000.0 } + end + + def print_timing_rows(stats) + baseline = stats.first + fastest = stats.min_by { |s| s[:avg] } + + stats.each do |s| + diff_pct = ((s[:avg] - baseline[:avg]) / baseline[:avg] * 100) + is_fastest = s == fastest && stats.size > 1 + marker = is_fastest ? '★' : ' ' + gc = s[:gc_avg] ? format(" gc: %7.3fs (%3.0f%%)", s[:gc_avg], s[:gc_pct]) : '' + if s == baseline + printf " %-20s avg: %7.3fs min: %7.3fs%s %s (baseline)\n", + s[:ruby], s[:avg], s[:min], gc, marker + else + slower_faster = diff_pct > 0 ? 'slower' : 'faster' + printf " %-20s avg: %7.3fs min: %7.3fs%s %s %+7.1f%% (%s)\n", + s[:ruby], s[:avg], s[:min], gc, marker, diff_pct, slower_faster + end + end + end + def print_memory_usage puts "MEMORY USAGE" puts "-" * 80 From 211380e5f3f788ac8d4966124dade9e8647b2dfb Mon Sep 17 00:00:00 2001 From: XrXr Date: Fri, 23 Oct 2026 21:57:32 -0400 Subject: [PATCH 2/6] [WIP] GC harness per iteration ZJIT side_exit_count --- harness/gc.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/harness/gc.rb b/harness/gc.rb index d3138aaa..7d68d5d2 100644 --- a/harness/gc.rb +++ b/harness/gc.rb @@ -16,6 +16,7 @@ def realtime def run_benchmark(_num_itrs_hint, **, &block) times = [] + side_exit_counts = [] rss_samples = [] marking_times = [] sweeping_times = [] @@ -80,6 +81,9 @@ def run_benchmark(_num_itrs_hint, **, &block) gc_heap_deltas << sample["gc_stat_heap_delta"] gc_total_time_ns << sample["gc_total_time_ns"] total_time += time + + last_side_exit_count = side_exit_counts.last || 0 + side_exit_counts << RubyVM::ZJIT.stats(:side_exit_count) - last_side_exit_count end until num_itrs >= WARMUP_ITRS + MIN_BENCH_ITRS and total_time >= MIN_BENCH_TIME warmup_range = 0...WARMUP_ITRS @@ -108,6 +112,8 @@ def run_benchmark(_num_itrs_hint, **, &block) extra["gc_total_time_warmup"] = gc_total_time_ms[warmup_range] extra["gc_total_time_bench"] = gc_total_time_ms[bench_range] end + extra["side_exit_count_warmup"] = side_exit_counts[warmup_range] + extra["side_exit_count_bench"] = side_exit_counts[bench_range] # Snapshot heap utilisation after benchmark if GC.respond_to?(:stat_heap) From c1a27f70b98d49d0c664b406c079a2358670badc Mon Sep 17 00:00:00 2001 From: XrXr Date: Fri, 23 Oct 2026 22:48:12 -0400 Subject: [PATCH 3/6] [WIP] show average per iter side exits in without gc time --- misc/zjit_diff.rb | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/misc/zjit_diff.rb b/misc/zjit_diff.rb index 9151f65f..27eb2188 100755 --- a/misc/zjit_diff.rb +++ b/misc/zjit_diff.rb @@ -239,10 +239,13 @@ def print_benchmark_timings_without_gc next nil if times.empty? gc_times = gc_seconds_per_iteration(ruby, bench_name, times.size) next nil unless gc_times + exits = side_exits_per_iteration(ruby, bench_name, times.size) + next nil unless exits without_gc = times.zip(gc_times).map { |time, gc| time - gc } gc_avg = gc_times.sum / gc_times.size { ruby: ruby, avg: without_gc.sum / without_gc.size, min: without_gc.min, - gc_avg: gc_avg, gc_pct: gc_avg / (times.sum / times.size) * 100 } + gc_avg: gc_avg, gc_pct: gc_avg / (times.sum / times.size) * 100, + side_exit_avg: exits.sum / exits.size } end.compact # Without the baseline's GC times there is nothing to compare against @@ -273,6 +276,13 @@ def gc_seconds_per_iteration(ruby, bench_name, size) series.transpose.map { |ms| ms.sum / 1000.0 } end + def side_exits_per_iteration(ruby, bench_name, size) + bench_data = @raw_data.dig(ruby, bench_name) || {} + series = bench_data["side_exit_count_bench"] + return nil unless series.size == size + series + end + def print_timing_rows(stats) baseline = stats.first fastest = stats.min_by { |s| s[:avg] } @@ -282,13 +292,16 @@ def print_timing_rows(stats) is_fastest = s == fastest && stats.size > 1 marker = is_fastest ? '★' : ' ' gc = s[:gc_avg] ? format(" gc: %7.3fs (%3.0f%%)", s[:gc_avg], s[:gc_pct]) : '' + if (side_exit_avg = s[:side_exit_avg]) + side_exit_avg = sprintf("side_exit_avg: %d", side_exit_avg) + end if s == baseline - printf " %-20s avg: %7.3fs min: %7.3fs%s %s (baseline)\n", - s[:ruby], s[:avg], s[:min], gc, marker + printf " %-20s avg: %7.3fs min: %7.3fs%s %s %s (baseline)\n", + s[:ruby], s[:avg], s[:min], gc, side_exit_avg, marker else slower_faster = diff_pct > 0 ? 'slower' : 'faster' - printf " %-20s avg: %7.3fs min: %7.3fs%s %s %+7.1f%% (%s)\n", - s[:ruby], s[:avg], s[:min], gc, marker, diff_pct, slower_faster + printf " %-20s avg: %7.3fs min: %7.3fs%s %s %s %+7.1f%% (%s)\n", + s[:ruby], s[:avg], s[:min], gc, side_exit_avg, marker, diff_pct, slower_faster end end end From a6600de721adead5081c766f933cd443b1882bc0 Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Tue, 3 Nov 2026 15:35:46 +0000 Subject: [PATCH 4/6] [WIP] gc harness: treat missing side_exit_count as 0 Rubies that don't count side exits by default return nil from RubyVM::ZJIT.stats(:side_exit_count) unless run with --zjit-stats. --- harness/gc.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/harness/gc.rb b/harness/gc.rb index 7d68d5d2..3189e477 100644 --- a/harness/gc.rb +++ b/harness/gc.rb @@ -83,7 +83,7 @@ def run_benchmark(_num_itrs_hint, **, &block) total_time += time last_side_exit_count = side_exit_counts.last || 0 - side_exit_counts << RubyVM::ZJIT.stats(:side_exit_count) - last_side_exit_count + side_exit_counts << (RubyVM::ZJIT.stats(:side_exit_count) || 0) - last_side_exit_count end until num_itrs >= WARMUP_ITRS + MIN_BENCH_ITRS and total_time >= MIN_BENCH_TIME warmup_range = 0...WARMUP_ITRS From 9bbb8fffd3198ae8c3111602145e0119a28ddd1d Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Tue, 3 Nov 2026 16:30:23 +0000 Subject: [PATCH 5/6] [WIP] gc harness: fix per-iteration side_exit_count delta Subtract the previous running total, not the previous iteration's delta. Take the starting total before the first iteration so boot-time side exits aren't counted against iteration 1. --- harness/gc.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/harness/gc.rb b/harness/gc.rb index 3189e477..0562fead 100644 --- a/harness/gc.rb +++ b/harness/gc.rb @@ -17,6 +17,7 @@ def realtime def run_benchmark(_num_itrs_hint, **, &block) times = [] side_exit_counts = [] + side_exit_total = RubyVM::ZJIT.stats(:side_exit_count) || 0 rss_samples = [] marking_times = [] sweeping_times = [] @@ -82,8 +83,9 @@ def run_benchmark(_num_itrs_hint, **, &block) gc_total_time_ns << sample["gc_total_time_ns"] total_time += time - last_side_exit_count = side_exit_counts.last || 0 - side_exit_counts << (RubyVM::ZJIT.stats(:side_exit_count) || 0) - last_side_exit_count + side_exit_now = RubyVM::ZJIT.stats(:side_exit_count) || 0 + side_exit_counts << side_exit_now - side_exit_total + side_exit_total = side_exit_now end until num_itrs >= WARMUP_ITRS + MIN_BENCH_ITRS and total_time >= MIN_BENCH_TIME warmup_range = 0...WARMUP_ITRS From da3e640e28c3973e4f46340b7f6673f702fe0ddc Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Tue, 3 Nov 2026 16:40:54 +0000 Subject: [PATCH 6/6] [WIP] zjit_diff: don't crash on results without side exit counts side_exits_per_iteration called .size on nil for JSON recorded with a harness-gc that doesn't track side exits. Return nil instead, and still print the without-GC timings, just without side_exit_avg. --- misc/zjit_diff.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/misc/zjit_diff.rb b/misc/zjit_diff.rb index 27eb2188..496f60e7 100755 --- a/misc/zjit_diff.rb +++ b/misc/zjit_diff.rb @@ -240,12 +240,11 @@ def print_benchmark_timings_without_gc gc_times = gc_seconds_per_iteration(ruby, bench_name, times.size) next nil unless gc_times exits = side_exits_per_iteration(ruby, bench_name, times.size) - next nil unless exits without_gc = times.zip(gc_times).map { |time, gc| time - gc } gc_avg = gc_times.sum / gc_times.size { ruby: ruby, avg: without_gc.sum / without_gc.size, min: without_gc.min, gc_avg: gc_avg, gc_pct: gc_avg / (times.sum / times.size) * 100, - side_exit_avg: exits.sum / exits.size } + side_exit_avg: exits && exits.sum / exits.size } end.compact # Without the baseline's GC times there is nothing to compare against @@ -276,10 +275,12 @@ def gc_seconds_per_iteration(ruby, bench_name, size) series.transpose.map { |ms| ms.sum / 1000.0 } end + # Side exits in each benchmark iteration, or nil if the results don't have them + # (e.g. recorded with a harness-gc that predates side exit tracking). def side_exits_per_iteration(ruby, bench_name, size) bench_data = @raw_data.dig(ruby, bench_name) || {} series = bench_data["side_exit_count_bench"] - return nil unless series.size == size + return nil unless series.is_a?(Array) && series.size == size series end