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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.bench.vm.lambda.invoke; 24 25 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.BenchmarkMode; 27 import org.openjdk.jmh.annotations.Level; 28 import org.openjdk.jmh.annotations.Mode; 29 import org.openjdk.jmh.annotations.OperationsPerInvocation; 30 import org.openjdk.jmh.annotations.OutputTimeUnit; 31 import org.openjdk.jmh.annotations.Scope; 32 import org.openjdk.jmh.annotations.Setup; 33 import org.openjdk.jmh.annotations.State; 34 import org.openjdk.jmh.infra.Blackhole; 35 36 import java.util.concurrent.TimeUnit; 37 import java.util.function.IntUnaryOperator; 38 39 /** 40 * evaluates N-morphic invocation costs. 41 * the same lambda capture different variables 42 * @author Sergey Kuksenko (sergey.kuksenko@oracle.com) 43 */ 44 @BenchmarkMode(Mode.AverageTime) 45 @OutputTimeUnit(TimeUnit.NANOSECONDS) 46 @State(Scope.Thread) 47 public class Morph2 { 48 49 50 private static final int LIMIT = 16536; 51 private static final int OPS = 4; 52 private static final int OPERATIONS = OPS*LIMIT; 53 54 // <source of functional interface>_N; where N - how many different targets 55 private IntUnaryOperator[] inner_1; 56 private IntUnaryOperator[] inner_2; 57 private IntUnaryOperator[] inner_4; 58 59 private IntUnaryOperator[] lambda_1; 60 private IntUnaryOperator[] lambda_2; 61 private IntUnaryOperator[] lambda_4; 62 63 @Setup(Level.Trial) 64 public void setup() { 65 setup_inner(1,2,3,4); 66 setup_lambda(1,2,3,4); 67 } 68 69 private void setup_inner(int a, int b, int c, int d) { 70 inner_4 = new IntUnaryOperator[] { 71 makeInner(a), 72 makeInner(b), 73 makeInner(c), 74 makeInner(d), 75 }; 76 inner_2 = new IntUnaryOperator[] { inner_4[0], inner_4[1], inner_4[0], inner_4[1], }; 77 inner_1 = new IntUnaryOperator[] { inner_4[0], inner_4[0], inner_4[0], inner_4[0], }; 78 } 79 80 private IntUnaryOperator makeInner(final int a) { 81 return new IntUnaryOperator() { 82 @Override 83 public int applyAsInt(int x) { 84 return x + a; 85 } 86 }; 87 } 88 89 private void setup_lambda(int a, int b, int c, int d) { 90 lambda_4 = new IntUnaryOperator[] { 91 makeLambda(a), 92 makeLambda(b), 93 makeLambda(c), 94 makeLambda(d), 95 }; 96 lambda_2 = new IntUnaryOperator[] { lambda_4[0], lambda_4[1], lambda_4[0], lambda_4[1], }; 97 lambda_1 = new IntUnaryOperator[] { lambda_4[0], lambda_4[0], lambda_4[0], lambda_4[0], }; 98 } 99 100 private IntUnaryOperator makeLambda(int a) { 101 return x -> x + a; 102 } 103 104 public void process(Blackhole bh, IntUnaryOperator[] operations) { 105 for (int i = 0; i < LIMIT; i++) { 106 for (IntUnaryOperator op : operations) { 107 bh.consume(op.applyAsInt(i)); 108 } 109 } 110 } 111 112 @Benchmark 113 @OperationsPerInvocation(OPERATIONS) 114 public void inner1(Blackhole bh) { 115 process(bh, inner_1); 116 } 117 118 @Benchmark 119 @OperationsPerInvocation(OPERATIONS) 120 public void inner2(Blackhole bh) { 121 process(bh, inner_2); 122 } 123 124 @Benchmark 125 @OperationsPerInvocation(OPERATIONS) 126 public void inner4(Blackhole bh) { 127 process(bh, inner_4); 128 } 129 130 @Benchmark 131 @OperationsPerInvocation(OPERATIONS) 132 public void lambda1(Blackhole bh) { 133 process(bh, lambda_1); 134 } 135 136 @Benchmark 137 @OperationsPerInvocation(OPERATIONS) 138 public void lambda2(Blackhole bh) { 139 process(bh, lambda_2); 140 } 141 142 @Benchmark 143 @OperationsPerInvocation(OPERATIONS) 144 public void lambda4(Blackhole bh) { 145 process(bh, lambda_4); 146 } 147 148 } 149