1 /* 2 * Copyright (c) 2020, 2022, 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 25 package org.openjdk.bench.java.lang.foreign; 26 27 import java.lang.foreign.MemorySegment; 28 29 import org.openjdk.jmh.annotations.Benchmark; 30 import org.openjdk.jmh.annotations.BenchmarkMode; 31 import org.openjdk.jmh.annotations.Fork; 32 import org.openjdk.jmh.annotations.Measurement; 33 import org.openjdk.jmh.annotations.Mode; 34 import org.openjdk.jmh.annotations.OutputTimeUnit; 35 import org.openjdk.jmh.annotations.Setup; 36 import org.openjdk.jmh.annotations.State; 37 import org.openjdk.jmh.annotations.Warmup; 38 39 import java.lang.invoke.MethodHandle; 40 import java.lang.invoke.MethodHandles; 41 import java.lang.invoke.MethodType; 42 import java.lang.invoke.VarHandle; 43 import java.util.concurrent.TimeUnit; 44 45 @BenchmarkMode(Mode.AverageTime) 46 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) 47 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) 48 @State(org.openjdk.jmh.annotations.Scope.Thread) 49 @OutputTimeUnit(TimeUnit.MILLISECONDS) 50 @Fork(3) 51 public class TestAdaptVarHandles extends JavaLayouts { 52 53 static class IntBox { 54 55 private final int value; 56 57 IntBox(int value) { 58 this.value = value; 59 } 60 61 int intValue() { 62 return value; 63 } 64 } 65 66 static final int ELEM_SIZE = 1_000_000; 67 68 static final MethodHandle INT_TO_INTBOX; 69 static final MethodHandle INTBOX_TO_INT; 70 71 static { 72 try { 73 INT_TO_INTBOX = MethodHandles.lookup() 74 .findConstructor(IntBox.class, MethodType.methodType(void.class, int.class)); 75 INTBOX_TO_INT = MethodHandles.lookup() 76 .findVirtual(IntBox.class, "intValue", MethodType.methodType(int.class)); 77 } catch (Throwable ex) { 78 throw new ExceptionInInitializerError(ex); 79 } 80 } 81 82 static final VarHandle VH_ARR_INT = MethodHandles.arrayElementVarHandle(int[].class); 83 84 static final VarHandle VH_BOX_ARR_INT = MethodHandles.filterValue(VH_ARR_INT, INTBOX_TO_INT, INT_TO_INTBOX); 85 86 static final VarHandle VH_BOX_INT = MethodHandles.filterValue(VH_INT, INTBOX_TO_INT, INT_TO_INTBOX); 87 88 static final MethodHandle MH_INT = VH_ARR_INT.toMethodHandle(VarHandle.AccessMode.GET); 89 90 static final MethodHandle MH_BOX_INT = MethodHandles.filterReturnValue(MH_INT, INT_TO_INTBOX); 91 92 int[] base = new int[ELEM_SIZE]; 93 MemorySegment segment = MemorySegment.ofArray(base); 94 95 @Setup 96 public void setup() { 97 for (int i = 0; i < ELEM_SIZE; i++) { 98 base[i] = i; 99 } 100 } 101 102 @Benchmark 103 public int vh_loop() throws Throwable { 104 int sum = 0; 105 for (int i = 0; i < ELEM_SIZE; i++) { 106 sum += (int) VH_ARR_INT.get(base, i); 107 } 108 return sum; 109 } 110 111 @Benchmark 112 public int vh_box_loop() throws Throwable { 113 int sum = 0; 114 for (int i = 0; i < ELEM_SIZE; i++) { 115 sum += ((IntBox) VH_BOX_ARR_INT.get(base, i)).intValue(); 116 } 117 return sum; 118 } 119 120 @Benchmark 121 public int mh_loop() throws Throwable { 122 int sum = 0; 123 for (int i = 0; i < ELEM_SIZE; i++) { 124 sum += (int) MH_INT.invokeExact(base, i); 125 } 126 return sum; 127 } 128 129 @Benchmark 130 public int mh_box_loop() throws Throwable { 131 int sum = 0; 132 for (int i = 0; i < ELEM_SIZE; i++) { 133 sum += ((IntBox) MH_BOX_INT.invokeExact(base, i)).intValue(); 134 } 135 return sum; 136 } 137 138 @Benchmark 139 public int segment_loop() throws Throwable { 140 int sum = 0; 141 for (int i = 0; i < ELEM_SIZE; i++) { 142 sum += (int)VH_INT.get(segment, (long)i); 143 } 144 return sum; 145 } 146 147 @Benchmark 148 public int segment_box_loop() throws Throwable { 149 int sum = 0; 150 for (int i = 0; i < ELEM_SIZE; i++) { 151 sum += ((IntBox)VH_BOX_INT.get(segment, (long)i)).intValue(); 152 } 153 return sum; 154 } 155 }