Skip to content

KindOf implementation using Class.isInstance() instead of instanceof #614

@ghost

Description

This would probably be an good entry-point for someone looking to get involved in hacking JRuby.

There are a heap of single-method KindOf classes that look like:

fixnum.kindOf = new RubyModule.KindOf() {
    @Override
    public boolean isKindOf(IRubyObject obj, RubyModule type) {
        return obj instanceof RubyFixnum;
    }
}

Most of these could be replaced with a single implementation that uses Class.isInstance()
e.g.

public static final class JavaClassKindOf extends RubyModule.KindOf {
    private final Class klass;
    public JavaClassKindOf(Class klass) { 
        this.klass = klass; 
    }

    @Override
    public boolean isKindOf(IRubyObject obj, RubyModule type) {
        return klass.isInstance(obj);
    }
}

This wrinkle would be to benchmark this to make sure hotspot is optimising it the same way - intrinsic isInstance() might only be in very recent hotspot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions