(original) (raw)
/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package compiler.valhalla.valuetypes; import java.util.concurrent.TimeUnit; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.RunnerException; import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; interface MyInterface { } class MyValue implements MyInterface { int x; } class MyObject implements MyInterface { int x; } @State(Scope.Thread) @OutputTimeUnit(TimeUnit.MICROSECONDS) public class NewAcmpBenchmark { Object u = new Object(); Object u2 = new Object(); MyObject o = new MyObject(); MyValue v = new MyValue(); @State(Scope.Thread) public static class ThreadState { boolean equal = false; } @CompilerControl(CompilerControl.Mode.DONT_INLINE) private boolean cmp1(Object a, Object b) { return a == b; // new acmp } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public void newCmp(ThreadState state) { cmp1(state.equal ? u : v, state.equal ? u : o); state.equal = !state.equal; } @CompilerControl(CompilerControl.Mode.DONT_INLINE) private boolean cmp2(MyValue a, Object b) { return a == b; // double null check } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public void newCmpDoubleNull(ThreadState state) { cmp2(state.equal ? null : v, state.equal ? null : o); state.equal = !state.equal; } @CompilerControl(CompilerControl.Mode.DONT_INLINE) private boolean cmp3(MyValue a, Object b) { if (a == null) { return false; } return a == b; // false } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public void newCmpDoubleNullFalse(ThreadState state) { cmp3(state.equal ? null : v, state.equal ? null : o); state.equal = !state.equal; } @CompilerControl(CompilerControl.Mode.DONT_INLINE) private boolean cmp4(Object a, MyObject b) { return a == b; // old acmp } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public void oldCmp(ThreadState state) { cmp4(state.equal ? o : v, state.equal ? o : o); state.equal = !state.equal; } @Benchmark @CompilerControl(CompilerControl.Mode.DONT_INLINE) public boolean newCmpField(ThreadState state) { return u == u2; } public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(NewAcmpBenchmark.class.getSimpleName()) .forks(1) .build(); new Runner(opt).run(); } }