From 851b8f852313c361465cf760701e23db0ea4d474 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Thu, 26 Mar 2026 14:44:56 -0700 Subject: 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] --- object.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'object.c') diff --git a/object.c b/object.c index c3241a198d..cb21741f31 100644 --- a/object.c +++ b/object.c @@ -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; } -- cgit v1.2.3