1 /*
 2  * Copyright (c) 2023, 2024, 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 
24 package org.openjdk.bench.valhalla.intrinsics;
25 
26 import org.openjdk.jmh.annotations.*;
27 
28 import java.lang.invoke.MethodHandles;
29 import java.lang.invoke.VarHandle;
30 import java.util.concurrent.TimeUnit;
31 import jdk.internal.misc.Unsafe;
32 
33 @BenchmarkMode(Mode.Throughput)
34 @OutputTimeUnit(TimeUnit.MICROSECONDS)
35 @Fork(value = 1,
36       jvmArgsAppend = {"--add-opens", "java.base/jdk.internal.misc=ALL-UNNAMED",
37                        "--enable-preview"})
38 @Warmup(iterations = 3, time = 1)
39 @Measurement(iterations = 5, time = 1)
40 public class IsFlatArray {
41 
42     private static final Unsafe U = Unsafe.getUnsafe();
43     private static final VarHandle objectArrayVarHandle =
44         MethodHandles.arrayElementVarHandle(Object[].class);
45 
46     @State(Scope.Benchmark)
47     public static class ClassState {
48         public Class flatArrayClass = Point[].class;
49         public Class nonFlatArrayClass = String[].class;
50 
51         public Object[] objectArray = new Object[10];
52         public Object objectElement = new Object();
53         public int arrayIndex = 0;
54     }
55 
56     @Benchmark
57     public boolean testKnownFlatClass() {
58         return U.isFlatArray(Point[].class);
59     }
60 
61     @Benchmark
62     public boolean testKnownNonFlatClass() {
63         return U.isFlatArray(String[].class);
64     }
65 
66     @Benchmark
67     public boolean testUnknownFlatClass(ClassState state) {
68         return U.isFlatArray(state.flatArrayClass);
69     }
70 
71     @Benchmark
72     public boolean testUnknownNonFlatClass(ClassState state) {
73         return U.isFlatArray(state.nonFlatArrayClass);
74     }
75 
76     @Benchmark
77     public void setArrayElement(ClassState state) {
78         objectArrayVarHandle.set(state.objectArray, state.arrayIndex, state.objectElement);
79     }
80 
81     @Benchmark
82     public VarHandle makeArrayVarHandle() {
83         return MethodHandles.arrayElementVarHandle(Object[].class);
84     }
85 
86 }
87 
88 value class Point {
89     int x;
90     int y;
91     public Point(int x, int y) {
92         this.x = x;
93         this.y = y;
94     }
95 }