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.value.ValueClass;
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 "--add-opens", "java.base/jdk.internal.value=ALL-UNNAMED",
38 "--enable-preview"})
39 @Warmup(iterations = 3, time = 1)
40 @Measurement(iterations = 5, time = 1)
41 public class IsFlatArray {
42
43 private static final VarHandle objectArrayVarHandle =
44 MethodHandles.arrayElementVarHandle(Object[].class);
45
46 @State(Scope.Benchmark)
47 public static class ClassState {
48 public Object[] flatArray = new Point[10];
49 public Object[] nonFlatArray = new String[10];
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 ValueClass.isFlatArray(new Point[8]);
59 }
60
61 @Benchmark
62 public boolean testKnownNonFlatClass() {
63 return ValueClass.isFlatArray(new String[8]);
64 }
65
66 @Benchmark
67 public boolean testUnknownFlatClass(ClassState state) {
68 return ValueClass.isFlatArray(state.flatArray);
69 }
70
71 @Benchmark
72 public boolean testUnknownNonFlatClass(ClassState state) {
73 return ValueClass.isFlatArray(state.nonFlatArray);
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 }