1 /*
  2  * Copyright (c) 2021, 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 package org.openjdk.bench.jdk.incubator.foreign;
 24 
 25 import jdk.incubator.foreign.MemoryLayout;
 26 import jdk.incubator.foreign.MemorySegment;
 27 import org.openjdk.jmh.annotations.Benchmark;
 28 import org.openjdk.jmh.annotations.BenchmarkMode;
 29 import org.openjdk.jmh.annotations.CompilerControl;
 30 import org.openjdk.jmh.annotations.Fork;
 31 import org.openjdk.jmh.annotations.Measurement;
 32 import org.openjdk.jmh.annotations.Mode;
 33 import org.openjdk.jmh.annotations.OutputTimeUnit;
 34 import org.openjdk.jmh.annotations.Param;
 35 import org.openjdk.jmh.annotations.Setup;
 36 import org.openjdk.jmh.annotations.State;
 37 import org.openjdk.jmh.annotations.TearDown;
 38 import org.openjdk.jmh.annotations.Warmup;
 39 import sun.misc.Unsafe;
 40 
 41 import java.lang.invoke.VarHandle;
 42 import java.nio.IntBuffer;
 43 import java.util.concurrent.TimeUnit;
 44 
 45 import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
 46 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 47 
 48 @BenchmarkMode(Mode.AverageTime)
 49 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 50 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 51 @State(org.openjdk.jmh.annotations.Scope.Thread)
 52 @OutputTimeUnit(TimeUnit.MILLISECONDS)
 53 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
 54 public class LoopOverNewHeap {
 55 
 56     static final Unsafe unsafe = Utils.unsafe;
 57 
 58     static final int ELEM_SIZE = 1_000_000;
 59     static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
 60 
 61     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(sequenceElement());
 62 
 63     @Param(value = {"false", "true"})
 64     boolean polluteProfile;
 65 
 66     @Setup
 67     public void setup() {
 68         if (polluteProfile) {
 69             for (int i = 0 ; i < 10000 ; i++) {
 70                 MemorySegment intB = MemorySegment.ofArray(new byte[ELEM_SIZE]);
 71                 MemorySegment intI = MemorySegment.ofArray(new int[ELEM_SIZE]);
 72                 MemorySegment intD = MemorySegment.ofArray(new double[ELEM_SIZE]);
 73                 MemorySegment intF = MemorySegment.ofArray(new float[ELEM_SIZE]);
 74             }
 75         }
 76     }
 77 
 78     @Benchmark
 79     public void unsafe_loop() {
 80         int[] elems = new int[ELEM_SIZE];
 81         for (int i = 0; i < ELEM_SIZE; i++) {
 82             unsafe.putInt(elems, Unsafe.ARRAY_INT_BASE_OFFSET + (i * CARRIER_SIZE) , i);
 83         }
 84     }
 85 
 86 
 87     @Benchmark
 88     public void segment_loop() {
 89         MemorySegment segment = MemorySegment.ofArray(new int[ELEM_SIZE]);
 90         for (int i = 0; i < ELEM_SIZE; i++) {
 91             VH_int.set(segment, (long) i, i);
 92         }
 93     }
 94 
 95     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
 96     @Benchmark
 97     public void segment_loop_dontinline() {
 98         MemorySegment segment = MemorySegment.ofArray(new int[ELEM_SIZE]);
 99         for (int i = 0; i < ELEM_SIZE; i++) {
100             VH_int.set(segment, (long) i, i);
101         }
102     }
103 
104     @Benchmark
105     public void buffer_loop() {
106         IntBuffer buffer = IntBuffer.wrap(new int[ELEM_SIZE]);
107         for (int i = 0; i < ELEM_SIZE; i++) {
108             buffer.put(i , i);
109         }
110     }
111 
112 }