├── .circleci └── config.yml ├── .clabot ├── .github └── dependabot.yml ├── .gitignore ├── ArrayPassingBenchmarks.md ├── DataBenchmarks.md ├── LICENSE ├── ObjectCreationBenchmarks.md ├── README.md ├── analysis ├── get_benchmarks │ ├── fig_1024_1_copyout_allbig.png │ ├── fig_1024_1_copyout_allsmall.png │ ├── fig_1024_1_copyout_ba.png │ ├── fig_1024_1_copyout_bb.png │ ├── fig_1024_1_copyout_crit.png │ ├── fig_1024_1_copyout_dir.png │ ├── fig_1024_1_copyout_getelems.png │ ├── fig_1024_1_copyout_ind.png │ ├── fig_1024_1_copyout_nopoolbig.png │ ├── fig_1024_1_copyout_nopoolsmall.png │ ├── fig_1024_1_copyout_setreg.png │ ├── fig_1024_1_none_allbig.png │ ├── fig_1024_1_none_allsmall.png │ ├── fig_1024_1_none_ba.png │ ├── fig_1024_1_none_bb.png │ ├── fig_1024_1_none_crit.png │ ├── fig_1024_1_none_dir.png │ ├── fig_1024_1_none_getelems.png │ ├── fig_1024_1_none_ind.png │ ├── fig_1024_1_none_nopoolbig.png │ ├── fig_1024_1_none_nopoolsmall.png │ ├── fig_1024_1_none_setreg.png │ └── jmh_2021-05-31T18:04:48.379041.md ├── put_benchmarks │ ├── fig_1024_1_17_copyin_allbig.png │ ├── fig_1024_1_17_copyin_allsmall.png │ ├── fig_1024_1_17_copyin_ba.png │ ├── fig_1024_1_17_copyin_bb.png │ ├── fig_1024_1_17_copyin_crit.png │ ├── fig_1024_1_17_copyin_dir.png │ ├── fig_1024_1_17_copyin_getelems.png │ ├── fig_1024_1_17_copyin_ind.png │ ├── fig_1024_1_17_copyin_nopoolbig.png │ ├── fig_1024_1_17_copyin_nopoolsmall.png │ ├── fig_1024_1_17_copyin_setreg.png │ ├── fig_1024_1_17_none_allbig.png │ ├── fig_1024_1_17_none_allsmall.png │ ├── fig_1024_1_17_none_ba.png │ ├── fig_1024_1_17_none_bb.png │ ├── fig_1024_1_17_none_crit.png │ ├── fig_1024_1_17_none_dir.png │ ├── fig_1024_1_17_none_getelems.png │ ├── fig_1024_1_17_none_ind.png │ ├── fig_1024_1_17_none_nopoolbig.png │ ├── fig_1024_1_17_none_nopoolsmall.png │ ├── fig_1024_1_17_none_setreg.png │ └── jmh_2021-05-28T11:40:43.503533.md ├── run3 │ ├── fig_cacheEntryOverhead_cacheMB_checksum_1024_16_copyout.png │ ├── fig_cacheEntryOverhead_cacheMB_checksum_1024_16_none.png │ ├── fig_cacheEntryOverhead_cacheMB_checksum_1024_4_copyout.png │ ├── fig_cacheEntryOverhead_cacheMB_checksum_1024_4_none.png │ └── jmh_2021-05-21T12:05:44.202319.md └── run6dv │ ├── fig_2021-05-24T16:05:47.731360_1024_1_copyout.png │ ├── fig_2021-05-24T16:05:47.731360_1024_1_none.png │ ├── fig_cacheEntryOverhead_cacheMB_checksum_1024_1_copyout.png │ ├── fig_cacheEntryOverhead_cacheMB_checksum_1024_1_none.png │ └── jmh_2021-05-24T13:29:03.036836.md ├── array-benchmark-100.sh ├── benchmark-100-with-close.sh ├── benchmark-100.sh ├── benchmark-results.png ├── jmh-benchmarks-parametrized.sh ├── jmh-benchmarks.sh ├── jmh_full_get.json ├── jmh_full_put.json ├── jmh_plot.json ├── jmh_run.json ├── jmh_small_get.json ├── jmh_small_put.json ├── jmh_tiny_get.json ├── jmh_tiny_put.json ├── jmhplot.py ├── jmhrun.py ├── jni-arrays-size-2.png ├── jni-arrays-size-20.png ├── pom.xml └── src └── main ├── assembly └── appassembler-output.xml ├── c++ ├── array │ ├── AllocateInCppGet2DArray.cpp │ ├── AllocateInCppGetArray.cpp │ ├── AllocateInCppGetArrayList.cpp │ ├── AllocateInJavaGet2DArray.cpp │ ├── AllocateInJavaGetArray.cpp │ ├── AllocateInJavaGetArrayList.cpp │ ├── AllocateInJavaGetMutableArray.cpp │ ├── FooNativeObjectArray.cpp │ ├── FooObject.h │ └── Portal.h ├── bytearray │ ├── GetByteArray.cpp │ └── Portal.h ├── call │ ├── Foo.cpp │ ├── Foo.h │ ├── FooByCall.cpp │ ├── FooByCallFinal.cpp │ ├── FooByCallInvoke.cpp │ ├── FooByCallInvokeFinal.cpp │ ├── FooByCallStatic.cpp │ ├── FooByCallStaticFinal.cpp │ └── Portal.h └── getputjni │ └── GetPutJNI.cpp └── java └── com └── evolvedbinary └── jnibench ├── common ├── NativeBackedObject.java ├── array │ ├── AllocateInCppGet2DArray.java │ ├── AllocateInCppGet2DArrayListWrapper.java │ ├── AllocateInCppGetArray.java │ ├── AllocateInCppGetArrayList.java │ ├── AllocateInJavaGet2DArray.java │ ├── AllocateInJavaGetArray.java │ ├── AllocateInJavaGetArrayList.java │ ├── AllocateInJavaGetMutableArray.java │ ├── FooNativeObjectArray.java │ ├── FooObject.java │ ├── JniListSupplier.java │ └── NativeObjectArray.java ├── bytearray │ └── GetByteArray.java ├── call │ ├── FooByCall.java │ ├── FooByCallFinal.java │ ├── FooByCallInvoke.java │ ├── FooByCallInvokeFinal.java │ ├── FooByCallStatic.java │ └── FooByCallStaticFinal.java └── getputjni │ └── GetPutJNI.java ├── consbench ├── ArrayBenchmark.java ├── ArrayBenchmarkFixture.java ├── Benchmark.java ├── BenchmarkFixture.java ├── BenchmarkHelper.java ├── BenchmarkInterface.java ├── BenchmarkOptions.java ├── CallBenchmark.java └── CallBenchmarkFixture.java └── jmhbench ├── ArrayBenchmark.java ├── ByteArrayFromNativeBenchmark.java ├── ByteArrayToNativeBenchmark.java ├── ConstructionBenchmark.java ├── GetJNIBenchmark.java ├── GetJNIUnsafeBenchmark.java ├── PutJNIBenchmark.java ├── cache ├── AllocationCache.java ├── BaseByteBufferCache.java ├── ByteArrayCache.java ├── DirectByteBufferCache.java ├── IndirectByteBufferCache.java ├── LinkedListAllocationCache.java ├── NettyByteBufCache.java └── UnsafeBufferCache.java └── common └── JMHCaller.java /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | jobs: 4 | build-and-test: 5 | parameters: 6 | jdk: 7 | type: string 8 | docker: 9 | - image: cimg/openjdk:<< parameters.jdk >> 10 | resource_class: small 11 | steps: 12 | - checkout 13 | - run: 14 | name: Build 15 | command: mvn -B -DskipTests clean package 16 | 17 | workflows: 18 | build: 19 | jobs: 20 | - build-and-test: 21 | matrix: 22 | parameters: 23 | jdk: ["8.0"] 24 | -------------------------------------------------------------------------------- /.clabot: -------------------------------------------------------------------------------- 1 | { 2 | "contributors": [ 3 | "adamretter", 4 | "alanpaxton", 5 | "alucarded", 6 | "duncdrum", 7 | "dependabot-preview", 8 | "dependabot-preview[bot]", 9 | "dependabot[bot]" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: maven 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | ignore: 9 | - dependency-name: com.mycila:license-maven-plugin 10 | versions: 11 | - "4.0" 12 | - dependency-name: org.openjdk.jmh:jmh-generator-annprocess 13 | versions: 14 | - "1.28" 15 | - dependency-name: org.openjdk.jmh:jmh-core 16 | versions: 17 | - "1.28" 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | 3 | .idea/ 4 | *.iml 5 | 6 | *.csv 7 | 8 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 9 | hs_err_pid* 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Evolved Binary Ltd 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of jni-construction-benchmark nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /ObjectCreationBenchmarks.md: -------------------------------------------------------------------------------- 1 | # JNI Object Creation Benchmarks 2 | 3 | The code contrasts three different approaches to constructing a Java Object that 4 | wraps a C++ object (which it has to construct). Such a scenario is common when 5 | writing a Java API wrapper for an existing C++ project. 6 | 7 | ## Scenario 1 - By Call 8 | 9 | From Java we call a JNI C++ member function to construct the C++ object and 10 | return a `jlong` which represents the memory pointer to the C++ object. 11 | 12 | ```Java 13 | public class FooByCall extends NativeBackedObject { 14 | public FooByCall() { 15 | super(); 16 | this._nativeHandle = newFoo(); 17 | } 18 | 19 | private native long newFoo(); 20 | 21 | ... 22 | 23 | ``` 24 | 25 | ```C++ 26 | jlong Java_com_evolvedbinary_jni_consbench_FooByCall_newFoo(JNIEnv* env, jobject jobj) { 27 | consbench::Foo* foo = new consbench::Foo(); 28 | return reinterpret_cast(foo); 29 | } 30 | ``` 31 | 32 | ## Scenario 2 - By Call, Static 33 | 34 | Similar to _Scenario 1_, except that we use a static call to a JNI C++ function. 35 | 36 | ```java 37 | public class FooByCallStatic extends NativeBackedObject { 38 | public FooByCallStatic() { 39 | super(); 40 | this._nativeHandle = newFoo(); 41 | } 42 | 43 | private static native long newFoo(); 44 | 45 | ... 46 | ``` 47 | 48 | ```C++ 49 | jlong Java_com_evolvedbinary_jni_consbench_FooByCallStatic_newFoo(JNIEnv* env, jclass jcls) { 50 | consbench::Foo* foo = new consbench::Foo(); 51 | return reinterpret_cast(foo); 52 | } 53 | ``` 54 | 55 | ## Scenario 3 - By Call, Invoke 56 | 57 | Similar to _Scenario 1_, however instead of returning a `jlong` pointer, we 58 | instead in C++ find the `_nativeHandle` member of the calling Java object, and 59 | then directly set the `long` field from C++. 60 | 61 | ```java 62 | public class FooByCallInvoke extends NativeBackedObject { 63 | public FooByCallInvoke() { 64 | super(); 65 | newFoo(); //the native method, will find _nativeHandle from the class and set it directly 66 | } 67 | 68 | private native void newFoo(); 69 | 70 | ... 71 | ``` 72 | 73 | ```C++ 74 | void Java_com_evolvedbinary_jni_consbench_FooByCallInvoke_newFoo(JNIEnv* env, jobject jobj) { 75 | consbench::Foo* foo = new consbench::Foo(); 76 | 77 | //set the _nativeHandle in Java 78 | consbench::FooByCallInvokeJni::setHandle(env, jobj, foo); 79 | } 80 | 81 | template class FooJniClass { 82 | public: 83 | // Get the java class id 84 | static jclass getJClass(JNIEnv* env, const char* jclazz_name) { 85 | jclass jclazz = env->FindClass(jclazz_name); 86 | assert(jclazz != nullptr); 87 | return jclazz; 88 | } 89 | 90 | // Get the field id of the member variable to store 91 | // the ptr 92 | static jfieldID getHandleFieldID(JNIEnv* env) { 93 | static jfieldID fid = env->GetFieldID( 94 | DERIVED::getJClass(env), "_nativeHandle", "J"); 95 | assert(fid != nullptr); 96 | return fid; 97 | } 98 | 99 | // Get the pointer from Java 100 | static PTR getHandle(JNIEnv* env, jobject jobj) { 101 | return reinterpret_cast( 102 | env->GetLongField(jobj, getHandleFieldID(env))); 103 | } 104 | 105 | // Pass the pointer to the java side. 106 | static void setHandle(JNIEnv* env, jobject jdb, PTR ptr) { 107 | env->SetLongField( 108 | jdb, getHandleFieldID(env), 109 | reinterpret_cast(ptr)); 110 | } 111 | }; 112 | 113 | 114 | // The portal class for com.evolvedbinary.jni.consbench.FooByCallInvoke 115 | class FooByCallInvokeJni : public FooJniClass { 116 | public: 117 | static jclass getJClass(JNIEnv* env) { 118 | return FooJniClass::getJClass(env, 119 | "com/evolvedbinary/jni/consbench/FooByCallInvoke"); 120 | } 121 | }; 122 | 123 | ``` 124 | 125 | ## Scenario 4, 5, and 6 126 | 127 | Scenarios 4, 5, and 6 are similar to 1, 2, and 3 respectively, except that the 128 | Java classes have been marked as `final`. 129 | 130 | ## Object Creation Results 131 | 132 | Test machine: MacBook Pro 15-inch 2019: 2.4 GHz 8-Core Intel Core i9 / 32 GB 133 | 2400 MHz DDR4. OS X 10.15.2 / Oracle JDK 8. 134 | 135 | ```bash 136 | $ java -version 137 | java version "1.8.0_221" 138 | Java(TM) SE Runtime Environment (build 1.8.0_221-b11) 139 | Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) 140 | ``` 141 | 142 | ``` 143 | $ clang --version 144 | Apple clang version 11.0.0 (clang-1100.0.33.16) 145 | Target: x86_64-apple-darwin19.2.0 146 | Thread model: posix 147 | ``` 148 | 149 | The `com.evolvedbinary.jnibench.consbench.Benchmark` class already calls each 150 | scenario 1,000,000 times, so for the benchmark we repeated this 100 times and 151 | plotted the results. 152 | 153 | ![Image of JNI Object Creation Benchmark Results](https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/main/benchmark-results.png) 154 | 155 | ## Object Creation Conclusions 156 | 157 | The difference between the non-final (Scenarios 1 - 3) and the final (Scenarios 158 | 4 - 6) class versions is so small that it could easily be accounted for by 159 | system noise. 160 | 161 | Scenario 2 and 5 - By Call, Static, appear to have the lowest JNI overhead for 162 | constructing C++ objects from Java. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JNI Benchmarks 2 | 3 | [![Build Status](https://dl.circleci.com/status-badge/img/gh/evolvedbinary/jni-benchmarks/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/evolvedbinary/jni-benchmarks/tree/main) 4 | [![License](https://img.shields.io/badge/license-BSD%203-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) 5 | 6 | We provide the code for a small set of benchmarks to compare various approaches 7 | to solving common JNI use-cases and then present the results. 8 | 9 | The benchmarks at present are: 10 | 11 | 1. [com.evolvedbinary.jnibench.common.call](tree/main/src/main/java/com/evolvedbinary/jnibench/common/call) - 12 | Benchmarks for [Creating Objects with JNI](ObjectCreationBenchmarks.md) 13 | [(results)](ObjectCreationBenchmarks.md#object-creation-results). 14 | 15 | 2. [com.evolvedbinary.jnibench.common.array](tree/main/src/main/java/com/evolvedbinary/jnibench/common/array) - 16 | Benchmarks for [Passing Arrays with JNI](ArrayPassingBenchmarks.md) 17 | [(results)](ArrayPassingBenchmarks.md#array-passing-results). 18 | 19 | 3. [com.evolvedbinary.jnibench.common.bytearray](tree/main/src/main/java/com/evolvedbinary/jnibench/common/bytearray), and [com.evolvedbinary.jnibench.common.getputjni](tree/main/src/main/java/com/evolvedbinary/jnibench/common/getputjni) - Benchmarks for [JNI Data Transfer](DataBenchmarks.md). 20 | 21 | # Reproducing 22 | 23 | If you want to run the code yourself, you need to have Java 8, Maven 3, and a 24 | C++ compiler that supports the C++ 11 standard. You can then simply run: 25 | 26 | ```bash 27 | $ mvn clean compile package 28 | ``` 29 | 30 | In the `target/` sub-directory, you will then find both a 31 | `jni-benchmarks-1.0.0-SNAPSHOT-application` folder and a 32 | `jni-benchmarks-1.0.0-SNAPSHOT-application.zip` file, you can use either of 33 | these. They both contain bash scripts in their `bin/` sub-folders for Mac, 34 | Linux, Unix and batch scripts for Windows. These scripts will run a single 35 | iteration of the benchmark. 36 | 37 | If you want to run multiple iterations and get a CSV file of the results, you 38 | can use `benchmark-100.sh` and/or `benchmark-100-with-close.sh`, or 39 | `array-benchmark-100.sh`. 40 | 41 | ## JMH support 42 | 43 | We have support for running the tests via JMH, see `jmh-benchmarks.sh`. You can 44 | also pass `--help` to the script to see JMH options. 45 | 46 | ### Byte array benchmarks 47 | 48 | There are two benchmarks, which are currently available only via JMH: 49 | ByteArrayFromNativeBenchmark and ByteArrayToNativeBenchmark. They can be run 50 | multiple times using `jmh-benchmarks-parametrized.sh` with: 51 | 52 | ```bash 53 | ./jmh-benchmarks-parametrized.sh -i 10 -b ByteArrayToNativeBenchmark -o results/ -f csv 54 | ``` 55 | 56 | The above command will run JMH with `ByteArrayToNativeBenchmark` benchmarks `10` times and store result in CSV files in `results` directory. You can also pass `--help` to the script to see additional JMH options that can be used. 57 | 58 | Results can then be plotted using the `process_byte_array_benchmarks_results.py` script. For example, to produce results for the `ByteArrayToNativeBenchmark` benchmarks, you can run: 59 | ```bash 60 | python3 process_byte_array_benchmarks_results.py -p results/ --param-name "Param: keySize" --chart-title "Performance comparison of passing byte array with {} bytes via JNI" 61 | ``` 62 | 63 | Command line parameter `p` expects a path to the directory with the JMH result CSV files from running the benchmarks with `jmh-benchmarks-parametrized.sh`. 64 | The `{}` in the `chart-title` parameter will be replaced by the value from the `param-name` column. 65 | 66 | # Other Resources 67 | 1. Java Foreign Interface prototype and performance results in RocksJava (i.e. Panama) - https://rocksdb.org/blog/2024/02/20/foreign-function-interface.html 68 | 2. RocksJava API Performance Improvements - https://rocksdb.org/blog/2023/11/06/java-jni-benchmarks.html 69 | -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_allbig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_allbig.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_allsmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_allsmall.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_ba.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_ba.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_bb.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_crit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_crit.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_dir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_dir.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_getelems.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_getelems.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_ind.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_ind.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_nopoolbig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_nopoolbig.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_nopoolsmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_nopoolsmall.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_copyout_setreg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_copyout_setreg.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_allbig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_allbig.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_allsmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_allsmall.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_ba.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_ba.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_bb.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_crit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_crit.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_dir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_dir.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_getelems.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_getelems.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_ind.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_ind.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_nopoolbig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_nopoolbig.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_nopoolsmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_nopoolsmall.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/fig_1024_1_none_setreg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/get_benchmarks/fig_1024_1_none_setreg.png -------------------------------------------------------------------------------- /analysis/get_benchmarks/jmh_2021-05-31T18:04:48.379041.md: -------------------------------------------------------------------------------- 1 | ## JMH Run 2 | This JMH run was generated on 2021-05-31T18:04:48.379041 3 | #### Config 4 | The configuration was read from `/home/apaxton/jni-benchmarks/jmh_full_get.json` 5 | ```json 6 | { 7 | "benchmark": "GetJNIBenchmark", 8 | "jvmargs": [ 9 | "Xmx4G", 10 | "XX:ErrorFile=./results/hs_err_pid%p.log", 11 | "XX:+HeapDumpOnOutOfMemoryError" 12 | ], 13 | "params": { 14 | "valueSize": [ 15 | 10, 16 | 50, 17 | 512, 18 | 1024, 19 | 4096, 20 | 8192, 21 | 16384, 22 | 32768, 23 | 65536 24 | ], 25 | "cacheMB": [ 26 | 1 27 | ], 28 | "checksum": [ 29 | "none", 30 | "copyout" 31 | ] 32 | }, 33 | "options": { 34 | "batchsize": 1, 35 | "warmupiterations": 20, 36 | "warmuptime": "50ms", 37 | "iterations": 50, 38 | "time": "500ms" 39 | }, 40 | "result.path": "./results", 41 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 42 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 43 | } 44 | ``` 45 | #### Command 46 | The java command executed to run the tests 47 | ``` 48 | java -Xmx4G -XX:ErrorFile=./results/hs_err_pid%p.log -XX:+HeapDumpOnOutOfMemoryError -Djava.library.path=target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib -jar target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar GetJNIBenchmark -p valueSize=10,50,512,1024,4096,8192,16384,32768,65536 -p cacheMB=1 -p checksum=none,copyout -bs 1 -wi 20 -w 50ms -i 50 -r 500ms -rff results/jmh_2021-05-31T18:04:48.379041/jmh_2021-05-31T18:04:48.379041.csv 49 | ``` 50 | -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_allbig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_allbig.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_allsmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_allsmall.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_ba.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_ba.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_bb.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_crit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_crit.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_dir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_dir.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_getelems.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_getelems.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_ind.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_ind.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_nopoolbig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_nopoolbig.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_nopoolsmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_nopoolsmall.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_copyin_setreg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_copyin_setreg.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_allbig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_allbig.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_allsmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_allsmall.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_ba.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_ba.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_bb.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_crit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_crit.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_dir.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_dir.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_getelems.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_getelems.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_ind.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_ind.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_nopoolbig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_nopoolbig.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_nopoolsmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_nopoolsmall.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/fig_1024_1_17_none_setreg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/put_benchmarks/fig_1024_1_17_none_setreg.png -------------------------------------------------------------------------------- /analysis/put_benchmarks/jmh_2021-05-28T11:40:43.503533.md: -------------------------------------------------------------------------------- 1 | ## JMH Run 2 | This JMH run was generated on 2021-05-28T11:40:43.503533 3 | #### Config 4 | The configuration was read from `/home/apaxton/jni-benchmarks/jmh_small_put.json` 5 | ```json 6 | { 7 | "benchmark": "PutJNIBenchmark", 8 | "jvmargs": [ 9 | "Xmx4G", 10 | "XX:ErrorFile=./results/hs_err_pid%p.log", 11 | "XX:+HeapDumpOnOutOfMemoryError" 12 | ], 13 | "params": { 14 | "valueSize": [ 15 | 10, 16 | 50, 17 | 512, 18 | 1024, 19 | 4096, 20 | 8192, 21 | 16384, 22 | 32768, 23 | 65536 24 | ], 25 | "cacheMB": [ 26 | 1 27 | ], 28 | "checksum": [ 29 | "none", 30 | "copyin" 31 | ] 32 | }, 33 | "options": { 34 | "batchsize": 1, 35 | "warmupiterations": 10, 36 | "warmuptime": "20ms", 37 | "iterations": 20, 38 | "time": "200ms" 39 | }, 40 | "result.path": "./results", 41 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 42 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 43 | } 44 | ``` 45 | #### Command 46 | The java command executed to run the tests 47 | ``` 48 | java -Xmx4G -XX:ErrorFile=./results/hs_err_pid%p.log -XX:+HeapDumpOnOutOfMemoryError -Djava.library.path=target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib -jar target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar PutJNIBenchmark -p valueSize=10,50,512,1024,4096,8192,16384,32768,65536 -p cacheMB=1 -p checksum=none,copyin -bs 1 -wi 10 -w 20ms -i 20 -r 200ms -rff results/jmh_2021-05-28T11:40:43.503533/jmh_2021-05-28T11:40:43.503533.csv 49 | ``` 50 | -------------------------------------------------------------------------------- /analysis/run3/fig_cacheEntryOverhead_cacheMB_checksum_1024_16_copyout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/run3/fig_cacheEntryOverhead_cacheMB_checksum_1024_16_copyout.png -------------------------------------------------------------------------------- /analysis/run3/fig_cacheEntryOverhead_cacheMB_checksum_1024_16_none.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/run3/fig_cacheEntryOverhead_cacheMB_checksum_1024_16_none.png -------------------------------------------------------------------------------- /analysis/run3/fig_cacheEntryOverhead_cacheMB_checksum_1024_4_copyout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/run3/fig_cacheEntryOverhead_cacheMB_checksum_1024_4_copyout.png -------------------------------------------------------------------------------- /analysis/run3/fig_cacheEntryOverhead_cacheMB_checksum_1024_4_none.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/run3/fig_cacheEntryOverhead_cacheMB_checksum_1024_4_none.png -------------------------------------------------------------------------------- /analysis/run3/jmh_2021-05-21T12:05:44.202319.md: -------------------------------------------------------------------------------- 1 | ## JMH Run 2 | This JMH run was generated on 2021-05-21T12:05:44.202319 3 | #### Config 4 | The configuration was read from `/Users/alan/swProjects/evolvedBinary/jni-benchmarks/jmh_huge.json` 5 | ```json 6 | { 7 | "benchmark": "GetJNIBenchmark", 8 | "jvmargs": {}, 9 | "params": { 10 | "valueSize": [ 11 | 10, 12 | 50, 13 | 512, 14 | 1024, 15 | 4096, 16 | 8192, 17 | 16384, 18 | 32768, 19 | 65536, 20 | 131072 21 | ], 22 | "cacheMB": [ 23 | 4, 24 | 16 25 | ], 26 | "checksum": [ 27 | "none", 28 | "copyout" 29 | ] 30 | }, 31 | "options": { 32 | "batchsize": 2, 33 | "warmupiterations": 125, 34 | "warmuptime": "1250ns", 35 | "iterations": 500, 36 | "time": "2000ns" 37 | }, 38 | "result.path": "./results", 39 | "java.library.path": "target/jni-benchmarks-1.0.0-SNAPSHOT-application/jni-benchmarks-1.0.0-SNAPSHOT/lib", 40 | "jar": "target/jni-benchmarks-1.0.0-SNAPSHOT-benchmarks.nar" 41 | } 42 | ``` 43 | #### Command 44 | The java command executed to run the tests 45 | ``` 46 | java -Djava.library.path=target/jni-benchmarks-1.0.0-SNAPSHOT-application/jni-benchmarks-1.0.0-SNAPSHOT/lib -jar target/jni-benchmarks-1.0.0-SNAPSHOT-benchmarks.nar GetJNIBenchmark -p valueSize=10,50,512,1024,4096,8192,16384,32768,65536,131072 -p cacheMB=4,16 -p checksum=none,copyout -bs 2 -wi 125 -w 1250ns -i 500 -r 2000ns -rff results/jmh_2021-05-21T12:05:44.202319.csv 47 | ``` 48 | -------------------------------------------------------------------------------- /analysis/run6dv/fig_2021-05-24T16:05:47.731360_1024_1_copyout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/run6dv/fig_2021-05-24T16:05:47.731360_1024_1_copyout.png -------------------------------------------------------------------------------- /analysis/run6dv/fig_2021-05-24T16:05:47.731360_1024_1_none.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/run6dv/fig_2021-05-24T16:05:47.731360_1024_1_none.png -------------------------------------------------------------------------------- /analysis/run6dv/fig_cacheEntryOverhead_cacheMB_checksum_1024_1_copyout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/run6dv/fig_cacheEntryOverhead_cacheMB_checksum_1024_1_copyout.png -------------------------------------------------------------------------------- /analysis/run6dv/fig_cacheEntryOverhead_cacheMB_checksum_1024_1_none.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/analysis/run6dv/fig_cacheEntryOverhead_cacheMB_checksum_1024_1_none.png -------------------------------------------------------------------------------- /analysis/run6dv/jmh_2021-05-24T13:29:03.036836.md: -------------------------------------------------------------------------------- 1 | ## JMH Run 2 | This JMH run was generated on 2021-05-24T13:29:03.036836 3 | #### Config 4 | The configuration was read from `/home/apaxton/jni-benchmarks/jmh_full_run.json` 5 | ```json 6 | { 7 | "benchmark": "GetJNIUnsafeBenchmark", 8 | "jvmargs": [ 9 | "Xmx24G" 10 | ], 11 | "params": { 12 | "valueSize": [ 13 | 10, 14 | 50, 15 | 512, 16 | 1024, 17 | 4096, 18 | 8192, 19 | 16384, 20 | 32768, 21 | 65536, 22 | 131072 23 | ], 24 | "cacheMB": [ 25 | 1 26 | ], 27 | "checksum": [ 28 | "none", 29 | "copyout" 30 | ] 31 | }, 32 | "options": { 33 | "batchsize": 2, 34 | "warmupiterations": 125, 35 | "warmuptime": "1250ns", 36 | "iterations": 2000, 37 | "time": "2000ns" 38 | }, 39 | "result.path": "./results", 40 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 41 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 42 | } 43 | ``` 44 | #### Command 45 | The java command executed to run the tests 46 | ``` 47 | java -Xmx24G -Djava.library.path=target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib -jar target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar GetJNIUnsafeBenchmark -p valueSize=10,50,512,1024,4096,8192,16384,32768,65536,131072 -p cacheMB=1 -p checksum=none,copyout -bs 2 -wi 125 -w 1250ns -i 2000 -r 2000ns -rff results/jmh_2021-05-24T13:29:03.036836.csv 48 | ``` 49 | -------------------------------------------------------------------------------- /array-benchmark-100.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "AllocateInJavaGetArray,AllocateInJavaGetMutableArray,AllocateInJavaGet2DArray,AllocateInCppGetArray,AllocateInCppGet2DArray,AllocateInCppGet2DArrayListWrapper,AllocateInJavaGetArrayList,AllocateInCppGetArrayList" > array-benchmark.csv 3 | for i in `seq 1 100`; 4 | do 5 | echo -en "\rArrayBenchmark run $i/100" 6 | target/jni-construction-benchmark-1.0-SNAPSHOT-application/jni-construction-benchmark-1.0-SNAPSHOT/bin/benchmark --benchmark=ArrayBenchmark --csv --iterations=1000000 >> array-benchmark.csv 7 | done 8 | -------------------------------------------------------------------------------- /benchmark-100-with-close.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "FooByCall,FooByCallStatic,FooByCallInvoke,FooByCallFinal,FooByCallStaticFinal,FooByCallInvokeFinal" > call-benchmark-with-close.csv 3 | for i in `seq 1 100`; 4 | do 5 | echo -en "\rCallBenchmark run $i/100" 6 | target/jni-construction-benchmark-1.0-SNAPSHOT-application/jni-construction-benchmark-1.0-SNAPSHOT/bin/benchmark --csv --close --iterations=1000000 >> call-benchmark-with-close.csv 7 | done 8 | -------------------------------------------------------------------------------- /benchmark-100.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "FooByCall,FooByCallStatic,FooByCallInvoke,FooByCallFinal,FooByCallStaticFinal,FooByCallInvokeFinal" > call-benchmark.csv 3 | for i in `seq 1 100`; 4 | do 5 | echo -en "\rCallBenchmark run $i/100" 6 | target/jni-construction-benchmark-1.0-SNAPSHOT-application/jni-construction-benchmark-1.0-SNAPSHOT/bin/benchmark --benchmark=CallBenchmark --csv --iterations=1000000 >> call-benchmark.csv 7 | done -------------------------------------------------------------------------------- /benchmark-results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/benchmark-results.png -------------------------------------------------------------------------------- /jmh-benchmarks-parametrized.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | 4 | # Default argument values 5 | POSITIONAL=() 6 | ITERATIONS=1 7 | BENCHMARK=ByteArrayFromNativeBenchmark 8 | OUTPUT_DIRECTORY=. 9 | FILE_FORMAT=csv 10 | 11 | while [[ $# -gt 0 ]] 12 | do 13 | key="$1" 14 | 15 | case $key in 16 | -i|--iterations) 17 | ITERATIONS="$2" 18 | shift 19 | shift 20 | ;; 21 | -b|--benchmark) 22 | BENCHMARK="$2" 23 | shift 24 | shift 25 | ;; 26 | -o|--output) 27 | OUTPUT_DIRECTORY="$2" 28 | shift 29 | shift 30 | ;; 31 | -f|--format) 32 | FILE_FORMAT="$2" 33 | shift 34 | shift 35 | ;; 36 | *) 37 | POSITIONAL+=("$1") 38 | shift 39 | ;; 40 | esac 41 | done 42 | set -- "${POSITIONAL[@]}" 43 | 44 | for i in `seq 1 $ITERATIONS`; 45 | do 46 | echo -en "\rBenchmark run $i/$ITERATIONS" 47 | TIMESTAMP=$(date +"%s") 48 | java -Djava.library.path=target/jni-benchmarks-1.0.0-SNAPSHOT-application/jni-benchmarks-1.0.0-SNAPSHOT/lib -jar target/jni-benchmarks-1.0.0-SNAPSHOT-benchmarks.nar ${BENCHMARK} -rff ${OUTPUT_DIRECTORY}/${BENCHMARK}_${TIMESTAMP}.${FILE_FORMAT} ${POSITIONAL} 49 | done; 50 | -------------------------------------------------------------------------------- /jmh-benchmarks.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | java -Djava.library.path=target/jni-benchmarks-1.0.0-SNAPSHOT-application/jni-benchmarks-1.0.0-SNAPSHOT/lib -jar target/jni-benchmarks-1.0.0-SNAPSHOT-benchmarks.nar $@ -rf csv 4 | -------------------------------------------------------------------------------- /jmh_full_get.json: -------------------------------------------------------------------------------- 1 | { 2 | "benchmark": "GetJNIBenchmark", 3 | "jvmargs": ["Xmx4G", "XX:ErrorFile=./results/hs_err_pid%p.log", "XX:+HeapDumpOnOutOfMemoryError"], 4 | "params": { 5 | "valueSize": [10, 50, 512, 1024, 4096, 8192, 16384, 32768, 65536], 6 | "cacheMB": [1], 7 | "checksum": ["none", "copyout"] 8 | }, 9 | "options": { 10 | "batchsize": 1, 11 | "warmupiterations": 20, 12 | "warmuptime": "50ms", 13 | "iterations": 50, 14 | "time": "500ms" 15 | }, 16 | "result.path": "./results", 17 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 18 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 19 | } -------------------------------------------------------------------------------- /jmh_full_put.json: -------------------------------------------------------------------------------- 1 | { 2 | "benchmark": "PutJNIBenchmark", 3 | "jvmargs": ["Xmx4G", "XX:ErrorFile=./results/hs_err_pid%p.log", "XX:+HeapDumpOnOutOfMemoryError"], 4 | "params": { 5 | "valueSize": [10, 50, 512, 1024, 4096, 8192, 16384, 32768, 65536], 6 | "cacheMB": [1], 7 | "checksum": ["none", "copyin"] 8 | }, 9 | "options": { 10 | "batchsize": 1, 11 | "warmupiterations": 20, 12 | "warmuptime": "50ms", 13 | "iterations": 50, 14 | "time": "500ms" 15 | }, 16 | "result.path": "./results", 17 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 18 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 19 | } -------------------------------------------------------------------------------- /jmh_plot.json: -------------------------------------------------------------------------------- 1 | { 2 | "plots": [ 3 | { 4 | "xaxisparam": { 5 | "name": "valueSize", 6 | "min": 1024 7 | }, 8 | "label": "allbig" 9 | }, 10 | { 11 | "xaxisparam": { 12 | "name": "valueSize", 13 | "min": 1, 14 | "max": 4096 15 | }, 16 | "label": "allsmall" 17 | # defaults to include_patterns of all matching 18 | }, 19 | { 20 | "xaxisparam": { 21 | "name": "valueSize", 22 | "min": 1, 23 | "max": 4096 24 | }, 25 | "exclude_patterns": ["Pooled"], 26 | "label": "nopoolsmall" 27 | }, 28 | { 29 | "xaxisparam": { 30 | "name": "valueSize", 31 | "min": 1024 32 | }, 33 | "exclude_patterns": ["Pooled"], 34 | "label": "nopoolbig" 35 | }, 36 | { 37 | "xaxisparam": { 38 | "name": "valueSize" 39 | }, 40 | "include_patterns": ["ByteArray"], 41 | "label": "ba" 42 | }, 43 | { 44 | "xaxisparam": { 45 | "name": "valueSize" 46 | }, 47 | "include_patterns": ["ByteBuf"], 48 | "label": "bb" 49 | }, 50 | { 51 | "xaxisparam": { 52 | "name": "valueSize" 53 | }, 54 | "include_patterns": ["Direct"], 55 | "label": "dir" 56 | }, 57 | { 58 | "xaxisparam": { 59 | "name": "valueSize" 60 | }, 61 | "include_patterns": ["Indirect"], 62 | "label": "ind" 63 | }, 64 | { 65 | "xaxisparam": { 66 | "name": "valueSize" 67 | }, 68 | "include_patterns": ["Critical"], 69 | "label": "crit" 70 | }, 71 | { 72 | "xaxisparam": { 73 | "name": "valueSize" 74 | }, 75 | "include_patterns": ["GetElements"], 76 | "label": "getelems" 77 | }, 78 | { 79 | "xaxisparam": { 80 | "name": "valueSize" 81 | }, 82 | "include_patterns": ["SetRegion"], 83 | "label": "setreg" 84 | } 85 | ], 86 | "result.path": "./analysis/testplots" 87 | } -------------------------------------------------------------------------------- /jmh_run.json: -------------------------------------------------------------------------------- 1 | { 2 | "help": false, 3 | "benchmark": "GetJNIBenchmark", 4 | "jvmargs": ["Xmx24G", "XX:+HeapDumpOnOutOfMemoryError"], 5 | "params": { 6 | "valueSize": [50, 4096], 7 | "cacheMB": [1], 8 | "checksum": ["none"] 9 | }, 10 | # "flags": ["lprof"], 11 | "options": { 12 | "batchsize": 2, 13 | "warmupiterations": 1, 14 | "warmuptime": "10us", 15 | "iterations": 1, 16 | "time": "10us" 17 | }, 18 | "result.path": "./results", 19 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 20 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 21 | } -------------------------------------------------------------------------------- /jmh_small_get.json: -------------------------------------------------------------------------------- 1 | { 2 | "benchmark": "GetJNIBenchmark", 3 | "jvmargs": ["Xmx4G", "XX:ErrorFile=./results/hs_err_pid%p.log", "XX:+HeapDumpOnOutOfMemoryError"], 4 | "params": { 5 | "valueSize": [10, 50, 512, 1024, 4096, 8192, 16384, 32768, 65536], 6 | "cacheMB": [1], 7 | "checksum": ["none", "copyout"] 8 | }, 9 | "options": { 10 | "batchsize": 1, 11 | "warmupiterations": 10, 12 | "warmuptime": "20ms", 13 | "iterations": 20, 14 | "time": "200ms" 15 | }, 16 | "result.path": "./results", 17 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 18 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 19 | } -------------------------------------------------------------------------------- /jmh_small_put.json: -------------------------------------------------------------------------------- 1 | { 2 | "benchmark": "PutJNIBenchmark", 3 | "jvmargs": ["Xmx4G", "XX:ErrorFile=./results/hs_err_pid%p.log", "XX:+HeapDumpOnOutOfMemoryError"], 4 | "params": { 5 | "valueSize": [10, 50, 512, 1024, 4096, 8192, 16384, 32768, 65536], 6 | "cacheMB": [1], 7 | "checksum": ["none", "copyin"] 8 | }, 9 | "options": { 10 | "batchsize": 1, 11 | "warmupiterations": 5, 12 | "warmuptime": "20ms", 13 | "iterations": 10, 14 | "time": "100ms" 15 | }, 16 | "result.path": "./results", 17 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 18 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 19 | } -------------------------------------------------------------------------------- /jmh_tiny_get.json: -------------------------------------------------------------------------------- 1 | { 2 | "benchmark": "GetJNIBenchmark", 3 | "jvmargs": ["Xmx4G", "XX:ErrorFile=./results/hs_err_pid%p.log", "XX:+HeapDumpOnOutOfMemoryError"], 4 | "params": { 5 | "valueSize": [50, 1024, 4096, 16384], 6 | "cacheMB": [1], 7 | "checksum": ["none", "copyout"] 8 | }, 9 | "options": { 10 | "batchsize": 1, 11 | "warmupiterations": 5, 12 | "warmuptime": "10ms", 13 | "iterations": 5, 14 | "time": "50ms" 15 | }, 16 | "result.path": "./results", 17 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 18 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 19 | } -------------------------------------------------------------------------------- /jmh_tiny_put.json: -------------------------------------------------------------------------------- 1 | { 2 | "benchmark": "PutJNIBenchmark", 3 | "jvmargs": ["Xmx4G", "XX:ErrorFile=./results/hs_err_pid%p.log", "XX:+HeapDumpOnOutOfMemoryError"], 4 | "params": { 5 | "valueSize": [50, 1024, 4096, 16384], 6 | "cacheMB": [1], 7 | "checksum": ["none", "copyin"] 8 | }, 9 | "options": { 10 | "batchsize": 1, 11 | "warmupiterations": 5, 12 | "warmuptime": "10ms", 13 | "iterations": 5, 14 | "time": "50ms" 15 | }, 16 | "result.path": "./results", 17 | "java.library.path": "target/jni-benchmarks-1.0.1-SNAPSHOT-application/jni-benchmarks-1.0.1-SNAPSHOT/lib", 18 | "jar": "target/jni-benchmarks-1.0.1-SNAPSHOT-benchmarks.nar" 19 | } -------------------------------------------------------------------------------- /jni-arrays-size-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/jni-arrays-size-2.png -------------------------------------------------------------------------------- /jni-arrays-size-20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evolvedbinary/jni-benchmarks/5894a008ac1bce19506f2c05284784fd8dab8a59/jni-arrays-size-20.png -------------------------------------------------------------------------------- /src/main/assembly/appassembler-output.xml: -------------------------------------------------------------------------------- 1 | 4 | application 5 | 6 | dir 7 | zip 8 | 9 | 10 | true 11 | 12 | 13 | 14 | 15 | 16 | ${project.build.directory}/appassembler 17 | 18 | bin/${project.artifactId} 19 | 20 | 755 21 | / 22 | 23 | 24 | 25 | 26 | ${project.build.directory}/appassembler/bin 27 | 28 | bin/${project.artifactId} 29 | 30 | /bin 31 | 32 | 33 | 34 | 35 | ${project.build.directory}/appassembler 36 | 37 | bin 38 | 39 | / 40 | 41 | 42 | 43 | ${project.build.directory}/nar/${project.artifactId}-${project.version}-${nar.aol}-jni/lib/${nar.aol}/jni 44 | 45 | *.jnilib 46 | *.dylib 47 | *.so 48 | *.dll 49 | 50 | /lib 51 | 52 | 53 | 54 | 55 | 56 | 57 | LICENSE 58 | / 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /src/main/c++/array/AllocateInCppGet2DArray.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "com_evolvedbinary_jnibench_common_array_AllocateInCppGet2DArray.h" 31 | #include "FooObject.h" 32 | #include "Portal.h" 33 | 34 | /* 35 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInCppGet2DArray 36 | * Method: get2DArray 37 | * Signature: (J)[[Ljava/lang/Object; 38 | */ 39 | jobjectArray Java_com_evolvedbinary_jnibench_common_array_AllocateInCppGet2DArray_get2DArray( 40 | JNIEnv *env, jclass, jlong handle) { 41 | const auto& cpp_array = *reinterpret_cast*>(handle); 42 | jsize len = static_cast(cpp_array.size()); 43 | 44 | const jclass jstring_clazz = StringJni::getJClass(env); 45 | if (jstring_clazz == nullptr) { 46 | // exception occurred accessing class 47 | return nullptr; 48 | } 49 | 50 | const jclass jlong_clazz = LongJni::getJClass(env); 51 | if (jlong_clazz == nullptr) { 52 | // exception occurred accessing class 53 | return nullptr; 54 | } 55 | 56 | jobjectArray jname_array = env->NewObjectArray(len, jstring_clazz, nullptr); 57 | if (jname_array == nullptr) { 58 | // exception thrown: OutOfMemoryError 59 | return nullptr; 60 | } 61 | jobjectArray jvalue_array = env->NewObjectArray(len, jlong_clazz, nullptr); 62 | if (jvalue_array == nullptr) { 63 | // exception thrown: OutOfMemoryError 64 | env->DeleteLocalRef(jname_array); 65 | return nullptr; 66 | } 67 | 68 | for (size_t i = 0; i < cpp_array.size(); ++i) { 69 | const jnibench::FooObject& foo_obj = cpp_array[i]; 70 | jstring jname = env->NewStringUTF(foo_obj.GetName().c_str()); 71 | if (env->ExceptionCheck()) { 72 | if (jname != nullptr) { 73 | env->DeleteLocalRef(jname_array); 74 | env->DeleteLocalRef(jvalue_array); 75 | env->DeleteLocalRef(jname); 76 | } 77 | return nullptr; 78 | } 79 | 80 | jobject jvalue = LongJni::construct(env, jlong_clazz, foo_obj.GetValue()); 81 | if (jvalue == nullptr) { 82 | env->DeleteLocalRef(jname_array); 83 | env->DeleteLocalRef(jvalue_array); 84 | env->DeleteLocalRef(jname); 85 | return nullptr; 86 | } 87 | 88 | env->SetObjectArrayElement(jname_array, static_cast(i), jname); 89 | if (env->ExceptionCheck()) { 90 | // exception thrown: ArrayIndexOutOfBoundsException 91 | // or ArrayStoreException 92 | env->DeleteLocalRef(jname_array); 93 | env->DeleteLocalRef(jvalue_array); 94 | env->DeleteLocalRef(jname); 95 | env->DeleteLocalRef(jvalue); 96 | return nullptr; 97 | } 98 | env->SetObjectArrayElement(jvalue_array, static_cast(i), jvalue); 99 | if (env->ExceptionCheck()) { 100 | // exception thrown: ArrayIndexOutOfBoundsException 101 | // or ArrayStoreException 102 | env->DeleteLocalRef(jname_array); 103 | env->DeleteLocalRef(jvalue_array); 104 | env->DeleteLocalRef(jname); 105 | env->DeleteLocalRef(jvalue); 106 | return nullptr; 107 | } 108 | 109 | env->DeleteLocalRef(jname); 110 | env->DeleteLocalRef(jvalue); 111 | } 112 | 113 | jobjectArray jobj_array = env->NewObjectArray(2, env->FindClass("java/lang/Object"), nullptr); 114 | if (jobj_array == nullptr) { 115 | // exception thrown: OutOfMemoryError 116 | env->DeleteLocalRef(jname_array); 117 | env->DeleteLocalRef(jvalue_array); 118 | return nullptr; 119 | } 120 | 121 | env->SetObjectArrayElement(jobj_array, 0, jname_array); 122 | if (env->ExceptionCheck()) { 123 | // exception thrown: ArrayIndexOutOfBoundsException 124 | // or ArrayStoreException 125 | env->DeleteLocalRef(jname_array); 126 | env->DeleteLocalRef(jvalue_array); 127 | env->DeleteLocalRef(jobj_array); 128 | return nullptr; 129 | } 130 | env->SetObjectArrayElement(jobj_array, 1, jvalue_array); 131 | if (env->ExceptionCheck()) { 132 | // exception thrown: ArrayIndexOutOfBoundsException 133 | // or ArrayStoreException 134 | env->DeleteLocalRef(jname_array); 135 | env->DeleteLocalRef(jvalue_array); 136 | env->DeleteLocalRef(jobj_array); 137 | return nullptr; 138 | } 139 | 140 | return jobj_array; 141 | } -------------------------------------------------------------------------------- /src/main/c++/array/AllocateInCppGetArray.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "com_evolvedbinary_jnibench_common_array_AllocateInCppGetArray.h" 31 | #include "FooObject.h" 32 | #include "Portal.h" 33 | 34 | /* 35 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInCppGetArray 36 | * Method: getArray 37 | * Signature: (J)[Lcom/evolvedbinary/jnibench/common/array/FooObject; 38 | */ 39 | jobjectArray Java_com_evolvedbinary_jnibench_common_array_AllocateInCppGetArray_getArray( 40 | JNIEnv *env, jclass, jlong handle) { 41 | const auto& cpp_array = *reinterpret_cast*>(handle); 42 | jsize length = static_cast(cpp_array.size()); 43 | 44 | const jclass jfoo_obj_clazz = FooObjectJni::getJClass(env); 45 | if (jfoo_obj_clazz == nullptr) { 46 | // exception occurred accessing class 47 | return nullptr; 48 | } 49 | 50 | jobjectArray java_array = env->NewObjectArray(length, jfoo_obj_clazz, nullptr); 51 | if (java_array == nullptr) { 52 | // exception thrown: OutOfMemoryError 53 | return nullptr; 54 | } 55 | 56 | for (size_t i = 0; i < cpp_array.size(); ++i) { 57 | const jnibench::FooObject& foo_obj = cpp_array[i]; 58 | jobject jfoo_obj = FooObjectJni::construct(env, jfoo_obj_clazz, foo_obj); 59 | if (jfoo_obj == nullptr) { 60 | // exception occurred 61 | env->DeleteLocalRef(java_array); 62 | return nullptr; 63 | } 64 | env->SetObjectArrayElement(java_array, static_cast(i), jfoo_obj); 65 | if (env->ExceptionCheck()) { 66 | // exception thrown: ArrayIndexOutOfBoundsException 67 | // or ArrayStoreException 68 | env->DeleteLocalRef(jfoo_obj); 69 | env->DeleteLocalRef(java_array); 70 | return nullptr; 71 | } 72 | 73 | env->DeleteLocalRef(jfoo_obj); 74 | } 75 | return java_array; 76 | } -------------------------------------------------------------------------------- /src/main/c++/array/AllocateInCppGetArrayList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "com_evolvedbinary_jnibench_common_array_AllocateInCppGetArrayList.h" 31 | #include "FooObject.h" 32 | #include "Portal.h" 33 | 34 | /* 35 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInCppGetArrayList 36 | * Method: getArrayList 37 | * Signature: (J)Ljava/util/List; 38 | */ 39 | jobject Java_com_evolvedbinary_jnibench_common_array_AllocateInCppGetArrayList_getArrayList( 40 | JNIEnv *env, jclass, jlong handle) { 41 | 42 | const jclass jfoo_obj_clazz = FooObjectJni::getJClass(env); 43 | if (jfoo_obj_clazz == nullptr) { 44 | // exception occurred accessing class 45 | return nullptr; 46 | } 47 | 48 | const jclass clazz_array_list = ListJni::getArrayListClass(env); 49 | const jmethodID ctor_array_list = ListJni::getArrayListConstructorMethodId(env); 50 | if (ctor_array_list == nullptr) { 51 | // exception occurred accessing method 52 | return nullptr; 53 | } 54 | 55 | const jmethodID add_mid = ListJni::getListAddMethodId(env); 56 | if (add_mid == nullptr) { 57 | // exception occurred accessing method 58 | return nullptr; 59 | } 60 | 61 | const auto& cpp_array = *reinterpret_cast*>(handle); 62 | const jsize len = static_cast(cpp_array.size()); 63 | 64 | // create new java.util.ArrayList 65 | const jobject jlist = env->NewObject(clazz_array_list, ctor_array_list, 66 | static_cast(len)); 67 | if (env->ExceptionCheck()) { 68 | // exception occurred constructing object 69 | if (jlist != nullptr) { 70 | env->DeleteLocalRef(jlist); 71 | } 72 | return nullptr; 73 | } 74 | 75 | for (auto foo_obj : cpp_array) { 76 | // create java FooObject 77 | const jobject jfoo_obj = FooObjectJni::construct(env, jfoo_obj_clazz, foo_obj); 78 | if (jfoo_obj == nullptr) { 79 | // exception occurred constructing object 80 | return nullptr; 81 | } 82 | 83 | // add to list 84 | const jboolean rs = env->CallBooleanMethod(jlist, add_mid, jfoo_obj); 85 | if (env->ExceptionCheck() || rs == JNI_FALSE) { 86 | // exception occurred calling method, or could not add 87 | env->DeleteLocalRef(jlist); 88 | env->DeleteLocalRef(jfoo_obj); 89 | return nullptr; 90 | } 91 | } 92 | 93 | return jlist; 94 | } -------------------------------------------------------------------------------- /src/main/c++/array/AllocateInJavaGet2DArray.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "com_evolvedbinary_jnibench_common_array_AllocateInJavaGet2DArray.h" 31 | #include "FooObject.h" 32 | 33 | /* 34 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInJavaGet2DArray 35 | * Method: getArraySize 36 | * Signature: (J)J 37 | */ 38 | jlong Java_com_evolvedbinary_jnibench_common_array_AllocateInJavaGet2DArray_getArraySize( 39 | JNIEnv *, jclass, jlong handle) { 40 | const auto& cpp_array = *reinterpret_cast*>(handle); 41 | return static_cast(cpp_array.size()); 42 | } 43 | 44 | /* 45 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInJavaGet2DArray 46 | * Method: getArrays 47 | * Signature: (J[Ljava/lang/String;[J)V 48 | */ 49 | void Java_com_evolvedbinary_jnibench_common_array_AllocateInJavaGet2DArray_getArrays( 50 | JNIEnv *env, jclass, jlong handle, jobjectArray name_array, jlongArray value_array) { 51 | jlong* value_array_ptr = env->GetLongArrayElements(value_array, nullptr); 52 | if (value_array_ptr == nullptr) { 53 | // exception thrown: OutOfMemoryError 54 | return; 55 | } 56 | 57 | auto* cpp_array = reinterpret_cast*>(handle); 58 | for (jsize i = 0; i < env->GetArrayLength(name_array); i++) { 59 | jnibench::FooObject foo_obj = (*cpp_array)[static_cast(i)]; 60 | 61 | jstring jname = env->NewStringUTF(foo_obj.GetName().c_str()); 62 | if (jname == nullptr) { 63 | // exception thrown: OutOfMemoryError 64 | env->ReleaseLongArrayElements(value_array, value_array_ptr, JNI_ABORT); 65 | return; 66 | } 67 | env->SetObjectArrayElement(name_array, i, jname); 68 | if (env->ExceptionCheck()) { 69 | // exception thrown: ArrayIndexOutOfBoundsException 70 | env->DeleteLocalRef(jname); 71 | env->ReleaseLongArrayElements(value_array, value_array_ptr, JNI_ABORT); 72 | return; 73 | } 74 | 75 | env->DeleteLocalRef(jname); 76 | 77 | value_array_ptr[static_cast(i)] = static_cast(foo_obj.GetValue()); 78 | } 79 | 80 | env->ReleaseLongArrayElements(value_array, value_array_ptr, 0); 81 | } -------------------------------------------------------------------------------- /src/main/c++/array/AllocateInJavaGetArray.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArray.h" 31 | #include "FooObject.h" 32 | #include "Portal.h" 33 | 34 | /* 35 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArray 36 | * Method: getArraySize 37 | * Signature: (J)J 38 | */ 39 | jlong Java_com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArray_getArraySize( 40 | JNIEnv *, jclass, jlong handle) { 41 | const auto& cpp_array = *reinterpret_cast*>(handle); 42 | return static_cast(cpp_array.size()); 43 | } 44 | 45 | /* 46 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArray 47 | * Method: getArray 48 | * Signature: (J[Lcom/evolvedbinary/jnibench/common/array/FooObject;)V 49 | */ 50 | void Java_com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArray_getArray( 51 | JNIEnv *env, jclass, jlong handle, jobjectArray jobject_array) { 52 | const jclass jfoo_obj_clazz = FooObjectJni::getJClass(env); 53 | if (jfoo_obj_clazz == nullptr) { 54 | // exception occurred accessing class 55 | return; 56 | } 57 | 58 | auto* cpp_array = reinterpret_cast*>(handle); 59 | for (jsize i = 0; i < env->GetArrayLength(jobject_array); i++) { 60 | jnibench::FooObject foo_obj = (*cpp_array)[static_cast(i)]; 61 | 62 | jobject jfoo_obj = FooObjectJni::construct(env, jfoo_obj_clazz, foo_obj); 63 | if (jfoo_obj == nullptr) { 64 | // exception occurred 65 | return; 66 | } 67 | 68 | env->SetObjectArrayElement(jobject_array, i, jfoo_obj); 69 | if(env->ExceptionCheck()) { 70 | // exception thrown: ArrayIndexOutOfBoundsException 71 | // or ArrayStoreException 72 | env->DeleteLocalRef(jfoo_obj); 73 | return; 74 | } 75 | 76 | env->DeleteLocalRef(jfoo_obj); 77 | } 78 | } -------------------------------------------------------------------------------- /src/main/c++/array/AllocateInJavaGetArrayList.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArrayList.h" 31 | #include "FooObject.h" 32 | #include "Portal.h" 33 | 34 | /* 35 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArrayList 36 | * Method: getListSize 37 | * Signature: (J)J 38 | */ 39 | jlong Java_com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArrayList_getListSize( 40 | JNIEnv *, jclass, jlong handle) { 41 | const auto& cpp_array = *reinterpret_cast*>(handle); 42 | return static_cast(cpp_array.size()); 43 | } 44 | 45 | /* 46 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArrayList 47 | * Method: getArrays 48 | * Signature: (JLjava/util/List;)V 49 | */ 50 | void Java_com_evolvedbinary_jnibench_common_array_AllocateInJavaGetArrayList_getList( 51 | JNIEnv *env, jclass, jlong handle, jobject jlist) { 52 | 53 | const jclass jfoo_obj_clazz = FooObjectJni::getJClass(env); 54 | if (jfoo_obj_clazz == nullptr) { 55 | // exception occurred accessing class 56 | return; 57 | } 58 | 59 | const jmethodID add_mid = ListJni::getListAddMethodId(env); 60 | if (add_mid == nullptr) { 61 | // exception occurred accessing method 62 | return; 63 | } 64 | 65 | const auto& cpp_array = *reinterpret_cast*>(handle); 66 | for (auto foo_obj : cpp_array) { 67 | // create java FooObject 68 | const jobject jfoo_obj = FooObjectJni::construct(env, jfoo_obj_clazz, foo_obj); 69 | if (jfoo_obj == nullptr) { 70 | // exception occurred constructing object 71 | return; 72 | } 73 | 74 | // add to list 75 | const jboolean rs = env->CallBooleanMethod(jlist, add_mid, jfoo_obj); 76 | if (env->ExceptionCheck() || rs == JNI_FALSE) { 77 | // exception occurred calling method, or could not add 78 | env->DeleteLocalRef(jfoo_obj); 79 | return; 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /src/main/c++/array/AllocateInJavaGetMutableArray.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "com_evolvedbinary_jnibench_common_array_AllocateInJavaGetMutableArray.h" 31 | #include "FooObject.h" 32 | #include "Portal.h" 33 | 34 | /* 35 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInJavaGetMutableArray 36 | * Method: getArraySize 37 | * Signature: (J)J 38 | */ 39 | jlong Java_com_evolvedbinary_jnibench_common_array_AllocateInJavaGetMutableArray_getArraySize( 40 | JNIEnv *, jclass, jlong handle) { 41 | const auto& cpp_array = *reinterpret_cast*>(handle); 42 | return static_cast(cpp_array.size()); 43 | } 44 | 45 | /* 46 | * Class: com_evolvedbinary_jnibench_common_array_AllocateInJavaGetMutableArray 47 | * Method: getArray 48 | * Signature: (J[Lcom/evolvedbinary/jnibench/common/array/FooObject;)V 49 | */ 50 | void Java_com_evolvedbinary_jnibench_common_array_AllocateInJavaGetMutableArray_getArray( 51 | JNIEnv *env, jclass, jlong handle, jobjectArray jobject_array) { 52 | const jclass jfoo_obj_clazz = FooObjectJni::getJClass(env); 53 | if (jfoo_obj_clazz == nullptr) { 54 | // exception occurred accessing class 55 | return; 56 | } 57 | 58 | const jfieldID fid_name = FooObjectJni::getNameField(env, jfoo_obj_clazz); 59 | if (fid_name == nullptr) { 60 | // exception occurred accessing field 61 | return; 62 | } 63 | 64 | const jfieldID fid_value = FooObjectJni::getValueField(env, jfoo_obj_clazz); 65 | if (fid_value == nullptr) { 66 | // exception occurred accessing field 67 | return; 68 | } 69 | 70 | auto* cpp_array = reinterpret_cast*>(handle); 71 | for (jsize i = 0; i < env->GetArrayLength(jobject_array); i++) { 72 | jnibench::FooObject foo_obj = (*cpp_array)[static_cast(i)]; 73 | 74 | jobject jfoo_obj = env->GetObjectArrayElement(jobject_array, i); 75 | if(env->ExceptionCheck()) { 76 | // exception thrown: ArrayIndexOutOfBoundsException 77 | // or ArrayStoreException 78 | if (jfoo_obj != nullptr) { 79 | env->DeleteLocalRef(jfoo_obj); 80 | } 81 | return; 82 | } 83 | 84 | // set name field 85 | jstring jname = env->NewStringUTF(foo_obj.GetName().c_str()); 86 | if (env->ExceptionCheck()) { 87 | if (jname != nullptr) { 88 | env->DeleteLocalRef(jname); 89 | } 90 | env->DeleteLocalRef(jfoo_obj); 91 | return; 92 | } 93 | env->SetObjectField(jfoo_obj, fid_name, jname); 94 | if (env->ExceptionCheck()) { 95 | env->DeleteLocalRef(jname); 96 | env->DeleteLocalRef(jfoo_obj); 97 | return; 98 | } 99 | env->DeleteLocalRef(jname); 100 | 101 | // set value field 102 | env->SetLongField(jfoo_obj, fid_value, static_cast(foo_obj.GetValue())); 103 | if (env->ExceptionCheck()) { 104 | env->DeleteLocalRef(jfoo_obj); 105 | return; 106 | } 107 | 108 | env->DeleteLocalRef(jfoo_obj); 109 | } 110 | } -------------------------------------------------------------------------------- /src/main/c++/array/FooNativeObjectArray.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | #include "com_evolvedbinary_jnibench_common_array_FooNativeObjectArray.h" 31 | #include "FooObject.h" 32 | 33 | /* 34 | * Class: com_evolvedbinary_jnibench_common_array_FooNativeObjectArray 35 | * Method: newFooObjectArray 36 | * Signature: ()J 37 | */ 38 | jlong Java_com_evolvedbinary_jnibench_common_array_FooNativeObjectArray_newFooObjectArray 39 | (JNIEnv *env, jclass, jobjectArray object_array) { 40 | auto* cpp_array = new std::vector(); 41 | for (jsize i = 0; i < env->GetArrayLength(object_array); ++i) { 42 | jobject obj = env->GetObjectArrayElement(object_array, i); 43 | jclass obj_clazz = env->GetObjectClass(obj); 44 | jfieldID name_field_id = env->GetFieldID(obj_clazz, "name" , "Ljava/lang/String;"); 45 | jfieldID value_field_id = env->GetFieldID(obj_clazz, "value" , "J"); 46 | jstring jname = (jstring) env->GetObjectField(obj, name_field_id); 47 | const char *native_name = env->GetStringUTFChars(jname, nullptr); 48 | jlong jvalue = env->GetLongField(obj, value_field_id); 49 | cpp_array->push_back(jnibench::FooObject(std::string(native_name), static_cast(jvalue))); 50 | } 51 | return reinterpret_cast(cpp_array); 52 | } 53 | 54 | /* 55 | * Class: com_evolvedbinary_jnibench_common_array_FooNativeObjectArray 56 | * Method: disposeInternal 57 | * Signature: (J)V 58 | */ 59 | void Java_com_evolvedbinary_jnibench_common_array_FooNativeObjectArray_disposeInternal 60 | (JNIEnv *, jobject, jlong handle) { 61 | auto* cpp_array = reinterpret_cast*>(handle); 62 | delete cpp_array; 63 | } -------------------------------------------------------------------------------- /src/main/c++/array/FooObject.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | 29 | namespace jnibench { 30 | 31 | class FooObject { 32 | public: 33 | FooObject(const std::string& n, int64_t v) : name(n), value(v){} 34 | 35 | const std::string& GetName() const { return name; } 36 | int64_t GetValue() const { return value; } 37 | 38 | private: 39 | const std::string name; 40 | const int64_t value; 41 | }; 42 | 43 | } // namespace jnibench -------------------------------------------------------------------------------- /src/main/c++/bytearray/Portal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | 29 | #include 30 | 31 | extern jclass g_jbyte_buffer_clazz; 32 | extern jmethodID g_jbyte_buffer_array_mid; 33 | extern jmethodID g_jbyte_buffer_allocate_mid; 34 | 35 | inline jbyteArray StringToJavaByteArray(JNIEnv *env, const std::string& str) { 36 | const jsize jlen = static_cast(str.size()); 37 | jbyteArray jbytes = env->NewByteArray(jlen); 38 | if(jbytes == nullptr) { 39 | // exception thrown: OutOfMemoryError 40 | return nullptr; 41 | } 42 | 43 | env->SetByteArrayRegion(jbytes, 0, jlen, 44 | const_cast(reinterpret_cast(str.c_str()))); 45 | if(env->ExceptionCheck()) { 46 | // exception thrown: ArrayIndexOutOfBoundsException 47 | env->DeleteLocalRef(jbytes); 48 | return nullptr; 49 | } 50 | 51 | return jbytes; 52 | } 53 | 54 | inline void SetByteBufferData(JNIEnv* env, const jmethodID jarray_mid, const jobject& jbuf, 55 | const char* content, const size_t content_len) { 56 | jbyteArray jarray = static_cast(env->CallObjectMethod(jbuf, jarray_mid)); 57 | if (env->ExceptionCheck()) { 58 | // exception occurred 59 | env->DeleteLocalRef(jbuf); 60 | return; 61 | } 62 | 63 | jboolean is_copy = JNI_FALSE; 64 | jbyte* ja = reinterpret_cast( 65 | env->GetPrimitiveArrayCritical(jarray, &is_copy)); 66 | if (ja == nullptr) { 67 | // exception occurred 68 | env->DeleteLocalRef(jarray); 69 | env->DeleteLocalRef(jbuf); 70 | return; 71 | } 72 | 73 | memcpy(ja, const_cast(content), content_len); 74 | 75 | env->ReleasePrimitiveArrayCritical(jarray, ja, is_copy ? 0 : JNI_ABORT); 76 | 77 | env->DeleteLocalRef(jarray); 78 | } 79 | 80 | inline jobject NewByteBuffer(JNIEnv* env, const size_t capacity, const char* content) { 81 | 82 | const jobject jbuf = env->CallStaticObjectMethod( 83 | g_jbyte_buffer_clazz, g_jbyte_buffer_allocate_mid, static_cast(capacity)); 84 | if (env->ExceptionCheck()) { 85 | // exception occurred 86 | return nullptr; 87 | } 88 | 89 | // Set buffer data 90 | if (content != nullptr) { 91 | SetByteBufferData(env, g_jbyte_buffer_array_mid, jbuf, content, capacity); 92 | } 93 | 94 | return jbuf; 95 | } 96 | 97 | inline jobject NewDirectByteBuffer(JNIEnv* env, const size_t capacity, const char* content) { 98 | bool allocated = false; 99 | if (content == nullptr) { 100 | content = new char[capacity]; 101 | allocated = true; 102 | } 103 | jobject jbuf = env->NewDirectByteBuffer(const_cast(content), static_cast(capacity)); 104 | if (jbuf == nullptr) { 105 | // exception occurred 106 | if (allocated) { 107 | delete[] static_cast(content); 108 | } 109 | return nullptr; 110 | } 111 | return jbuf; 112 | } -------------------------------------------------------------------------------- /src/main/c++/call/Foo.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include "Foo.h" 28 | 29 | namespace jnibench { 30 | Foo::Foo() { 31 | } 32 | 33 | Foo::~Foo() { 34 | } 35 | } //end namespace jnibench -------------------------------------------------------------------------------- /src/main/c++/call/Foo.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #ifndef FOO_H_ 28 | #define FOO_H_ 29 | 30 | namespace jnibench { 31 | class Foo { 32 | public: 33 | Foo(); 34 | ~Foo(); 35 | }; 36 | } //end namespace jnibench 37 | 38 | #endif // FOO -------------------------------------------------------------------------------- /src/main/c++/call/FooByCall.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include "com_evolvedbinary_jnibench_common_call_FooByCall.h" 29 | #include "Foo.h" 30 | 31 | /* 32 | * Class: com_evolvedbinary_jnibench_common_FooByCall 33 | * Method: newFoo 34 | * Signature: ()J 35 | */ 36 | jlong Java_com_evolvedbinary_jnibench_common_call_FooByCall_newFoo(JNIEnv* env, jobject jobj) { 37 | jnibench::Foo* foo = new jnibench::Foo(); 38 | return reinterpret_cast(foo); 39 | } 40 | 41 | /* 42 | * Class: com_evolvedbinary_jnibench_common_FooByCall 43 | * Method: disposeInternal 44 | * Signature: (J)V 45 | */ 46 | void Java_com_evolvedbinary_jnibench_common_call_FooByCall_disposeInternal(JNIEnv* env, jobject jobj, jlong handle) { 47 | delete reinterpret_cast(handle); 48 | } -------------------------------------------------------------------------------- /src/main/c++/call/FooByCallFinal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include "com_evolvedbinary_jnibench_common_call_FooByCallFinal.h" 29 | #include "Foo.h" 30 | 31 | /* 32 | * Class: com_evolvedbinary_jnibench_common_FooByCallFinal 33 | * Method: newFoo 34 | * Signature: ()J 35 | */ 36 | jlong Java_com_evolvedbinary_jnibench_common_call_FooByCallFinal_newFoo(JNIEnv* env, jobject jobj) { 37 | jnibench::Foo* foo = new jnibench::Foo(); 38 | return reinterpret_cast(foo); 39 | } 40 | 41 | /* 42 | * Class: com_evolvedbinary_jnibench_common_FooByCallFinal 43 | * Method: disposeInternal 44 | * Signature: (J)V 45 | */ 46 | void Java_com_evolvedbinary_jnibench_common_call_FooByCallFinal_disposeInternal(JNIEnv* env, jobject jobj, jlong handle) { 47 | delete reinterpret_cast(handle); 48 | } -------------------------------------------------------------------------------- /src/main/c++/call/FooByCallInvoke.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include "com_evolvedbinary_jnibench_common_call_FooByCallInvoke.h" 29 | #include "Foo.h" 30 | #include "Portal.h" 31 | 32 | /* 33 | * Class: com_evolvedbinary_jnibench_common_FooByCallInvoke 34 | * Method: newFoo 35 | * Signature: ()J 36 | */ 37 | void Java_com_evolvedbinary_jnibench_common_call_FooByCallInvoke_newFoo(JNIEnv* env, jobject jobj) { 38 | jnibench::Foo* foo = new jnibench::Foo(); 39 | 40 | //set the _nativeHandle in Java 41 | jnibench::FooByCallInvokeJni::setHandle(env, jobj, foo); 42 | } 43 | 44 | /* 45 | * Class: com_evolvedbinary_jnibench_common_FooByCallInvoke 46 | * Method: disposeInternal 47 | * Signature: (J)V 48 | */ 49 | void Java_com_evolvedbinary_jnibench_common_call_FooByCallInvoke_disposeInternal(JNIEnv* env, jobject jobj, jlong handle) { 50 | delete reinterpret_cast(handle); 51 | } -------------------------------------------------------------------------------- /src/main/c++/call/FooByCallInvokeFinal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include "com_evolvedbinary_jnibench_common_call_FooByCallInvokeFinal.h" 29 | #include "Foo.h" 30 | #include "Portal.h" 31 | 32 | /* 33 | * Class: com_evolvedbinary_jnibench_common_FooByCallInvokeFinal 34 | * Method: newFoo 35 | * Signature: ()J 36 | */ 37 | void Java_com_evolvedbinary_jnibench_common_call_FooByCallInvokeFinal_newFoo(JNIEnv* env, jobject jobj) { 38 | jnibench::Foo* foo = new jnibench::Foo(); 39 | 40 | //set the _nativeHandle in Java 41 | jnibench::FooByCallInvokeFinalJni::setHandle(env, jobj, foo); 42 | } 43 | 44 | /* 45 | * Class: com_evolvedbinary_jnibench_common_FooByCallInvoke 46 | * Method: disposeInternal 47 | * Signature: (J)V 48 | */ 49 | void Java_com_evolvedbinary_jnibench_common_call_FooByCallInvokeFinal_disposeInternal(JNIEnv* env, jobject jobj, jlong handle) { 50 | delete reinterpret_cast(handle); 51 | } -------------------------------------------------------------------------------- /src/main/c++/call/FooByCallStatic.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include "com_evolvedbinary_jnibench_common_call_FooByCallStatic.h" 29 | #include "Foo.h" 30 | 31 | /* 32 | * Class: com_evolvedbinary_jnibench_common_FooByCallStatic 33 | * Method: newFoo 34 | * Signature: ()J 35 | */ 36 | jlong Java_com_evolvedbinary_jnibench_common_call_FooByCallStatic_newFoo(JNIEnv* env, jclass jcls) { 37 | jnibench::Foo* foo = new jnibench::Foo(); 38 | return reinterpret_cast(foo); 39 | } 40 | 41 | /* 42 | * Class: com_evolvedbinary_jnibench_common_FooByCallStatic 43 | * Method: disposeInternal 44 | * Signature: (J)V 45 | */ 46 | void Java_com_evolvedbinary_jnibench_common_call_FooByCallStatic_disposeInternal(JNIEnv* env, jclass jcls, jlong handle) { 47 | delete reinterpret_cast(handle); 48 | } -------------------------------------------------------------------------------- /src/main/c++/call/FooByCallStaticFinal.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include "com_evolvedbinary_jnibench_common_call_FooByCallStaticFinal.h" 29 | #include "Foo.h" 30 | 31 | /* 32 | * Class: com_evolvedbinary_jnibench_common_FooByCallStaticFinal 33 | * Method: newFoo 34 | * Signature: ()J 35 | */ 36 | jlong Java_com_evolvedbinary_jnibench_common_call_FooByCallStaticFinal_newFoo(JNIEnv* env, jclass jcls) { 37 | jnibench::Foo* foo = new jnibench::Foo(); 38 | return reinterpret_cast(foo); 39 | } 40 | 41 | /* 42 | * Class: com_evolvedbinary_jnibench_common_FooByCallStaticFinal 43 | * Method: disposeInternal 44 | * Signature: (J)V 45 | */ 46 | void Java_com_evolvedbinary_jnibench_common_call_FooByCallStaticFinal_disposeInternal(JNIEnv* env, jclass jcls, jlong handle) { 47 | delete reinterpret_cast(handle); 48 | } -------------------------------------------------------------------------------- /src/main/c++/call/Portal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | #include 28 | #include 29 | 30 | namespace jnibench { 31 | 32 | // Native class template 33 | template class FooJniClass { 34 | public: 35 | // Get the java class id 36 | static jclass getJClass(JNIEnv* env, const char* jclazz_name) { 37 | jclass jclazz = env->FindClass(jclazz_name); 38 | assert(jclazz != nullptr); 39 | return jclazz; 40 | } 41 | 42 | // Get the field id of the member variable to store 43 | // the ptr 44 | static jfieldID getHandleFieldID(JNIEnv* env) { 45 | static jfieldID fid = env->GetFieldID( 46 | DERIVED::getJClass(env), "_nativeHandle", "J"); 47 | assert(fid != nullptr); 48 | return fid; 49 | } 50 | 51 | // Get the pointer from Java 52 | static PTR getHandle(JNIEnv* env, jobject jobj) { 53 | return reinterpret_cast( 54 | env->GetLongField(jobj, getHandleFieldID(env))); 55 | } 56 | 57 | // Pass the pointer to the java side. 58 | static void setHandle(JNIEnv* env, jobject jdb, PTR ptr) { 59 | env->SetLongField( 60 | jdb, getHandleFieldID(env), 61 | reinterpret_cast(ptr)); 62 | } 63 | }; 64 | 65 | 66 | // The portal class for com.evolvedbinary.jnibench.common.call.FooByCallInvoke 67 | class FooByCallInvokeJni : public FooJniClass { 68 | public: 69 | static jclass getJClass(JNIEnv* env) { 70 | return FooJniClass::getJClass(env, 71 | "com/evolvedbinary/jnibench/common/call/FooByCallInvoke"); 72 | } 73 | }; 74 | 75 | // The portal class for com.evolvedbinary.jnibench.common.call.FooByCallInvokeFinal 76 | class FooByCallInvokeFinalJni : public FooJniClass { 77 | public: 78 | static jclass getJClass(JNIEnv* env) { 79 | return FooJniClass::getJClass(env, 80 | "com/evolvedbinary/jnibench/common/call/FooByCallInvokeFinal"); 81 | } 82 | }; 83 | 84 | } //end namespace jnibench -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/NativeBackedObject.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common; 28 | 29 | /** 30 | * A Java Object which is backed by a C++ object. 31 | * 32 | * @author Adam Retter 33 | */ 34 | public abstract class NativeBackedObject implements AutoCloseable { 35 | 36 | protected long _nativeHandle; 37 | protected boolean _nativeOwner; 38 | 39 | protected NativeBackedObject() { 40 | this._nativeHandle = 0; 41 | this._nativeOwner = true; 42 | } 43 | 44 | @Override 45 | public void close() { 46 | // synchronized(this) { 47 | if(_nativeOwner && _nativeHandle != 0) { 48 | disposeInternal(); 49 | _nativeHandle = 0; 50 | _nativeOwner = false; 51 | } 52 | // } 53 | } 54 | 55 | public long get_nativeHandle() { 56 | return _nativeHandle; 57 | } 58 | 59 | protected abstract void disposeInternal(); 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/AllocateInCppGet2DArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | 32 | public class AllocateInCppGet2DArray implements JniListSupplier { 33 | @Override 34 | public List getObjectList(final NativeObjectArray nativeObjectArray) { 35 | final Object[][] objArr = get2DArray(nativeObjectArray.get_nativeHandle()); 36 | final String[] names = (String[]) objArr[0]; 37 | final Long[] values = (Long[]) objArr[1]; 38 | final List objList = new ArrayList<>(); 39 | for (int i = 0; i < names.length; ++i) { 40 | objList.add(new FooObject(names[i], values[i])); 41 | } 42 | return objList; 43 | } 44 | 45 | protected static native Object[][] get2DArray(final long handle); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/AllocateInCppGetArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | import java.util.Arrays; 30 | import java.util.List; 31 | 32 | public class AllocateInCppGetArray implements JniListSupplier { 33 | 34 | @Override 35 | public List getObjectList(final NativeObjectArray nativeObjectArray) { 36 | return Arrays.asList(getArray(nativeObjectArray.get_nativeHandle())); 37 | } 38 | 39 | private static native FooObject[] getArray(final long handle); 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/AllocateInCppGetArrayList.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | import java.util.List; 30 | 31 | public class AllocateInCppGetArrayList implements JniListSupplier { 32 | 33 | @Override 34 | public List getObjectList(final NativeObjectArray nativeObjectArray) { 35 | return getArrayList(nativeObjectArray.get_nativeHandle()); 36 | } 37 | 38 | private static native List getArrayList(final long handle); 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/AllocateInJavaGet2DArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | import java.util.ArrayList; 30 | import java.util.Collections; 31 | import java.util.List; 32 | 33 | public class AllocateInJavaGet2DArray implements JniListSupplier { 34 | 35 | @Override 36 | public List getObjectList(final NativeObjectArray nativeObjectArray) { 37 | final int len = (int) getArraySize(nativeObjectArray.get_nativeHandle()); 38 | if (len == 0) { 39 | return Collections.emptyList(); 40 | } else { 41 | final String names[] = new String[len]; 42 | final long values[] = new long[len]; 43 | 44 | getArrays(nativeObjectArray.get_nativeHandle(), names, values); 45 | 46 | final List objectList = new ArrayList<>(); 47 | for (int i = 0; i < len; i++) { 48 | objectList.add(new FooObject(names[i], values[i])); 49 | } 50 | return objectList; 51 | } 52 | } 53 | 54 | private static native long getArraySize(final long handle); 55 | 56 | private static native void getArrays(final long handle, final String[] paths, 57 | final long[] targetSizes); 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/AllocateInJavaGetArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | import java.util.Arrays; 30 | import java.util.Collections; 31 | import java.util.List; 32 | 33 | public class AllocateInJavaGetArray implements JniListSupplier { 34 | 35 | @Override 36 | public List getObjectList(final NativeObjectArray nativeObjectArray) { 37 | final int len = (int) getArraySize(nativeObjectArray.get_nativeHandle()); 38 | if (len == 0) { 39 | return Collections.emptyList(); 40 | } else { 41 | final FooObject objectList[] = new FooObject[len]; 42 | 43 | getArray(nativeObjectArray.get_nativeHandle(), objectList); 44 | 45 | return Arrays.asList(objectList); 46 | } 47 | } 48 | 49 | private static native long getArraySize(final long handle); 50 | 51 | private static native void getArray(final long handle, final FooObject[] objectList); 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/AllocateInJavaGetArrayList.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | import java.util.ArrayList; 30 | import java.util.Collections; 31 | import java.util.List; 32 | 33 | public class AllocateInJavaGetArrayList implements JniListSupplier { 34 | 35 | @Override 36 | public List getObjectList(final NativeObjectArray nativeObjectArray) { 37 | final int len = (int) getListSize(nativeObjectArray.get_nativeHandle()); 38 | if (len == 0) { 39 | return Collections.emptyList(); 40 | } else { 41 | final List objectList = new ArrayList<>(len); 42 | 43 | getList(nativeObjectArray.get_nativeHandle(), objectList); 44 | 45 | return objectList; 46 | } 47 | } 48 | 49 | private static native long getListSize(final long handle); 50 | 51 | private static native void getList(final long handle, final List list); 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/AllocateInJavaGetMutableArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | import java.util.Arrays; 30 | import java.util.Collections; 31 | import java.util.List; 32 | 33 | public class AllocateInJavaGetMutableArray implements JniListSupplier { 34 | 35 | @Override 36 | public List getObjectList(final NativeObjectArray nativeObjectArray) { 37 | final int len = (int) getArraySize(nativeObjectArray.get_nativeHandle()); 38 | if (len == 0) { 39 | return Collections.emptyList(); 40 | } else { 41 | final FooObject objectList[] = new FooObject[len]; 42 | for (int i = 0; i < len; i++) { 43 | objectList[i] = new FooObject(); 44 | } 45 | 46 | getArray(nativeObjectArray.get_nativeHandle(), objectList); 47 | 48 | return Arrays.asList(objectList); 49 | } 50 | } 51 | 52 | private static native long getArraySize(final long handle); 53 | 54 | private static native void getArray(final long handle, final FooObject[] objectList); 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/FooNativeObjectArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | public class FooNativeObjectArray extends NativeObjectArray { 30 | 31 | public FooNativeObjectArray(final FooObject[] fooObjects) { 32 | super(fooObjects); 33 | } 34 | 35 | @Override 36 | protected long newObjectArray(final FooObject[] objectArray) { 37 | return newFooObjectArray(objectArray); 38 | } 39 | 40 | @Override 41 | protected void disposeInternal() { 42 | disposeInternal(_nativeHandle); 43 | } 44 | 45 | private static native long newFooObjectArray(final Object[] fooObjects); 46 | 47 | private native void disposeInternal(final long handle); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/FooObject.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | public class FooObject { 30 | String name; 31 | long value; 32 | 33 | FooObject() { 34 | } 35 | 36 | public FooObject(final String name, final long value) { 37 | this.name = name; 38 | this.value = value; 39 | } 40 | 41 | @Override 42 | public boolean equals(Object o) { 43 | if (this == o) return true; 44 | if (o == null || getClass() != o.getClass()) return false; 45 | 46 | FooObject fooObject = (FooObject) o; 47 | 48 | if (value != fooObject.value) return false; 49 | return name.equals(fooObject.name); 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | int result = name.hashCode(); 55 | result = 31 * result + (int) (value ^ (value >>> 32)); 56 | return result; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/JniListSupplier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | import java.util.List; 30 | 31 | public interface JniListSupplier { 32 | List getObjectList(final NativeObjectArray nativeObjectArray); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/array/NativeObjectArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.array; 28 | 29 | import com.evolvedbinary.jnibench.common.NativeBackedObject; 30 | 31 | public abstract class NativeObjectArray extends NativeBackedObject { 32 | NativeObjectArray(T[] objects) { 33 | _nativeHandle = newObjectArray(objects); 34 | } 35 | 36 | protected abstract long newObjectArray(final T[] objectArray); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/call/FooByCall.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.call; 28 | 29 | import com.evolvedbinary.jnibench.common.NativeBackedObject; 30 | 31 | /** 32 | * Follows 9.2.3 Pattern 1: Call from Java Platform Performance by Steve Wilson 33 | * for setting up the handle to the native object. 34 | * 35 | * @author Adam Retter 36 | */ 37 | public class FooByCall extends NativeBackedObject { 38 | public FooByCall() { 39 | super(); 40 | this._nativeHandle = newFoo(); 41 | } 42 | 43 | @Override 44 | protected void disposeInternal() { 45 | disposeInternal(_nativeHandle); 46 | } 47 | 48 | private native long newFoo(); 49 | private native void disposeInternal(final long handle); 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/call/FooByCallFinal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.call; 28 | 29 | import com.evolvedbinary.jnibench.common.NativeBackedObject; 30 | 31 | /** 32 | * Similar to {@link FooByCall} but this class is marked final. 33 | * 34 | * @author Adam Retter 35 | */ 36 | public final class FooByCallFinal extends NativeBackedObject { 37 | public FooByCallFinal() { 38 | super(); 39 | this._nativeHandle = newFoo(); 40 | } 41 | 42 | @Override 43 | protected void disposeInternal() { 44 | disposeInternal(_nativeHandle); 45 | } 46 | 47 | private native long newFoo(); 48 | private native void disposeInternal(final long handle); 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/call/FooByCallInvoke.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.call; 28 | 29 | import com.evolvedbinary.jnibench.common.NativeBackedObject; 30 | 31 | /** 32 | * Follows 9.2.7 Pattern 4: Call-Invoke from Java Platform Performance by Steve Wilson 33 | * for setting up the handle to the native object. 34 | * 35 | * @author Adam Retter 36 | */ 37 | public class FooByCallInvoke extends NativeBackedObject { 38 | public FooByCallInvoke() { 39 | super(); 40 | newFoo(); //the native method, will find _nativeHandle from the class and set it directly 41 | } 42 | 43 | @Override 44 | protected void disposeInternal() { 45 | disposeInternal(_nativeHandle); 46 | } 47 | 48 | private native void newFoo(); 49 | private native void disposeInternal(final long handle); 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/call/FooByCallInvokeFinal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.call; 28 | 29 | import com.evolvedbinary.jnibench.common.NativeBackedObject; 30 | 31 | /** 32 | * Similar to {@link FooByCallInvoke} but this class is marked final. 33 | * 34 | * @author Adam Retter 35 | */ 36 | public final class FooByCallInvokeFinal extends NativeBackedObject { 37 | public FooByCallInvokeFinal() { 38 | super(); 39 | newFoo(); //the native method, will find _nativeHandle from the class and set it directly 40 | } 41 | 42 | @Override 43 | protected void disposeInternal() { 44 | disposeInternal(_nativeHandle); 45 | } 46 | 47 | private native void newFoo(); 48 | private native void disposeInternal(final long handle); 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/call/FooByCallStatic.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.call; 28 | 29 | import com.evolvedbinary.jnibench.common.NativeBackedObject; 30 | 31 | /** 32 | * Follows 9.2.3 Pattern 1: Call from Java Platform Performance by Steve Wilson 33 | * for setting up the handle to the native object. 34 | * 35 | * @author Adam Retter 36 | */ 37 | public class FooByCallStatic extends NativeBackedObject { 38 | public FooByCallStatic() { 39 | super(); 40 | this._nativeHandle = newFoo(); 41 | } 42 | 43 | @Override 44 | protected void disposeInternal() { 45 | disposeInternal(_nativeHandle); 46 | } 47 | 48 | private static native long newFoo(); 49 | private static native void disposeInternal(final long handle); 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/common/call/FooByCallStaticFinal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.common.call; 28 | 29 | import com.evolvedbinary.jnibench.common.NativeBackedObject; 30 | 31 | /** 32 | * Similar to {@link FooByCallStatic} but this class is marked final. 33 | * 34 | * @author Adam Retter 35 | */ 36 | public final class FooByCallStaticFinal extends NativeBackedObject { 37 | public FooByCallStaticFinal() { 38 | super(); 39 | this._nativeHandle = newFoo(); 40 | } 41 | 42 | @Override 43 | protected void disposeInternal() { 44 | disposeInternal(_nativeHandle); 45 | } 46 | 47 | private static native long newFoo(); 48 | private static native void disposeInternal(final long handle); 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/consbench/ArrayBenchmark.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.consbench; 28 | 29 | import com.evolvedbinary.jnibench.common.array.*; 30 | 31 | import java.util.ArrayList; 32 | import java.util.List; 33 | import java.util.Random; 34 | 35 | import static com.evolvedbinary.jnibench.consbench.BenchmarkHelper.outputResults; 36 | import static com.evolvedbinary.jnibench.consbench.BenchmarkHelper.time; 37 | 38 | public class ArrayBenchmark implements BenchmarkInterface { 39 | 40 | private static final String ARRAY_SIZE_PARAM = "arraySize"; 41 | private static final int[] DEFAULT_ARRAY_SIZES = { 10, 50, 512, 1024, 4096, 16384 }; 42 | 43 | @Override 44 | public void test(final BenchmarkOptions benchmarkOptions) { 45 | 46 | // get array sizes parameterization 47 | List arraySizesParam = benchmarkOptions.getParams().get(ARRAY_SIZE_PARAM); 48 | final int[] arraySizes; 49 | if (arraySizesParam != null && !arraySizesParam.isEmpty()) { 50 | arraySizes = new int[arraySizesParam.size()]; 51 | for (int i = 0; i < arraySizes.length; i++) { 52 | arraySizes[i] = Integer.parseInt(arraySizesParam.get(i)); 53 | } 54 | } else { 55 | arraySizes = DEFAULT_ARRAY_SIZES; 56 | } 57 | 58 | // create fixtures 59 | final List benchmarkFixtures = new ArrayList<>(8 * arraySizes.length); 60 | for (int i = 0; i < arraySizes.length; i ++) { 61 | final int arraySize = arraySizes[i]; 62 | 63 | benchmarkFixtures.add(new ArrayBenchmarkFixture(arraySize, "Allocate array in Java", AllocateInJavaGetArray::new)); 64 | benchmarkFixtures.add(new ArrayBenchmarkFixture(arraySize, "Allocate array of mutable objects in Java", AllocateInJavaGetMutableArray::new)); 65 | benchmarkFixtures.add(new ArrayBenchmarkFixture(arraySize, "Allocate 2D array in Java", AllocateInJavaGet2DArray::new)); 66 | benchmarkFixtures.add(new ArrayBenchmarkFixture(arraySize, "Allocate array in CPP", AllocateInCppGetArray::new)); 67 | benchmarkFixtures.add(new ArrayBenchmarkFixture(arraySize, "Allocate 2D object array in CPP", AllocateInCppGet2DArray::new)); 68 | benchmarkFixtures.add(new ArrayBenchmarkFixture(arraySize, "Allocate 2D object array in CPP and wrap result in custom Java List", AllocateInCppGet2DArrayListWrapper::new)); 69 | benchmarkFixtures.add(new ArrayBenchmarkFixture(arraySize, "Allocate array list in Java", AllocateInJavaGetArrayList::new)); 70 | benchmarkFixtures.add(new ArrayBenchmarkFixture(arraySize, "Allocate array list in CPP", AllocateInCppGetArrayList::new)); 71 | } 72 | 73 | // run each benchmark fixture 74 | final int iterations = benchmarkOptions.getIterations(); 75 | for (final ArrayBenchmarkFixture benchmarkFixture : benchmarkFixtures) { 76 | final FooNativeObjectArray fooObjectArray = getNativeObjectArray(benchmarkFixture.getArraySize()); 77 | final JniListSupplier listSupplier = benchmarkFixture.listSupplierConstructor.get(); 78 | benchmarkFixture.start = time(benchmarkOptions.isInNs()); 79 | for (int i = 0; i < iterations; i++) { 80 | listSupplier.getObjectList(fooObjectArray); 81 | } 82 | benchmarkFixture.end = time(benchmarkOptions.isInNs()); 83 | } 84 | 85 | // output the results of the benchmarks 86 | outputResults(benchmarkOptions.isOutputAsCSV(), benchmarkOptions.isNoCsvHeader(), benchmarkOptions.isInNs(), benchmarkFixtures); 87 | } 88 | 89 | private static FooNativeObjectArray getNativeObjectArray(final int arraySize) { 90 | final FooObject[] fooObjects = new FooObject[arraySize]; 91 | final Random random = new Random(); 92 | for (int i = 0; i < arraySize; i++) { 93 | final int num = random.nextInt(); 94 | fooObjects[i] = new FooObject("str" + num, num); 95 | } 96 | return new FooNativeObjectArray(fooObjects); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/consbench/ArrayBenchmarkFixture.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.consbench; 28 | 29 | import com.evolvedbinary.jnibench.common.array.FooObject; 30 | import com.evolvedbinary.jnibench.common.array.JniListSupplier; 31 | import java.util.function.Supplier; 32 | 33 | public class ArrayBenchmarkFixture implements BenchmarkFixture { 34 | final int arraySize; 35 | final String description; 36 | final Supplier> listSupplierConstructor; 37 | long start; 38 | long end; 39 | 40 | public ArrayBenchmarkFixture(final int arraySize, final String description, final Supplier> listSupplierConstructor) { 41 | this.arraySize = arraySize; 42 | this.description = description; 43 | this.listSupplierConstructor = listSupplierConstructor; 44 | } 45 | 46 | public int getArraySize() { 47 | return arraySize; 48 | } 49 | 50 | @Override 51 | public String getDescription() { 52 | return description + " (size " + arraySize+ ")"; 53 | } 54 | 55 | @Override 56 | public long duration() { 57 | return end - start; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/consbench/Benchmark.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.consbench; 28 | 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | import java.util.Map; 32 | import java.util.TreeMap; 33 | 34 | /** 35 | * A small JNI Benchmark to show the difference 36 | * in cost between various models of Object Construction 37 | * for a Java API that wraps a C++ API using JNI 38 | * 39 | * @author Adam Retter 40 | */ 41 | public class Benchmark { 42 | private final static int DEFAULT_ITERATIONS = 1_000_000; 43 | 44 | public static final void main(final String args[]) { 45 | 46 | int iterations = DEFAULT_ITERATIONS; 47 | boolean outputAsCSV = false; 48 | boolean noCsvHeader = false; 49 | boolean inNs = false; 50 | boolean close = false; 51 | String benchmarkName = "CallBenchmark"; 52 | final Map> params = new TreeMap<>(); 53 | 54 | if (args != null && args.length > 0) { 55 | for (String arg : args) { 56 | if (arg.startsWith("--iterations=")) { 57 | arg = arg.substring("--iterations=".length()); 58 | iterations = Integer.parseInt(arg); 59 | } else if (arg.equals("--csv")) { 60 | outputAsCSV = true; 61 | } else if (arg.equals("--no-csv-header")) { 62 | noCsvHeader = true; 63 | } else if (arg.equals("--ns")) { 64 | inNs = true; 65 | } else if (arg.equals("--close")) { 66 | close = true; 67 | } else if (arg.startsWith("--benchmark=")) { 68 | benchmarkName = arg.substring("--benchmark=".length()); 69 | } else if (arg.startsWith("--param=")) { 70 | final String param = arg.substring("--param=".length()); 71 | final String[] nameValue = param.split(":"); 72 | List values = params.get(nameValue[0]); 73 | if (values == null) { 74 | values = new ArrayList<>(); 75 | params.put(nameValue[0], values); 76 | } 77 | values.add(nameValue[1]); 78 | } else if (arg.equals("--help") || arg.equals("-h") || arg.equals("/?")) { 79 | System.out.println(); 80 | System.out.println("Benchmark"); 81 | System.out.println("--iterations=n set the number of iterations"); 82 | System.out.println("--csv output results in CSV format"); 83 | System.out.println("--no-csv-header disable CSV header line"); 84 | System.out.println("--ns compute times in ns as opposed to ms"); 85 | System.out.println("--close native objects should be closed (disposed) after use"); 86 | System.out.println("--benchmark=name name of the benchmark"); 87 | System.out.println("--param=name:value parameter for the benchmark"); 88 | System.out.println(); 89 | } 90 | } 91 | } 92 | 93 | NarSystem.loadLibrary(); 94 | 95 | final BenchmarkOptions benchmarkOptions = new BenchmarkOptions(iterations, outputAsCSV, noCsvHeader, inNs, close, params); 96 | 97 | try { 98 | final Class benchmarkClazz = Class.forName("com.evolvedbinary.jnibench.consbench." + benchmarkName); 99 | final BenchmarkInterface benchmarkObject = (BenchmarkInterface) benchmarkClazz.newInstance(); 100 | benchmarkObject.test(benchmarkOptions); 101 | } catch (final ClassNotFoundException | IllegalAccessException | InstantiationException e) { 102 | e.printStackTrace(); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/consbench/BenchmarkFixture.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.consbench; 28 | 29 | public interface BenchmarkFixture { 30 | String getDescription(); 31 | long duration(); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/consbench/BenchmarkHelper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.consbench; 28 | 29 | import java.util.List; 30 | 31 | public class BenchmarkHelper { 32 | 33 | public static void outputResults(final boolean outputAsCSV, final boolean noCsvHeader, final boolean inNs, final List benchmarkFixtures) { 34 | if (outputAsCSV) { 35 | final StringBuilder stringBuilder = new StringBuilder(); 36 | 37 | // header 38 | if (!noCsvHeader) { 39 | for (int i = 0; i < benchmarkFixtures.size(); i++) { 40 | stringBuilder.append('"').append(benchmarkFixtures.get(i).getDescription()).append('"'); 41 | if (i < benchmarkFixtures.size() - 1) { 42 | stringBuilder.append(','); 43 | } 44 | } 45 | stringBuilder.append(System.getProperty("line.separator")); 46 | } 47 | 48 | // data 49 | for (int i = 0; i < benchmarkFixtures.size(); i++) { 50 | stringBuilder.append(benchmarkFixtures.get(i).duration()); 51 | if (i < benchmarkFixtures.size() - 1) { 52 | stringBuilder.append(','); 53 | } 54 | } 55 | 56 | System.out.println(stringBuilder.toString()); 57 | } else { 58 | final String timeUnits = timeUnits(inNs); 59 | for (final BenchmarkFixture benchmarkFixture : benchmarkFixtures) { 60 | System.out.println(benchmarkFixture.getDescription() + ": " + benchmarkFixture.duration() + timeUnits); 61 | } 62 | } 63 | } 64 | 65 | public static long time(final boolean inNs) { 66 | if (inNs) { 67 | return System.nanoTime(); 68 | } else { 69 | return System.currentTimeMillis(); 70 | } 71 | } 72 | 73 | private static String timeUnits(final boolean inNs) { 74 | if (inNs) { 75 | return "ns"; 76 | } else { 77 | return "ms"; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/consbench/BenchmarkInterface.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.consbench; 28 | 29 | public interface BenchmarkInterface { 30 | void test(final BenchmarkOptions benchmarkOptions); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/consbench/BenchmarkOptions.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.consbench; 28 | 29 | import java.util.List; 30 | import java.util.Map; 31 | 32 | public class BenchmarkOptions { 33 | private final int iterations; 34 | private final boolean outputAsCSV; 35 | private final boolean noCsvHeader; 36 | private final boolean inNs; 37 | private final boolean close; 38 | private final Map> params; 39 | 40 | public BenchmarkOptions(final int iterations, final boolean outputAsCSV, final boolean noCsvHeader, final boolean inNs, final boolean close, final Map> params) { 41 | this.iterations = iterations; 42 | this.outputAsCSV = outputAsCSV; 43 | this.noCsvHeader = noCsvHeader; 44 | this.inNs = inNs; 45 | this.close = close; 46 | this.params = params; 47 | } 48 | 49 | public int getIterations() { 50 | return iterations; 51 | } 52 | 53 | public boolean isOutputAsCSV() { 54 | return outputAsCSV; 55 | } 56 | 57 | public boolean isNoCsvHeader() { 58 | return noCsvHeader; 59 | } 60 | 61 | public boolean isInNs() { 62 | return inNs; 63 | } 64 | 65 | public boolean isClose() { 66 | return close; 67 | } 68 | 69 | public Map> getParams() { 70 | return params; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/consbench/CallBenchmark.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.consbench; 28 | 29 | import com.evolvedbinary.jnibench.common.NativeBackedObject; 30 | import com.evolvedbinary.jnibench.common.call.*; 31 | 32 | import java.util.Arrays; 33 | 34 | import static com.evolvedbinary.jnibench.consbench.BenchmarkHelper.outputResults; 35 | import static com.evolvedbinary.jnibench.consbench.BenchmarkHelper.time; 36 | 37 | public class CallBenchmark implements BenchmarkInterface { 38 | 39 | @Override 40 | public void test(final BenchmarkOptions benchmarkOptions) { 41 | if (benchmarkOptions.isClose()) { 42 | testWithClose(benchmarkOptions.getIterations(), benchmarkOptions.isOutputAsCSV(), benchmarkOptions.isNoCsvHeader(), benchmarkOptions.isInNs()); 43 | } else { 44 | testWithoutClose(benchmarkOptions.getIterations(), benchmarkOptions.isOutputAsCSV(), benchmarkOptions.isNoCsvHeader(), benchmarkOptions.isInNs()); 45 | } 46 | } 47 | 48 | private static void testWithClose(final int iterations, final boolean outputAsCSV, final boolean noCsvHeader, final boolean inNs) { 49 | final CallBenchmarkFixture[] benchmarkFixtures = { 50 | new CallBenchmarkFixture("FooByCall", FooByCall::new), 51 | new CallBenchmarkFixture("FooByCallStatic", FooByCallStatic::new), 52 | new CallBenchmarkFixture("FooByCallInvoke", FooByCallInvoke::new), 53 | new CallBenchmarkFixture("FooByCallFinal", FooByCallFinal::new), 54 | new CallBenchmarkFixture("FooByCallStaticFinal", FooByCallStaticFinal::new), 55 | new CallBenchmarkFixture("FooByCallInvokeFinal", FooByCallInvokeFinal::new), 56 | }; 57 | 58 | // run each benchmark fixture 59 | for (final CallBenchmarkFixture benchmarkFixture : benchmarkFixtures) { 60 | benchmarkFixture.start = time(inNs); 61 | for (int i = 0; i < iterations; i++) { 62 | final NativeBackedObject nativeBackedObject = benchmarkFixture.nativeBackedObjectConstructor.get(); 63 | nativeBackedObject.close(); // CLOSE 64 | } 65 | benchmarkFixture.end = time(inNs); 66 | } 67 | 68 | outputResults(outputAsCSV, noCsvHeader, inNs, Arrays.asList(benchmarkFixtures)); 69 | } 70 | 71 | private static void testWithoutClose(final int iterations, final boolean outputAsCSV, final boolean noCsvHeader, final boolean inNs) { 72 | final CallBenchmarkFixture[] benchmarkFixtures = { 73 | new CallBenchmarkFixture("FooByCall", FooByCall::new), 74 | new CallBenchmarkFixture("FooByCallStatic", FooByCallStatic::new), 75 | new CallBenchmarkFixture("FooByCallInvoke", FooByCallInvoke::new), 76 | new CallBenchmarkFixture("FooByCallFinal", FooByCallFinal::new), 77 | new CallBenchmarkFixture("FooByCallStaticFinal", FooByCallStaticFinal::new), 78 | new CallBenchmarkFixture("FooByCallInvokeFinal", FooByCallInvokeFinal::new), 79 | }; 80 | 81 | // run each benchmark fixture 82 | for (final CallBenchmarkFixture benchmarkFixture : benchmarkFixtures) { 83 | benchmarkFixture.start = time(inNs); 84 | for (int i = 0; i < iterations; i++) { 85 | final NativeBackedObject nativeBackedObject = benchmarkFixture.nativeBackedObjectConstructor.get(); 86 | } 87 | benchmarkFixture.end = time(inNs); 88 | } 89 | 90 | outputResults(outputAsCSV, noCsvHeader, inNs, Arrays.asList(benchmarkFixtures)); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/consbench/CallBenchmarkFixture.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.consbench; 28 | 29 | import com.evolvedbinary.jnibench.common.NativeBackedObject; 30 | 31 | import java.util.function.Supplier; 32 | 33 | public class CallBenchmarkFixture implements BenchmarkFixture { 34 | final String description; 35 | final Supplier nativeBackedObjectConstructor; 36 | long start; 37 | long end; 38 | 39 | public CallBenchmarkFixture(final String description, final Supplier nativeBackedObjectConstructor) { 40 | this.description = description; 41 | this.nativeBackedObjectConstructor = nativeBackedObjectConstructor; 42 | } 43 | 44 | @Override 45 | public String getDescription() { 46 | return description; 47 | } 48 | 49 | @Override 50 | public long duration() { 51 | return 0; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/ConstructionBenchmark.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2016, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench; 28 | 29 | import com.evolvedbinary.jnibench.common.call.FooByCall; 30 | import com.evolvedbinary.jnibench.common.call.FooByCallFinal; 31 | import com.evolvedbinary.jnibench.common.call.FooByCallInvoke; 32 | import com.evolvedbinary.jnibench.common.call.FooByCallInvokeFinal; 33 | import com.evolvedbinary.jnibench.common.call.FooByCallStatic; 34 | import com.evolvedbinary.jnibench.common.call.FooByCallStaticFinal; 35 | import com.evolvedbinary.jnibench.consbench.NarSystem; 36 | import org.openjdk.jmh.annotations.Benchmark; 37 | import org.openjdk.jmh.annotations.BenchmarkMode; 38 | import org.openjdk.jmh.annotations.Mode; 39 | import org.openjdk.jmh.annotations.OutputTimeUnit; 40 | 41 | import java.util.concurrent.TimeUnit; 42 | 43 | @BenchmarkMode(Mode.SampleTime) 44 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 45 | public class ConstructionBenchmark { 46 | 47 | static { 48 | NarSystem.loadLibrary(); 49 | } 50 | 51 | @Benchmark 52 | public FooByCall fooByCall() { 53 | return new FooByCall(); 54 | } 55 | 56 | @Benchmark 57 | public FooByCallStatic fooByCallStatic() { 58 | return new FooByCallStatic(); 59 | } 60 | 61 | @Benchmark 62 | public FooByCallInvoke fooByCallInvoke() { 63 | return new FooByCallInvoke(); 64 | } 65 | 66 | @Benchmark 67 | public FooByCallFinal fooByCallFinal() { 68 | return new FooByCallFinal(); 69 | } 70 | 71 | @Benchmark 72 | public FooByCallStaticFinal fooByCallStaticFinal() { 73 | return new FooByCallStaticFinal(); 74 | } 75 | 76 | @Benchmark 77 | public FooByCallInvokeFinal fooByCallInvokeFinal() { 78 | return new FooByCallInvokeFinal(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/cache/AllocationCache.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2021, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench.cache; 28 | 29 | public interface AllocationCache { 30 | 31 | public enum Checksum { 32 | none, 33 | copyout, 34 | bytesum, 35 | longsum, 36 | }; 37 | 38 | public enum Prepare { 39 | none, 40 | copyin 41 | }; 42 | 43 | T acquire(); 44 | 45 | void release(T item); 46 | 47 | void checksumBuffer(T item); 48 | 49 | void prepareBuffer(T item, byte fillByte); 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/cache/BaseByteBufferCache.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2021, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench.cache; 28 | 29 | import java.nio.ByteBuffer; 30 | import java.nio.LongBuffer; 31 | 32 | public abstract class BaseByteBufferCache extends LinkedListAllocationCache { 33 | 34 | @Override 35 | public byte[] copyOut(ByteBuffer item) { 36 | 37 | byte[] array = byteArrayOfSize(item.capacity()); 38 | item.get(array); 39 | 40 | return array; 41 | } 42 | 43 | @Override 44 | protected long copyIn(ByteBuffer item, byte fillByte) { 45 | 46 | for (int i = 0; i < item.capacity(); i++) { 47 | item.put(i, fillByte); 48 | } 49 | return fillByte; 50 | } 51 | 52 | @Override 53 | public int byteChecksum(ByteBuffer item) { 54 | 55 | if (item.remaining() != item.capacity()) { 56 | throw new RuntimeException("Remaining: " + item.remaining() + " < capacity: " + item.capacity()); 57 | } 58 | int sum = 0; 59 | while (item.remaining() > 0) { 60 | sum += item.get(); 61 | } 62 | return sum; 63 | } 64 | 65 | @Override 66 | public int longChecksum(ByteBuffer item) { 67 | if (item.remaining() != item.capacity()) { 68 | throw new RuntimeException("Remaining: " + item.remaining() + " < capacity: " + item.capacity()); 69 | } 70 | LongBuffer buffer = item.asLongBuffer(); 71 | long sum = 0; 72 | while (buffer.remaining() > 0) { 73 | sum += buffer.get(); 74 | } 75 | return (int)sum; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/cache/ByteArrayCache.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2021, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench.cache; 28 | 29 | import java.nio.ByteBuffer; 30 | import java.nio.LongBuffer; 31 | import java.util.Arrays; 32 | 33 | public class ByteArrayCache extends LinkedListAllocationCache { 34 | 35 | @Override 36 | byte[] allocate(int valueSize) { 37 | return new byte[valueSize]; 38 | } 39 | 40 | @Override 41 | void free(byte[] buffer) { 42 | //nothing to do 43 | } 44 | 45 | 46 | @Override 47 | public byte[] copyOut(byte[] item) { 48 | 49 | byte[] array = byteArrayOfSize(item.length); 50 | ByteBuffer target = ByteBuffer.wrap(array); 51 | target.put(item); 52 | 53 | return array; 54 | } 55 | 56 | @Override 57 | protected long copyIn(byte[] item, byte fillByte) { 58 | Arrays.fill(item, fillByte); 59 | return fillByte; 60 | } 61 | 62 | @Override 63 | public int byteChecksum(byte[] item) { 64 | 65 | int sum = 0; 66 | for (byte b : item) { 67 | sum += b; 68 | } 69 | 70 | return sum; 71 | } 72 | 73 | @Override 74 | public int longChecksum(byte[] item) { 75 | LongBuffer buffer = ByteBuffer.wrap(item).asLongBuffer(); 76 | long sum = 0; 77 | while (buffer.remaining() > 0) { 78 | sum += buffer.get(); 79 | } 80 | return (int)sum; 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/cache/DirectByteBufferCache.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2021, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench.cache; 28 | 29 | import com.evolvedbinary.jnibench.jmhbench.cache.BaseByteBufferCache; 30 | 31 | import java.nio.ByteBuffer; 32 | 33 | public class DirectByteBufferCache extends BaseByteBufferCache { 34 | 35 | @Override 36 | ByteBuffer allocate(int valueSize) { 37 | return ByteBuffer.allocateDirect(valueSize); 38 | } 39 | 40 | @Override 41 | void free(ByteBuffer buffer) { 42 | //we have no way to forcibly deallocate 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/cache/IndirectByteBufferCache.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2021, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench.cache; 28 | 29 | import com.evolvedbinary.jnibench.jmhbench.cache.BaseByteBufferCache; 30 | 31 | import java.nio.ByteBuffer; 32 | 33 | public class IndirectByteBufferCache extends BaseByteBufferCache { 34 | 35 | @Override 36 | ByteBuffer allocate(int valueSize) { 37 | return ByteBuffer.allocate(valueSize); 38 | } 39 | 40 | @Override 41 | void free(ByteBuffer buffer) { 42 | //automatically GC-ed 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/cache/LinkedListAllocationCache.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2021, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench.cache; 28 | 29 | import org.openjdk.jmh.infra.Blackhole; 30 | 31 | import java.util.HashMap; 32 | import java.util.LinkedList; 33 | import java.util.Map; 34 | 35 | public abstract class LinkedListAllocationCache implements AllocationCache { 36 | 37 | private Checksum checksum; 38 | private Prepare prepare; 39 | private Blackhole blackhole; 40 | 41 | @Override 42 | public final T acquire() { 43 | return cacheBuffers.removeFirst(); 44 | } 45 | 46 | @Override 47 | public final void release(T buffer) { 48 | cacheBuffers.addLast(buffer); 49 | } 50 | 51 | abstract T allocate(int valueSize); 52 | 53 | abstract void free(T buffer); 54 | 55 | // As many elements of valueSize as fit in cacheSize 56 | private LinkedList cacheBuffers = new LinkedList<>(); 57 | 58 | protected byte[] byteArrayOfSize(int size) { 59 | return copyOutCache.computeIfAbsent(size, k -> new byte[size]); 60 | } 61 | 62 | Map copyOutCache = new HashMap<>(); 63 | 64 | public void setup(int valueSize, int valueOverhead, int cacheSize, Checksum checksum, Blackhole blackhole) { 65 | this.checksum = checksum; 66 | this.prepare = Prepare.none; 67 | this.blackhole = blackhole; 68 | 69 | for (int totalBuffers = 0; totalBuffers < cacheSize; totalBuffers += valueSize + valueOverhead) 70 | { 71 | cacheBuffers.addLast(allocate(valueSize)); 72 | } 73 | } 74 | 75 | public void setup(int valueSize, int valueOverhead, int cacheSize, Prepare prepare, Blackhole blackhole) { 76 | this.prepare = prepare; 77 | this.checksum = Checksum.none; 78 | this.blackhole = blackhole; 79 | 80 | for (int totalBuffers = 0; totalBuffers < cacheSize; totalBuffers += valueSize + valueOverhead) 81 | { 82 | cacheBuffers.addLast(allocate(valueSize)); 83 | } 84 | } 85 | 86 | // Hope we have done enough to free direct BB memory. 87 | public void tearDown() { 88 | 89 | while (!cacheBuffers.isEmpty()) { 90 | T buffer = cacheBuffers.removeFirst(); 91 | free(buffer); 92 | } 93 | } 94 | 95 | abstract protected int byteChecksum(T item); 96 | 97 | abstract protected int longChecksum(T item); 98 | 99 | abstract protected byte[] copyOut(T item); 100 | 101 | abstract protected long copyIn(T item, byte fillByte); 102 | 103 | public void checksumBuffer(T item) { 104 | switch (checksum) { 105 | case copyout: 106 | blackhole.consume(this.copyOut(item)); 107 | break; 108 | case bytesum: 109 | blackhole.consume(this.byteChecksum(item)); 110 | break; 111 | case longsum: 112 | blackhole.consume(this.longChecksum(item)); 113 | break; 114 | case none: 115 | break; 116 | } 117 | } 118 | 119 | public void prepareBuffer(T item, byte fillByte) { 120 | switch(prepare) { 121 | case copyin: 122 | blackhole.consume(this.copyIn(item, fillByte)); 123 | break; 124 | case none: 125 | break; 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/cache/NettyByteBufCache.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2021, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench.cache; 28 | 29 | import com.evolvedbinary.jnibench.jmhbench.GetJNIBenchmark; 30 | import io.netty.buffer.ByteBuf; 31 | import io.netty.buffer.PooledByteBufAllocator; 32 | import io.netty.util.ByteProcessor; 33 | 34 | import java.util.logging.Logger; 35 | 36 | public class NettyByteBufCache extends LinkedListAllocationCache { 37 | 38 | private static final Logger LOG = Logger.getLogger(NettyByteBufCache.class.getName()); 39 | 40 | PooledByteBufAllocator allocator = PooledByteBufAllocator.DEFAULT; 41 | 42 | @Override 43 | protected long copyIn(ByteBuf byteBuf, byte fillByte) { 44 | 45 | for (int i = 0; i < byteBuf.capacity(); i++) { 46 | byteBuf.setByte(i, fillByte); 47 | } 48 | return fillByte; 49 | } 50 | 51 | @Override 52 | ByteBuf allocate(int valueSize) { 53 | ByteBuf byteBuf = allocator.directBuffer(valueSize); 54 | assert byteBuf.refCnt() == 1; 55 | 56 | return byteBuf; 57 | } 58 | 59 | @Override 60 | void free(ByteBuf byteBuf) { 61 | byteBuf.release(); 62 | } 63 | 64 | 65 | @Override 66 | public byte[] copyOut(ByteBuf byteBuf) { 67 | 68 | byte[] array = byteArrayOfSize(byteBuf.capacity()); 69 | byteBuf.forEachByte(new ByteCopyProcessor(array)); 70 | 71 | return array; 72 | } 73 | 74 | private static class ByteCopyProcessor implements ByteProcessor { 75 | 76 | final byte[] copy; 77 | int pos = 0; 78 | 79 | ByteCopyProcessor(byte[] copy) { 80 | this.copy = copy; 81 | } 82 | 83 | @Override 84 | public boolean process(byte b) { 85 | this.copy[pos++] = b; 86 | return true; 87 | } 88 | } 89 | 90 | private static class ByteSumProcessor implements ByteProcessor { 91 | 92 | int sum = 0; 93 | 94 | @Override 95 | public boolean process(byte b) { 96 | sum += b; 97 | return true; 98 | } 99 | } 100 | 101 | @Override 102 | public int byteChecksum(ByteBuf byteBuf) { 103 | 104 | ByteSumProcessor byteProcessor = new ByteSumProcessor(); 105 | 106 | byteBuf.forEachByte(byteProcessor); 107 | return byteProcessor.sum; 108 | } 109 | 110 | @Override 111 | public int longChecksum(ByteBuf byteBuf) { 112 | 113 | //TODO this is probably not optimal, but the checksum stuff is not core 114 | return byteChecksum(byteBuf); 115 | } 116 | } 117 | 118 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/cache/UnsafeBufferCache.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2021, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench.cache; 28 | 29 | import sun.misc.Unsafe; 30 | 31 | import java.lang.reflect.Field; 32 | 33 | public class UnsafeBufferCache extends LinkedListAllocationCache { 34 | 35 | private static Unsafe unsafe; 36 | 37 | static { 38 | try { 39 | Field f = Unsafe.class.getDeclaredField("theUnsafe"); 40 | f.setAccessible(true); 41 | unsafe = (Unsafe) f.get(null); 42 | } catch(Exception e) { 43 | throw new RuntimeException(e); 44 | } 45 | } 46 | 47 | @Override 48 | public int byteChecksum(UnsafeBuffer item) { 49 | int sum = 0; 50 | long handle = item.handle; 51 | for (int i = 0; i < item.size; i++) { 52 | sum += unsafe.getByte(handle++); 53 | } 54 | return sum; 55 | } 56 | 57 | @Override 58 | protected long copyIn(UnsafeBuffer item, byte fillByte) { 59 | 60 | long handle = item.handle; 61 | for (int i = 0; i < item.size; i++) { 62 | unsafe.putByte(handle++, fillByte); 63 | } 64 | return fillByte; 65 | } 66 | 67 | @Override 68 | public int longChecksum(UnsafeBuffer item) { 69 | long sum = 0; 70 | long handle = item.handle; 71 | for (int i = 0; i < item.size; i += Long.BYTES) { 72 | sum += unsafe.getLong(handle); 73 | handle += Long.BYTES; 74 | } 75 | return (int)sum; 76 | } 77 | 78 | @Override 79 | public byte[] copyOut(UnsafeBuffer item) { 80 | 81 | byte[] array = byteArrayOfSize((int)item.size); 82 | 83 | //Is there a faster/bulk way to do this ? 84 | long handle = item.handle; 85 | for (int i = 0; i < item.size; i++) { 86 | array[i] = unsafe.getByte(handle++); 87 | } 88 | 89 | return array; 90 | } 91 | 92 | public static class UnsafeBuffer { 93 | 94 | public long handle; 95 | public long size; 96 | 97 | public UnsafeBuffer(long handle, long size) { 98 | this.handle = handle; 99 | this.size = size; 100 | } 101 | 102 | public void reset() { 103 | this.handle = 0; 104 | this.size = 0; 105 | } 106 | } 107 | 108 | @Override 109 | UnsafeBuffer allocate(int bytes) { 110 | long handle = unsafe.allocateMemory(bytes); 111 | return new UnsafeBuffer(handle, bytes); 112 | } 113 | 114 | @Override 115 | void free(UnsafeBuffer buffer) { 116 | if (buffer.handle != 0 && buffer.size > 0) { 117 | unsafe.freeMemory(buffer.handle); 118 | buffer.reset(); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/com/evolvedbinary/jnibench/jmhbench/common/JMHCaller.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2021, Evolved Binary Ltd 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package com.evolvedbinary.jnibench.jmhbench.common; 28 | 29 | /** 30 | * Utility which uses assumptions about the JMH setup call stack 31 | * in order to figure out which benchmark class / method is being run. 32 | * The caller can use this information to guide the setup. 33 | */ 34 | public class JMHCaller { 35 | 36 | private final static int JMH_MAGIC = 3; 37 | 38 | public String benchmarkMethod; 39 | public String benchmarkClass; 40 | 41 | public static JMHCaller fromStack() { 42 | JMHCaller caller = new JMHCaller(); 43 | Exception e = new Exception(); 44 | StackTraceElement[] stack = e.getStackTrace(); 45 | assert stack.length > JMH_MAGIC; 46 | char dot = Character.toChars(0x2E)[0]; 47 | char backslash = Character.toChars(0x5C)[0]; 48 | String clazz = stack[JMH_MAGIC].getClassName(); 49 | String[] clazzPath = clazz.split(String.valueOf(backslash) + String.valueOf(dot)); 50 | String[] methodParts = stack[JMH_MAGIC].getMethodName().split(String.valueOf('_')); 51 | assert "jmh_generated".equals(clazzPath[clazzPath.length - 2]); 52 | String[] clazzParts = clazzPath[clazzPath.length - 1].split(String.valueOf('_')); 53 | caller.benchmarkClass = clazzParts[0]; 54 | caller.benchmarkMethod = clazzParts[1]; 55 | assert caller.benchmarkMethod.equals(methodParts[0]); 56 | 57 | return caller; 58 | } 59 | } 60 | --------------------------------------------------------------------------------