From 3d7525a30046faa1b3740194d47d3ef3537bb449 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 30 Apr 2025 16:16:43 -0700 Subject: [PATCH 1/2] YJIT: call alloc func directly in new This removes an indirect branch by calling the allocator function from the class directly. --- yjit/src/codegen.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/yjit/src/codegen.rs b/yjit/src/codegen.rs index 0d74f2a3fbc224..40bb83f88af918 100644 --- a/yjit/src/codegen.rs +++ b/yjit/src/codegen.rs @@ -4912,7 +4912,10 @@ fn gen_opt_new( // Fast path // call rb_class_alloc to actually allocate jit_prepare_non_leaf_call(jit, asm); - let obj = asm.ccall(rb_obj_alloc as _, vec![comptime_recv.into()]); + + let allocator = unsafe { rb_get_alloc_func(comptime_recv) }; + let allocator = allocator.unwrap_or(rb_obj_alloc); + let obj = asm.ccall(allocator as _, vec![comptime_recv.into()]); // Get a reference to the stack location where we need to save the // return instance. From 7548512f57b7b4e5821c4ce0f7f45b10dba0f9d0 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 30 Apr 2025 16:51:13 -0700 Subject: [PATCH 2/2] Don't allow allocating singleton class --- yjit/src/codegen.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/yjit/src/codegen.rs b/yjit/src/codegen.rs index 40bb83f88af918..cd0593c29af879 100644 --- a/yjit/src/codegen.rs +++ b/yjit/src/codegen.rs @@ -4913,7 +4913,13 @@ fn gen_opt_new( // call rb_class_alloc to actually allocate jit_prepare_non_leaf_call(jit, asm); - let allocator = unsafe { rb_get_alloc_func(comptime_recv) }; + let allocator = + if unsafe { FL_TEST(comptime_recv, VALUE(RUBY_FL_SINGLETON as usize)) } != VALUE(0) { + None + } else { + unsafe { rb_get_alloc_func(comptime_recv) } + }; + let allocator = allocator.unwrap_or(rb_obj_alloc); let obj = asm.ccall(allocator as _, vec![comptime_recv.into()]);