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  * the same lambda capture different variables
  44  * @author Sergey Kuksenko (sergey.kuksenko@oracle.com)
  45  */
  46 @BenchmarkMode(Mode.AverageTime)
  47 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  48 @State(Scope.Thread)
  49 public class Morph2 {
  50 
  51 
  52     private static final int LIMIT = 16536;
  53     private static final int OPS = 4;
  54     private static final int OPERATIONS = OPS*LIMIT;
  55 
  56     // <source of functional interface>_N; where N - how many different targets
  57     private IntUnaryOperator[] inner_1;
  58     private IntUnaryOperator[] inner_2;
  59     private IntUnaryOperator[] inner_4;
  60 
  61     private IntUnaryOperator[] lambda_1;
  62     private IntUnaryOperator[] lambda_2;
  63     private IntUnaryOperator[] lambda_4;
  64 
  65     @Setup(Level.Trial)
  66     public void setup() {
  67         setup_inner(1,2,3,4);
  68         setup_lambda(1,2,3,4);
  69     }
  70 
  71     private void setup_inner(int a, int b, int c, int d) {
  72         inner_4 =  new IntUnaryOperator[] {
  73                 makeInner(a),
  74                 makeInner(b),
  75                 makeInner(c),
  76                 makeInner(d),
  77         };
  78         inner_2 =  new IntUnaryOperator[] { inner_4[0], inner_4[1], inner_4[0], inner_4[1], };
  79         inner_1 =  new IntUnaryOperator[] { inner_4[0], inner_4[0], inner_4[0], inner_4[0], };
  80     }
  81 
  82     private IntUnaryOperator makeInner(final int a) {
  83         return new IntUnaryOperator() {
  84             @Override
  85             public int applyAsInt(int x) {
  86                 return x + a;
  87             }
  88         };
  89     }
  90 
  91     private void setup_lambda(int a, int b, int c, int d) {
  92         lambda_4 =  new IntUnaryOperator[] {
  93                 makeLambda(a),
  94                 makeLambda(b),
  95                 makeLambda(c),
  96                 makeLambda(d),
  97         };
  98         lambda_2 =  new IntUnaryOperator[] { lambda_4[0], lambda_4[1], lambda_4[0], lambda_4[1], };
  99         lambda_1 =  new IntUnaryOperator[] { lambda_4[0], lambda_4[0], lambda_4[0], lambda_4[0], };
 100     }
 101 
 102     private IntUnaryOperator makeLambda(int a) {
 103         return x -> x + a;
 104     }
 105 
 106     public void process(Blackhole bh, IntUnaryOperator[] operations) {
 107         for (int i = 0; i < LIMIT; i++) {
 108             for (IntUnaryOperator op : operations) {
 109                 bh.consume(op.applyAsInt(i));
 110             }
 111         }
 112     }
 113 
 114     @Benchmark
 115     @OperationsPerInvocation(OPERATIONS)
 116     public void inner1(Blackhole bh) {
 117         process(bh, inner_1);
 118     }
 119 
 120     @Benchmark
 121     @OperationsPerInvocation(OPERATIONS)
 122     public void inner2(Blackhole bh) {
 123         process(bh, inner_2);
 124     }
 125 
 126     @Benchmark
 127     @OperationsPerInvocation(OPERATIONS)
 128     public void inner4(Blackhole bh) {
 129         process(bh, inner_4);
 130     }
 131 
 132     @Benchmark
 133     @OperationsPerInvocation(OPERATIONS)
 134     public void lambda1(Blackhole bh) {
 135         process(bh, lambda_1);
 136     }
 137 
 138     @Benchmark
 139     @OperationsPerInvocation(OPERATIONS)
 140     public void lambda2(Blackhole bh) {
 141         process(bh, lambda_2);
 142     }
 143 
 144     @Benchmark
 145     @OperationsPerInvocation(OPERATIONS)
 146     public void lambda4(Blackhole bh) {
 147         process(bh, lambda_4);
 148     }
 149 
 150 }
 151