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