ZJIT: Optimize class guards by directly reading klass field (#14136)

Replace `rb_yarv_class_of` call with:
- a constant check for special constants (nil, fixnums, symbols, etc)
- a check for false
- direct memory read at offset 8 for regular heap objects for the class check
This commit is contained in:
Stan Lo 2025-08-07 23:38:02 +01:00 committed by GitHub
parent 96c9e1e93a
commit d25eb1eb5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 7 deletions

View file

@ -1432,6 +1432,30 @@ class TestZJIT < Test::Unit::TestCase
}, call_threshold: 2, insns: [:opt_nil_p]
end
def test_basic_object_guard_works_with_immediate
assert_compiles 'NilClass', %q{
class Foo; end
def test(val) = val.class
test(Foo.new)
test(Foo.new)
test(nil)
}, call_threshold: 2
end
def test_basic_object_guard_works_with_false
assert_compiles 'FalseClass', %q{
class Foo; end
def test(val) = val.class
test(Foo.new)
test(Foo.new)
test(false)
}, call_threshold: 2
end
private
# Assert that every method call in `test_script` can be compiled by ZJIT