diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2025-08-30 13:04:11 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2025-08-30 14:14:10 +0200 |
commit | f5da6395bd3fdf4b76a480f174cd304823ddc57f (patch) | |
tree | 1e4bc98c7fbd4ee29e3f98e2009e23865f3a8432 | |
parent | 01b89528cb663e1e533375d4282d906e49ca1496 (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.rb | 2 | ||||
-rw-r--r-- | object.c | 12 |
2 files changed, 13 insertions, 1 deletions
@@ -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 # @@ -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 |