diff options
| author | John Hawthorn <john@hawthorn.email> | 2026-03-26 14:44:56 -0700 |
|---|---|---|
| committer | John Hawthorn <john@hawthorn.email> | 2026-03-27 09:00:25 -0700 |
| commit | 851b8f852313c361465cf760701e23db0ea4d474 (patch) | |
| tree | 93d2291ab29a77803fb11cc12675c36a78f8fb4b /object.c | |
| parent | 5eff7e0ec2ab405be1db7a57819d50ddf75008e2 (diff) | |
Remove class alloc check
This checks that the value returned from the function registered with
rb_define_alloc_func is of the correct class. When this was first
introduced in 1fe40b7cc5 (by Matz on 2001-10-03), allocation was done
via user-defined Object#allocate, so it made sense to have a runtime
check in release builds. Now that it's defined via rb_define_alloc_func
in the C extension API, I don't think it's necessary.
The check is surprisingly expensive. Removing it makes Object.new about
10% faster. It allows the C compiler to optimize the call to the
function pointer as a tail-call. Removing this also allows ZJIT/YJIT to
call the function directly (ZJIT already does this by having a list of
known safe allocation functions).
There's no way for users to ever have seen this check, other than by
writing a misbehaving C extension, which returns objects with the wrong
class.
[Feature #21966]
Diffstat (limited to 'object.c')
| -rw-r--r-- | object.c | 6 |
1 files changed, 1 insertions, 5 deletions
@@ -2337,11 +2337,7 @@ class_call_alloc_func(rb_alloc_func_t allocator, VALUE klass) obj = (*allocator)(klass); - if (UNLIKELY(RBASIC_CLASS(obj) != klass)) { - if (rb_obj_class(obj) != rb_class_real(klass)) { - rb_raise(rb_eTypeError, "wrong instance allocation"); - } - } + RUBY_ASSERT(rb_obj_class(obj) == rb_class_real(klass)); return obj; } |
