< prev index next >

test/micro/org/openjdk/bench/jdk/incubator/foreign/TestAdaptVarHandles.java

Print this page

  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)

 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;

  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.MemorySegment;
 30 import jdk.incubator.foreign.ValueLayout;
 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)

 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(ValueLayout.JAVA_INT)
 89             .varHandle(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;
< prev index next >