< prev index next >

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

Print this page

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package org.openjdk.bench.jdk.incubator.foreign;
 27 

 28 import jdk.incubator.foreign.CLinker;
 29 import jdk.incubator.foreign.FunctionDescriptor;
 30 import jdk.incubator.foreign.MemoryAccess;
 31 import jdk.incubator.foreign.MemoryAddress;
 32 import jdk.incubator.foreign.MemorySegment;
 33 import jdk.incubator.foreign.ResourceScope;
 34 import jdk.incubator.foreign.SegmentAllocator;
 35 import org.openjdk.jmh.annotations.Benchmark;
 36 import org.openjdk.jmh.annotations.BenchmarkMode;
 37 import org.openjdk.jmh.annotations.Fork;
 38 import org.openjdk.jmh.annotations.Setup;
 39 import org.openjdk.jmh.annotations.Param;
 40 import org.openjdk.jmh.annotations.TearDown;
 41 import org.openjdk.jmh.annotations.Measurement;
 42 import org.openjdk.jmh.annotations.Mode;
 43 import org.openjdk.jmh.annotations.OutputTimeUnit;
 44 import org.openjdk.jmh.annotations.State;
 45 import org.openjdk.jmh.annotations.Warmup;
 46 
 47 import java.lang.invoke.MethodHandle;
 48 import java.lang.invoke.MethodType;
 49 import java.util.concurrent.TimeUnit;
 50 
 51 import static jdk.incubator.foreign.CLinker.*;
 52 
 53 @BenchmarkMode(Mode.AverageTime)
 54 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 55 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 56 @State(org.openjdk.jmh.annotations.Scope.Thread)
 57 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 58 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
 59 public class StrLenTest {
 60 
 61     ResourceScope scope = ResourceScope.newConfinedScope();
 62 
 63     SegmentAllocator segmentAllocator;
 64     SegmentAllocator arenaAllocator = SegmentAllocator.arenaAllocator(scope);
 65 
 66     @Param({"5", "20", "100"})
 67     public int size;
 68     public String str;
 69 
 70     static {
 71         System.loadLibrary("StrLen");
 72     }
 73 
 74     static final MethodHandle STRLEN;
 75     static final MethodHandle STRLEN_TRIVIAL;
 76     static final MethodHandle MALLOC_TRIVIAL;
 77     static final MethodHandle FREE_TRIVIAL;
 78 
 79     static {
 80         CLinker abi = CLinker.getInstance();
 81         STRLEN = abi.downcallHandle(CLinker.systemLookup().lookup("strlen").get(),
 82                 MethodType.methodType(int.class, MemoryAddress.class),
 83                 FunctionDescriptor.of(C_INT, C_POINTER));
 84         STRLEN_TRIVIAL = abi.downcallHandle(CLinker.systemLookup().lookup("strlen").get(),
 85                 MethodType.methodType(int.class, MemoryAddress.class),
 86                 FunctionDescriptor.of(C_INT, C_POINTER).withAttribute(FunctionDescriptor.TRIVIAL_ATTRIBUTE_NAME, true));
 87         MALLOC_TRIVIAL = abi.downcallHandle(CLinker.systemLookup().lookup("malloc").get(),
 88                 MethodType.methodType(MemoryAddress.class, long.class),
 89                 FunctionDescriptor.of(C_POINTER, C_LONG_LONG).withAttribute(FunctionDescriptor.TRIVIAL_ATTRIBUTE_NAME, true));
 90 
 91         FREE_TRIVIAL = abi.downcallHandle(CLinker.systemLookup().lookup("free").get(),
 92                 MethodType.methodType(void.class, MemoryAddress.class),
 93                 FunctionDescriptor.ofVoid(C_POINTER).withAttribute(FunctionDescriptor.TRIVIAL_ATTRIBUTE_NAME, true));
 94     }
 95 
 96     @Setup
 97     public void setup() {
 98         str = makeString(size);
 99         segmentAllocator = SegmentAllocator.ofSegment(MemorySegment.allocateNative(size + 1, ResourceScope.newImplicitScope()));
100     }
101 
102     @TearDown
103     public void tearDown() {
104         scope.close();
105     }
106 
107     @Benchmark
108     public int jni_strlen() throws Throwable {
109         return strlen(str);
110     }
111 
112     @Benchmark
113     public int panama_strlen() throws Throwable {
114         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
115             MemorySegment segment = CLinker.toCString(str, scope);
116             return (int)STRLEN.invokeExact(segment.address());

117         }
118     }
119 
120     @Benchmark
121     public int panama_strlen_arena() throws Throwable {
122         return (int)STRLEN.invokeExact(CLinker.toCString(str, arenaAllocator).address());
123     }
124 
125     @Benchmark
126     public int panama_strlen_prefix() throws Throwable {
127         return (int)STRLEN.invokeExact(CLinker.toCString(str, segmentAllocator).address());
128     }
129 
130     @Benchmark
131     public int panama_strlen_unsafe() throws Throwable {
132         MemoryAddress address = makeStringUnsafe(str);
133         int res = (int) STRLEN.invokeExact(address);
134         CLinker.freeMemory(address);
135         return res;
136     }
137 
138     @Benchmark
139     public int panama_strlen_unsafe_trivial() throws Throwable {
140         MemoryAddress address = makeStringUnsafeTrivial(str);
141         int res = (int) STRLEN_TRIVIAL.invokeExact(address);
142         FREE_TRIVIAL.invokeExact(address);
143         return res;
144     }
145 
146     static MemoryAddress makeStringUnsafe(String s) {
147         byte[] bytes = s.getBytes();
148         int len = bytes.length;
149         MemoryAddress address = CLinker.allocateMemory(len + 1);
150         MemorySegment str = address.asSegment(len + 1, ResourceScope.globalScope());
151         str.copyFrom(MemorySegment.ofArray(bytes));
152         MemoryAccess.setByteAtOffset(str, len, (byte)0);
153         return address;
154     }
155 
156     static MemoryAddress makeStringUnsafeTrivial(String s) throws Throwable {
157         byte[] bytes = s.getBytes();
158         int len = bytes.length;
159         MemoryAddress address = (MemoryAddress)MALLOC_TRIVIAL.invokeExact((long)len + 1);
160         MemorySegment str = address.asSegment(len + 1, ResourceScope.globalScope());
161         str.copyFrom(MemorySegment.ofArray(bytes));
162         MemoryAccess.setByteAtOffset(str, len, (byte)0);
163         return address;
164     }
165 
166     static native int strlen(String str);
167 
168     static String makeString(int size) {
169         String lorem = """
170                 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
171                  dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
172                  ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
173                  fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
174                  mollit anim id est laborum.
175                 """;
176         return lorem.substring(0, size);
177     }
178 }

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package org.openjdk.bench.jdk.incubator.foreign;
 27 
 28 import jdk.incubator.foreign.Addressable;
 29 import jdk.incubator.foreign.CLinker;
 30 import jdk.incubator.foreign.FunctionDescriptor;

 31 import jdk.incubator.foreign.MemoryAddress;
 32 import jdk.incubator.foreign.MemorySegment;
 33 import jdk.incubator.foreign.ResourceScope;
 34 import jdk.incubator.foreign.SegmentAllocator;
 35 import org.openjdk.jmh.annotations.Benchmark;
 36 import org.openjdk.jmh.annotations.BenchmarkMode;
 37 import org.openjdk.jmh.annotations.Fork;
 38 import org.openjdk.jmh.annotations.Setup;
 39 import org.openjdk.jmh.annotations.Param;
 40 import org.openjdk.jmh.annotations.TearDown;
 41 import org.openjdk.jmh.annotations.Measurement;
 42 import org.openjdk.jmh.annotations.Mode;
 43 import org.openjdk.jmh.annotations.OutputTimeUnit;
 44 import org.openjdk.jmh.annotations.State;
 45 import org.openjdk.jmh.annotations.Warmup;
 46 
 47 import java.lang.invoke.MethodHandle;

 48 import java.util.concurrent.TimeUnit;
 49 
 50 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 51 
 52 @BenchmarkMode(Mode.AverageTime)
 53 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 54 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 55 @State(org.openjdk.jmh.annotations.Scope.Thread)
 56 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 57 @Fork(value = 3, jvmArgsAppend = { "--add-modules=jdk.incubator.foreign", "--enable-native-access=ALL-UNNAMED" })
 58 public class StrLenTest extends CLayouts {
 59 
 60     ResourceScope scope = ResourceScope.newImplicitScope();
 61 
 62     SegmentAllocator segmentAllocator;
 63     SegmentAllocator arenaAllocator = SegmentAllocator.newNativeArena(scope);
 64 
 65     @Param({"5", "20", "100"})
 66     public int size;
 67     public String str;
 68 
 69     static {
 70         System.loadLibrary("StrLen");
 71     }
 72 
 73     static final MethodHandle STRLEN;



 74 
 75     static {
 76         CLinker abi = CLinker.systemCLinker();
 77         STRLEN = abi.downcallHandle(abi.lookup("strlen").get(),

 78                 FunctionDescriptor.of(C_INT, C_POINTER));










 79     }
 80 
 81     @Setup
 82     public void setup() {
 83         str = makeString(size);
 84         segmentAllocator = SegmentAllocator.prefixAllocator(MemorySegment.allocateNative(size + 1, ResourceScope.newConfinedScope()));
 85     }
 86 
 87     @TearDown
 88     public void tearDown() {
 89         scope.close();
 90     }
 91 
 92     @Benchmark
 93     public int jni_strlen() throws Throwable {
 94         return strlen(str);
 95     }
 96 
 97     @Benchmark
 98     public int panama_strlen() throws Throwable {
 99         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
100             MemorySegment segment = MemorySegment.allocateNative(str.length() + 1, scope);
101             segment.setUtf8String(0, str);
102             return (int)STRLEN.invokeExact((Addressable)segment);
103         }
104     }
105 
106     @Benchmark
107     public int panama_strlen_arena() throws Throwable {
108         return (int)STRLEN.invokeExact((Addressable)arenaAllocator.allocateUtf8String(str));
109     }
110 
111     @Benchmark
112     public int panama_strlen_prefix() throws Throwable {
113         return (int)STRLEN.invokeExact((Addressable)segmentAllocator.allocateUtf8String(str));
114     }
115 
116     @Benchmark
117     public int panama_strlen_unsafe() throws Throwable {
118         MemoryAddress address = makeStringUnsafe(str);
119         int res = (int) STRLEN.invokeExact((Addressable)address);
120         freeMemory(address);








121         return res;
122     }
123 
124     static MemoryAddress makeStringUnsafe(String s) {
125         byte[] bytes = s.getBytes();
126         int len = bytes.length;
127         MemoryAddress address = allocateMemory(len + 1);
128         MemorySegment str = MemorySegment.ofAddressNative(address, len + 1, ResourceScope.globalScope());










129         str.copyFrom(MemorySegment.ofArray(bytes));
130         str.set(JAVA_BYTE, len, (byte)0);
131         return address;
132     }
133 
134     static native int strlen(String str);
135 
136     static String makeString(int size) {
137         String lorem = """
138                 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
139                  dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
140                  ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
141                  fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
142                  mollit anim id est laborum.
143                 """;
144         return lorem.substring(0, size);
145     }
146 }
< prev index next >