From f5da6395bd3fdf4b76a480f174cd304823ddc57f Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Sat, 30 Aug 2025 13:04:11 +0200 Subject: Kernel#class skip null check `Kernel#class` can't possibly be called on an hidden object, hence we don't need to check for `klass == 0`. ``` compare-ruby: ruby 3.5.0dev (2025-08-30T01:45:42Z obj-class 01a57bd6cd) +YJIT +PRISM [arm64-darwin24] built-ruby: ruby 3.5.0dev (2025-08-30T10:21:10Z obj-class b67c16c477) +YJIT +PRISM [arm64-darwin24] | |compare-ruby|built-ruby| |:----------|-----------:|---------:| |obj | 445.217| 642.446| | | -| 1.44x| |extended | 136.826| 117.974| | | 1.16x| -| |singleton | 166.269| 166.695| | | -| 1.00x| |immediate | 380.243| 515.775| | | -| 1.36x| ``` --- kernel.rb | 2 +- object.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel.rb b/kernel.rb index 888ef0c531..5d596c8892 100644 --- a/kernel.rb +++ b/kernel.rb @@ -17,7 +17,7 @@ module Kernel # def class Primitive.attr! :leaf - Primitive.cexpr! 'rb_obj_class(self)' + Primitive.cexpr! 'rb_obj_class_must(self)' end # diff --git a/object.c b/object.c index dc5ab2d934..1ac14ec634 100644 --- a/object.c +++ b/object.c @@ -281,6 +281,7 @@ rb_obj_not_equal(VALUE obj1, VALUE obj2) static inline VALUE fake_class_p(VALUE klass) { + RUBY_ASSERT(klass); RUBY_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS)); STATIC_ASSERT(t_iclass_overlap_t_class, !(T_CLASS & T_ICLASS)); STATIC_ASSERT(t_iclass_overlap_t_module, !(T_MODULE & T_ICLASS)); @@ -307,6 +308,17 @@ rb_obj_class(VALUE obj) return cl; } +VALUE +rb_obj_class_must(VALUE obj) +{ + VALUE cl = CLASS_OF(obj); + RUBY_ASSERT(cl); + while (RB_UNLIKELY(fake_class_p(cl))) { + cl = RCLASS_SUPER(cl); + } + return cl; +} + /* * call-seq: * obj.singleton_class -> class -- cgit v1.2.3