Skip to content

Commit 9715348

Browse files
committed
ZJIT: Optimize class guards by directly reading klass field
Replace `rb_yarv_class_of` call with direct memory read at offset 8 for regular heap objects and a constant check for special constants (nil, fixnums, symbols, etc).
1 parent 71b4693 commit 9715348

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

zjit/src/codegen.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,8 +1073,12 @@ fn gen_guard_type(jit: &mut JITState, asm: &mut Assembler, val: lir::Opnd, guard
10731073
} else if let Some(expected_class) = guard_type.runtime_exact_ruby_class() {
10741074
asm_comment!(asm, "guard exact class");
10751075

1076-
// Get the class of the value
1077-
let klass = asm.ccall(rb_yarv_class_of as *const u8, vec![val]);
1076+
// Check if it's a special constant first
1077+
asm.test(val, (RUBY_IMMEDIATE_MASK as u64).into());
1078+
asm.jnz(side_exit(jit, state, GuardType(guard_type))?);
1079+
1080+
// Load the class from the object's klass field
1081+
let klass = asm.load(Opnd::mem(64, val, RUBY_OFFSET_RBASIC_KLASS));
10781082

10791083
asm.cmp(klass, Opnd::Value(expected_class));
10801084
asm.jne(side_exit(jit, state, GuardType(guard_type))?);

0 commit comments

Comments
 (0)