summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <jean.boussier@gmail.com>2025-08-30 13:04:11 +0200
committerJean Boussier <jean.boussier@gmail.com>2025-08-30 14:14:10 +0200
commitf5da6395bd3fdf4b76a480f174cd304823ddc57f (patch)
tree1e4bc98c7fbd4ee29e3f98e2009e23865f3a8432
parent01b89528cb663e1e533375d4282d906e49ca1496 (diff)
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| ```
-rw-r--r--kernel.rb2
-rw-r--r--object.c12
2 files changed, 13 insertions, 1 deletions
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