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|
```
This commit is contained in:
Jean Boussier 2025-08-30 13:04:11 +02:00
parent 01b89528cb
commit f5da6395bd
2 changed files with 13 additions and 1 deletions

View File

@ -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
#

View File

@ -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