1 /*
   2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.bench.vm.lambda.invoke;
  26 
  27 import org.openjdk.jmh.annotations.Benchmark;
  28 import org.openjdk.jmh.annotations.BenchmarkMode;
  29 import org.openjdk.jmh.annotations.Level;
  30 import org.openjdk.jmh.annotations.Mode;
  31 import org.openjdk.jmh.annotations.OperationsPerInvocation;
  32 import org.openjdk.jmh.annotations.OutputTimeUnit;
  33 import org.openjdk.jmh.annotations.Scope;
  34 import org.openjdk.jmh.annotations.Setup;
  35 import org.openjdk.jmh.annotations.State;
  36 import org.openjdk.jmh.infra.Blackhole;
  37 
  38 import java.util.concurrent.TimeUnit;
  39 import java.util.function.IntUnaryOperator;
  40 
  41 /**
  42  * evaluates N-morphic invocation costs.
  43  * N different lambdas each capture 0 variable
  44  *
  45  * @author Sergey Kuksenko (sergey.kuksenko@oracle.com)
  46  */
  47 @BenchmarkMode(Mode.AverageTime)
  48 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  49 @State(Scope.Thread)
  50 public class Morph0 {
  51 
  52 
  53     private static final int LIMIT = 16536;
  54     private static final int OPS = 4;
  55     private static final int OPERATIONS = OPS*LIMIT;
  56 
  57     // <source of functional interface>_N; where N - how many different targets
  58     private IntUnaryOperator[] inner_1;
  59     private IntUnaryOperator[] inner_2;
  60     private IntUnaryOperator[] inner_4;
  61 
  62     private IntUnaryOperator[] lambda_1;
  63     private IntUnaryOperator[] lambda_2;
  64     private IntUnaryOperator[] lambda_4;
  65 
  66     private IntUnaryOperator[] unbounded_mref_1;
  67     private IntUnaryOperator[] unbounded_mref_2;
  68     private IntUnaryOperator[] unbounded_mref_4;
  69 
  70     private IntUnaryOperator[] bounded_mref_1;
  71     private IntUnaryOperator[] bounded_mref_2;
  72     private IntUnaryOperator[] bounded_mref_4;
  73 
  74     @Setup(Level.Trial)
  75     public void setup() {
  76         setup_inner();
  77         setup_lambda();
  78         setup_unbounded_mref();
  79         setup_bounded_mref();
  80     }
  81 
  82     private void setup_inner() {
  83         inner_4 =  new IntUnaryOperator[] {
  84             new IntUnaryOperator() {
  85                 @Override
  86                 public int applyAsInt(int x) {
  87                     return x + 1;
  88                 }
  89             },
  90             new IntUnaryOperator() {
  91                 @Override
  92                 public int applyAsInt(int x) {
  93                     return x + 2;
  94                 }
  95             },
  96             new IntUnaryOperator() {
  97                 @Override
  98                 public int applyAsInt(int x) {
  99                     return x + 3;
 100                 }
 101             },
 102             new IntUnaryOperator() {
 103                 @Override
 104                 public int applyAsInt(int x) {
 105                     return x + 4;
 106                 }
 107             },
 108         };
 109         inner_2 =  new IntUnaryOperator[] { inner_4[0], inner_4[1], inner_4[0], inner_4[1], };
 110         inner_1 =  new IntUnaryOperator[] { inner_4[0], inner_4[0], inner_4[0], inner_4[0], };
 111     }
 112 
 113     private void setup_lambda() {
 114         lambda_4 =  new IntUnaryOperator[] {
 115                 x -> x + 1,
 116                 x -> x + 2,
 117                 x -> x + 3,
 118                 x -> x + 4,
 119         };
 120         lambda_2 =  new IntUnaryOperator[] { lambda_4[0], lambda_4[1], lambda_4[0], lambda_4[1], };
 121         lambda_1 =  new IntUnaryOperator[] { lambda_4[0], lambda_4[0], lambda_4[0], lambda_4[0], };
 122     }
 123 
 124     public static int func1(int x) {
 125         return x + 1;
 126     }
 127 
 128     public static int func2(int x) {
 129         return x + 2;
 130     }
 131 
 132     public static int func3(int x) {
 133         return x + 3;
 134     }
 135 
 136     public static int func4(int x) {
 137         return x + 4;
 138     }
 139 
 140     private void setup_unbounded_mref() {
 141         unbounded_mref_4 =  new IntUnaryOperator[] {
 142                 Morph0::func1,
 143                 Morph0::func2,
 144                 Morph0::func3,
 145                 Morph0::func4,
 146         };
 147         unbounded_mref_2 =  new IntUnaryOperator[] { unbounded_mref_4[0], unbounded_mref_4[1], unbounded_mref_4[0], unbounded_mref_4[1], };
 148         unbounded_mref_1 =  new IntUnaryOperator[] { unbounded_mref_4[0], unbounded_mref_4[0], unbounded_mref_4[0], unbounded_mref_4[0], };
 149     }
 150 
 151     public int ifunc1(int x) {
 152         return x + 1;
 153     }
 154 
 155     public int ifunc2(int x) {
 156         return x + 2;
 157     }
 158 
 159     public int ifunc3(int x) {
 160         return x + 3;
 161     }
 162 
 163     public int ifunc4(int x) {
 164         return x + 4;
 165     }
 166 
 167     private void setup_bounded_mref() {
 168         bounded_mref_4 =  new IntUnaryOperator[] {
 169                 this::ifunc1,
 170                 this::ifunc2,
 171                 this::ifunc3,
 172                 this::ifunc4,
 173         };
 174         bounded_mref_2 =  new IntUnaryOperator[] { bounded_mref_4[0], bounded_mref_4[1], bounded_mref_4[0], bounded_mref_4[1], };
 175         bounded_mref_1 =  new IntUnaryOperator[] { bounded_mref_4[0], bounded_mref_4[0], bounded_mref_4[0], bounded_mref_4[0], };
 176     }
 177 
 178     public void process(Blackhole bh, IntUnaryOperator[] operations) {
 179         for (int i = 0; i < LIMIT; i++) {
 180             for (IntUnaryOperator op : operations) {
 181                 bh.consume(op.applyAsInt(i));
 182             }
 183         }
 184     }
 185 
 186     @Benchmark
 187     @OperationsPerInvocation(OPERATIONS)
 188     public void inner1(Blackhole bh) {
 189         process(bh, inner_1);
 190     }
 191 
 192     @Benchmark
 193     @OperationsPerInvocation(OPERATIONS)
 194     public void inner2(Blackhole bh) {
 195         process(bh, inner_2);
 196     }
 197 
 198     @Benchmark
 199     @OperationsPerInvocation(OPERATIONS)
 200     public void inner4(Blackhole bh) {
 201         process(bh, inner_4);
 202     }
 203 
 204     @Benchmark
 205     @OperationsPerInvocation(OPERATIONS)
 206     public void lambda1(Blackhole bh) {
 207         process(bh, lambda_1);
 208     }
 209 
 210     @Benchmark
 211     @OperationsPerInvocation(OPERATIONS)
 212     public void lambda2(Blackhole bh) {
 213         process(bh, lambda_2);
 214     }
 215 
 216     @Benchmark
 217     @OperationsPerInvocation(OPERATIONS)
 218     public void lambda4(Blackhole bh) {
 219         process(bh, lambda_4);
 220     }
 221 
 222     @Benchmark
 223     @OperationsPerInvocation(OPERATIONS)
 224     public void mref1(Blackhole bh) {
 225         process(bh, unbounded_mref_1);
 226     }
 227 
 228     @Benchmark
 229     @OperationsPerInvocation(OPERATIONS)
 230     public void mref2(Blackhole bh) {
 231         process(bh, unbounded_mref_2);
 232     }
 233 
 234     @Benchmark
 235     @OperationsPerInvocation(OPERATIONS)
 236     public void mref4(Blackhole bh) {
 237         process(bh, unbounded_mref_4);
 238     }
 239 
 240     @Benchmark
 241     @OperationsPerInvocation(OPERATIONS)
 242     public void mref_bnd1(Blackhole bh) {
 243         process(bh, bounded_mref_1);
 244     }
 245 
 246     @Benchmark
 247     @OperationsPerInvocation(OPERATIONS)
 248     public void mref_bnd2(Blackhole bh) {
 249         process(bh, bounded_mref_2);
 250     }
 251 
 252     @Benchmark
 253     @OperationsPerInvocation(OPERATIONS)
 254     public void mref_bnd4(Blackhole bh) {
 255         process(bh, bounded_mref_4);
 256     }
 257 
 258 }
 259