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