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