summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2026-02-10 10:46:40 -0800
committerGitHub <noreply@github.com>2026-02-10 10:46:40 -0800
commitf3bfffe8565b08b3426003aebbe328a0ecedad26 (patch)
tree0a9e5ceb5069a9ef4f92a0a4b899123b03f6228a
parent0768f08caff514b0ba616dc26d76aad90577eefe (diff)
ZJIT: Avoid runtime exceptions from RubyVM::ZJIT.stats_string (#16139)
Before this it would raise if zjit wasn't enabled and raise a different exception if zjit was but extended stats were not (_some_ stats are available). Co-authored-by: Randy Stauner <randy@r4s6.net>
-rw-r--r--test/ruby/test_zjit.rb12
-rw-r--r--zjit.rb6
2 files changed, 15 insertions, 3 deletions
diff --git a/test/ruby/test_zjit.rb b/test/ruby/test_zjit.rb
index 805ecb98b2..5f5f8922f0 100644
--- a/test/ruby/test_zjit.rb
+++ b/test/ruby/test_zjit.rb
@@ -27,6 +27,18 @@ class TestZJIT < Test::Unit::TestCase
RUBY
end
+ def test_stats_string_no_zjit
+ assert_runs 'nil', <<~RUBY, zjit: false
+ RubyVM::ZJIT.stats_string
+ RUBY
+ assert_runs 'true', <<~RUBY, stats: false
+ RubyVM::ZJIT.stats_string.is_a?(String)
+ RUBY
+ assert_runs 'true', <<~RUBY, stats: true
+ RubyVM::ZJIT.stats_string.is_a?(String)
+ RUBY
+ end
+
def test_stats_quiet
# Test that --zjit-stats-quiet collects stats but doesn't print them
script = <<~RUBY
diff --git a/zjit.rb b/zjit.rb
index e2aa55f764..10c2c9d6c7 100644
--- a/zjit.rb
+++ b/zjit.rb
@@ -165,13 +165,13 @@ class << RubyVM::ZJIT
# Get the summary of ZJIT statistics as a String
def stats_string
+ return unless stats = self.stats
buf = +"***ZJIT: Printing ZJIT statistics on exit***\n"
- stats = self.stats
- if stats[:guard_type_count].nonzero?
+ if stats[:guard_type_count]&.nonzero?
stats[:guard_type_exit_ratio] = stats[:exit_guard_type_failure].to_f / stats[:guard_type_count] * 100
end
- if stats[:guard_shape_count].nonzero?
+ if stats[:guard_shape_count]&.nonzero?
stats[:guard_shape_exit_ratio] = stats[:exit_guard_shape_failure].to_f / stats[:guard_shape_count] * 100
end