[9] RFR [XS] 8054492: Casting can result in redundant null checks in generated code

Paul Sandoz paul.sandoz at oracle.com
Tue Oct 14 16:58:21 UTC 2014


Here is a simpler test case that just uses a MH to Class.cast that exhibits the same behaviour.

The commented out use of the BiFunction created from a lambda expressions works fine. 

This suggests there might be something about the MH invocation that is stopping the optimization from kicking in.

Paul.

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.function.BiFunction;

public class NullCheckDroppingsMHCastTest {

    static final MethodHandle MH_CAST;

    static {
        try {
            MH_CAST = MethodHandles.lookup().findVirtual(Class.class,
                                                         "cast",
                                                         MethodType.methodType(Object.class, Object.class));
        }
        catch (Exception e) {
            throw new Error(e);
        }
    }

    static final BiFunction<Class, Object, Object> fCast = (c, o) -> c.cast(o);


    volatile String svalue = "A";
    volatile String snull = null;
    String ssink;

    public static void main(String[] args) {
        NullCheckDroppingsMHCastTest t = new NullCheckDroppingsMHCastTest();
        t.testLoop();
    }

    void testLoop() {
        // Ensure testLoopOne is compiled and does not
        // see a null value
        for (int i = 0; i < 1000000; i++) {
            testLoopOne(svalue);
            // Uncomment the following and the call outside
            // the loop should not result in any deoptimization
            // testLoopOne(snull);
        }

        // Sleep just to create delay in the compilation log
        try {
            Thread.currentThread().sleep(1000);
        }
        catch (Exception e) {
        }

        // This should cause a de-optimisation
        // if testLoopOne is compiled with a null-check
        testLoopOne(snull);
    }

    void testLoopOne(String s) {
        try {
            ssink = (String) (Object) MH_CAST.invokeExact(String.class, (Object) s);
//            ssink = (String) fCast.apply(String.class, s);
        }
        catch (Throwable t) {
            throw new Error(t);
        }
    }
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 841 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.openjdk.java.net/pipermail/hotspot-compiler-dev/attachments/20141014/77d71fc3/signature-0001.asc>


More information about the hotspot-compiler-dev mailing list