< prev index next >

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

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

@@ -20,11 +20,11 @@
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  package org.openjdk.bench.jdk.incubator.foreign;
  
- import jdk.incubator.foreign.MemoryAccess;
+ import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.incubator.foreign.ResourceScope;
  import org.openjdk.jmh.annotations.Benchmark;
  import org.openjdk.jmh.annotations.BenchmarkMode;

@@ -42,27 +42,27 @@
  import java.nio.ByteBuffer;
  import java.nio.ByteOrder;
  import java.util.concurrent.TimeUnit;
  
  import static jdk.incubator.foreign.MemoryLayout.PathElement.sequenceElement;
- import static jdk.incubator.foreign.MemoryLayouts.JAVA_INT;
+ import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
  
  @BenchmarkMode(Mode.AverageTime)
  @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
  @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
  @State(org.openjdk.jmh.annotations.Scope.Thread)
  @OutputTimeUnit(TimeUnit.MILLISECONDS)
- @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign" })
+ @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
  public class LoopOverNonConstant {
  
      static final Unsafe unsafe = Utils.unsafe;
  
      static final int ELEM_SIZE = 1_000_000;
      static final int CARRIER_SIZE = (int)JAVA_INT.byteSize();
      static final int ALLOC_SIZE = ELEM_SIZE * CARRIER_SIZE;
  
-     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(int.class, sequenceElement());
+     static final VarHandle VH_int = MemoryLayout.sequenceLayout(JAVA_INT).varHandle(sequenceElement());
      MemorySegment segment;
      long unsafe_addr;
  
      ByteBuffer byteBuffer;
  

@@ -115,23 +115,52 @@
          }
          return res;
      }
  
      @Benchmark
-     public int segment_loop_static() {
-         int res = 0;
-         for (int i = 0; i < ELEM_SIZE; i ++) {
-             res += MemoryAccess.getIntAtIndex(segment, i);
+     public int segment_loop() {
+         int sum = 0;
+         for (int i = 0; i < ELEM_SIZE; i++) {
+             sum += (int) VH_int.get(segment, (long) i);
          }
-         return res;
+         return sum;
      }
  
      @Benchmark
-     public int segment_loop() {
+     public int segment_loop_instance() {
          int sum = 0;
          for (int i = 0; i < ELEM_SIZE; i++) {
-             sum += (int) VH_int.get(segment, (long) i);
+             sum += segment.get(JAVA_INT, i * CARRIER_SIZE);
+ 
+         }
+         return sum;
+     }
+ 
+     @Benchmark
+     public int segment_loop_instance_index() {
+         int sum = 0;
+         for (int i = 0; i < ELEM_SIZE; i++) {
+             sum += segment.getAtIndex(JAVA_INT, i);
+ 
+         }
+         return sum;
+     }
+ 
+     @Benchmark
+     public int segment_loop_instance_address() {
+         int sum = 0;
+         for (int i = 0; i < ELEM_SIZE; i++) {
+             sum += segment.address().get(JAVA_INT, i * CARRIER_SIZE);
+         }
+         return sum;
+     }
+ 
+     @Benchmark
+     public int segment_loop_instance_address_index() {
+         int sum = 0;
+         for (int i = 0; i < ELEM_SIZE; i++) {
+             sum += segment.address().getAtIndex(JAVA_INT, i);
          }
          return sum;
      }
  
      @Benchmark
< prev index next >