1 /*
  2  *  Copyright (c) 2020, 2023, 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     @SuppressWarnings("initialization")
 94     MemorySegment segment = MemorySegment.ofArray(base);
 95 
 96     @Setup
 97     public void setup() {
 98         for (int i = 0; i < ELEM_SIZE; i++) {
 99             base[i] = i;
100         }
101     }
102 
103     @Benchmark
104     public int vh_loop() throws Throwable {
105         int sum = 0;
106         for (int i = 0; i < ELEM_SIZE; i++) {
107             sum += (int) VH_ARR_INT.get(base, i);
108         }
109         return sum;
110     }
111 
112     @Benchmark
113     public int vh_box_loop() throws Throwable {
114         int sum = 0;
115         for (int i = 0; i < ELEM_SIZE; i++) {
116             sum += ((IntBox) VH_BOX_ARR_INT.get(base, i)).intValue();
117         }
118         return sum;
119     }
120 
121     @Benchmark
122     public int mh_loop() throws Throwable {
123         int sum = 0;
124         for (int i = 0; i < ELEM_SIZE; i++) {
125             sum += (int) MH_INT.invokeExact(base, i);
126         }
127         return sum;
128     }
129 
130     @Benchmark
131     public int mh_box_loop() throws Throwable {
132         int sum = 0;
133         for (int i = 0; i < ELEM_SIZE; i++) {
134             sum += ((IntBox) MH_BOX_INT.invokeExact(base, i)).intValue();
135         }
136         return sum;
137     }
138 
139     @Benchmark
140     public int segment_loop() throws Throwable {
141         int sum = 0;
142         for (int i = 0; i < ELEM_SIZE; i++) {
143             sum += (int)VH_INT.get(segment, (long)i);
144         }
145         return sum;
146     }
147 
148     @Benchmark
149     public int segment_box_loop() throws Throwable {
150         int sum = 0;
151         for (int i = 0; i < ELEM_SIZE; i++) {
152             sum += ((IntBox)VH_BOX_INT.get(segment, (long)i)).intValue();
153         }
154         return sum;
155     }
156 }