├── .gitignore ├── LICENSE ├── README.md ├── faster-than-reflection-benchmark ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── github │ └── javalbert │ ├── FasterThanReflectionBenchmark.java │ └── Foo.java ├── pom.xml └── src ├── main └── java │ └── com │ └── github │ └── javalbert │ ├── bytecode │ └── utils │ │ └── AsmUtils.java │ └── reflection │ ├── AccessClassLoader.java │ ├── ClassAccess.java │ ├── ClassAccessFactory.java │ ├── FieldAccess.java │ ├── MethodAccess.java │ └── PropertyAccess.java └── test └── java └── com └── github └── javalbert └── reflection └── test ├── ClassAccessCommonReferenceTypesTest.java ├── FieldAccessBoxedPrimitivesTest.java ├── FieldAccessGeneralTest.java ├── FieldAccessPrimitivesTest.java ├── Foo.java ├── FooFactory.java ├── Main.java ├── MethodAccessCallTest.java ├── MethodAccessInvokeTest.java ├── PropertyAccessBoxedPrimitivesTest.java ├── PropertyAccessGeneralTest.java └── PropertyAccessPrimitivesTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | /faster-than-reflection-benchmark/.settings/ 3 | /faster-than-reflection-benchmark/target/ 4 | /faster-than-reflection-benchmark/.classpath 5 | /faster-than-reflection-benchmark/.project 6 | /target/ 7 | /.classpath 8 | /.project 9 | /dependency-reduced-pom.xml 10 | /hs_err_pid* 11 | /replay_pid* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FasterThanReflection 2 | 3 | Mainly inspired by [ReflectASM library](https://github.com/EsotericSoftware/reflectasm) and a desire to provide faster performance in [SqlbuilderORM library](https://github.com/Javalbert/sql-builder-orm). Many ORMs use bytecode generation to generate classes that can set the fields of different entities, which is faster than using the Reflection API. 4 | 5 | ## Design Principles 6 | - Get and set fields directly (including private fields) 7 | - Get and set via JavaBean properties 8 | - Methods for getting and setting primitive fields or properties 9 | - Methods for getting and setting boxed versions of primitive fields or properties 10 | - Methods for getting and setting fields or properties of common classes e.g. String, Date, BigDecimal 11 | - 23 Methods for calling methods with 0 to 22 parameters (no expensive varargs creation) 12 | - Method that accepts Object varargs for calling any method 13 | - Not a reinvention of Reflection API (but faster field, property and method access) 14 | - Uses `new ClassWriter(0)` for best performance, instead of `new ClassWriter(ClassWriter.COMPUTE_MAXS)` (10% slower) or `new ClassWriter(ClassWriter.COMPUTE_FRAMES)` (2x slower) 15 | 16 | ## Installation 17 | 18 | Maven 19 | 20 | ```xml 21 | 22 | com.github.javalbert 23 | faster-than-reflection 24 | 1.0.0 25 | 26 | ``` 27 | 28 | ## Usage 29 | 30 | Retrieve an instance of ClassAccess. 31 | 32 | ```java 33 | ClassAccess classAccess = ClassAccessFactory.get(Foo.class); 34 | ``` 35 | 36 | ClassAccess interface extends interfaces: FieldAccess, PropertyAccess, and MethodAccess. 37 | 38 | ```java 39 | FieldAccess fieldAccess = ClassAccessFactory.get(Foo.class); 40 | PropertyAccess propertyAccess = ClassAccessFactory.get(Foo.class); 41 | MethodAccess methodAccess = ClassAccessFactory.get(Foo.class); 42 | ``` 43 | 44 | Get and set fields or properties 45 | 46 | ```java 47 | // Instance of a class whose fields or properties you want to get or set 48 | Foo foo = new Foo(); 49 | 50 | /* Fields */ 51 | 52 | int intValFieldIndex = fieldAccess.fieldIndex("intVal"); 53 | 54 | fieldAccess.getIntField(foo, intValFieldIndex); // i.e. foo.intVal 55 | fieldAccess.setIntField(foo, intValFieldIndex, 1); // i.e. foo.intVal = 1 56 | 57 | /* Properties */ 58 | 59 | int intValPropertyIndex = propertyAccess.propertyIndex("intVal"); 60 | 61 | propertyAccess.getIntProperty(foo, intValPropertyIndex); // i.e. foo.getIntVal() 62 | propertyAccess.setIntProperty(foo, intValPropertyIndex, 2); // i.e. foo.setIntVal(2); 63 | ``` 64 | 65 | Call methods 66 | 67 | ```java 68 | int methodIndex = methodAccess.methodIndex("newInstance"); 69 | 70 | // Call a method that has 2 parameters 71 | methodAccess.call(fooFactory, methodIndex, true, 1337); // i.e. fooFactory.newInstance(true, 1337) 72 | 73 | // Or use invoke for methods that have more than 22 parameters 74 | methodAccess.invoke(fooFactory, someCrazyMethod, (Object[])lotsOfVarargs); 75 | ``` 76 | 77 | ## Performance 78 | 79 | JMH benchmark code can be found in [faster-than-reflection-benchmark](faster-than-reflection-benchmark) folder in the root of this project. 80 | 81 | Test environment: Intel Core i7-4790k, Windows 10 x64 + High Performance Power Plan, Java version "1.8.0_121" Java Hotspot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode) 82 | 83 | | Benchmark | Mode | Samples | Score | Score error | Units | 84 | |---|---|---:|---:|---:|---| 85 | | testFieldAccessDirect | avgt | 200 | 0.686 | 0.001 | ns/op | 86 | | testFieldAccessFasterThanReflection | avgt | 200 | 1.028 | 0.002 | ns/op | 87 | | testFieldAccessReflectAsm | avgt | 200 | 0.912 | 0.001 | ns/op | 88 | | testFieldAccessReflectionApi | avgt | 200 | 2.901 | 0.002 | ns/op | 89 | | testPropertyAccessDirect | avgt | 200 | 0.685 | 0.001 | ns/op | 90 | | testPropertyAccessFasterThanReflection | avgt | 200 | 1.027 | 0.002 | ns/op | 91 | | testPropertyAccessReflectAsm | avgt | 200 | 1.301 | 0.025 | ns/op | 92 | | testPropertyAccessReflectionApi | avgt | 200 | 4.026 | 0.006 | ns/op | 93 | 96 | 97 | ## Contributing 98 | 99 | This is a one-man project but feel free to fork. 100 | 101 | ## Acknowledgments 102 | 103 | * [ReflectASM](https://github.com/EsotericSoftware/reflectasm), main inspiration, AccessClassLoader.java 104 | * dimzon's [fork](https://github.com/dimzon/reflectasm) of ReflectASM, where I figured out how his changes allow access to private class members (which is a limitation of ReflectASM) -------------------------------------------------------------------------------- /faster-than-reflection-benchmark/pom.xml: -------------------------------------------------------------------------------- 1 | 13 | 15 | 4.0.0 16 | 17 | com.github.javalbert 18 | faster-than-reflection-benchmark 19 | 0.1.0 20 | jar 21 | 22 | FasterThanReflection JMH benchmark 23 | 24 | 25 | 3.0 26 | 27 | 28 | 29 | 30 | org.openjdk.jmh 31 | jmh-core 32 | ${jmh.version} 33 | 34 | 35 | org.openjdk.jmh 36 | jmh-generator-annprocess 37 | ${jmh.version} 38 | provided 39 | 40 | 41 | com.esotericsoftware 42 | reflectasm 43 | ${reflectasm.version} 44 | shaded 45 | 46 | 47 | org.ow2.asm 48 | asm 49 | 50 | 51 | 52 | 53 | com.github.javalbert 54 | faster-than-reflection 55 | ${faster-than-reflection.version} 56 | 57 | 58 | org.slf4j 59 | slf4j-simple 60 | ${slf4j.version} 61 | 62 | 63 | 64 | 65 | UTF-8 66 | 1.0 67 | 1.8 68 | benchmarks 69 | 1.0.0 70 | 1.11.3 71 | 1.7.24 72 | 73 | 74 | 75 | 76 | 77 | org.apache.maven.plugins 78 | maven-compiler-plugin 79 | 3.1 80 | 81 | ${javac.target} 82 | ${javac.target} 83 | ${javac.target} 84 | 85 | 86 | 87 | org.apache.maven.plugins 88 | maven-shade-plugin 89 | 2.2 90 | 91 | 92 | package 93 | 94 | shade 95 | 96 | 97 | ${uberjar.name} 98 | 99 | 100 | org.openjdk.jmh.Main 101 | 102 | 103 | 104 | 105 | 109 | *:* 110 | 111 | META-INF/*.SF 112 | META-INF/*.DSA 113 | META-INF/*.RSA 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | maven-clean-plugin 126 | 2.5 127 | 128 | 129 | maven-deploy-plugin 130 | 2.8.1 131 | 132 | 133 | maven-install-plugin 134 | 2.5.1 135 | 136 | 137 | maven-jar-plugin 138 | 2.4 139 | 140 | 141 | maven-javadoc-plugin 142 | 2.9.1 143 | 144 | 145 | maven-resources-plugin 146 | 2.6 147 | 148 | 149 | maven-site-plugin 150 | 3.3 151 | 152 | 153 | maven-source-plugin 154 | 2.2.1 155 | 156 | 157 | maven-surefire-plugin 158 | 2.17 159 | 160 | 161 | 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /faster-than-reflection-benchmark/src/main/java/com/github/javalbert/FasterThanReflectionBenchmark.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright 2017 Albert Shun-Dat Chan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | *******************************************************************************/ 13 | package com.github.javalbert; 14 | 15 | import java.lang.reflect.Field; 16 | import java.lang.reflect.InvocationTargetException; 17 | import java.lang.reflect.Method; 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import org.openjdk.jmh.annotations.Benchmark; 21 | import org.openjdk.jmh.annotations.BenchmarkMode; 22 | import org.openjdk.jmh.annotations.Level; 23 | import org.openjdk.jmh.annotations.Mode; 24 | import org.openjdk.jmh.annotations.OutputTimeUnit; 25 | import org.openjdk.jmh.annotations.Scope; 26 | import org.openjdk.jmh.annotations.Setup; 27 | import org.openjdk.jmh.annotations.State; 28 | 29 | import com.github.javalbert.reflection.ClassAccessFactory; 30 | import com.github.javalbert.reflection.FieldAccess; 31 | import com.github.javalbert.reflection.PropertyAccess; 32 | 33 | public class FasterThanReflectionBenchmark { 34 | /* START Field access */ 35 | 36 | @State(Scope.Thread) 37 | public static class FieldAccessDirectState { 38 | public Foo foo = new Foo(); 39 | } 40 | 41 | @State(Scope.Thread) 42 | public static class FieldAccessFasterThanReflectionState { 43 | public Foo foo = new Foo(); 44 | public FieldAccess fieldAccess; 45 | public int fieldIndex; 46 | 47 | @Setup(Level.Trial) 48 | public void doSetup() { 49 | fieldAccess = ClassAccessFactory.get(Foo.class); 50 | fieldIndex = fieldAccess.fieldIndex("intVal"); 51 | } 52 | } 53 | 54 | @State(Scope.Thread) 55 | public static class FieldAccessReflectAsmState { 56 | public Foo foo = new Foo(); 57 | public com.esotericsoftware.reflectasm.FieldAccess fieldAccess; 58 | public int fieldIndex; 59 | 60 | @Setup(Level.Trial) 61 | public void doSetup() { 62 | fieldAccess = com.esotericsoftware.reflectasm.FieldAccess.get(Foo.class); 63 | fieldIndex = fieldAccess.getIndex("intVal"); 64 | } 65 | } 66 | 67 | @State(Scope.Thread) 68 | public static class FieldAccessReflectionApiState { 69 | public Foo foo = new Foo(); 70 | public Field intValField; 71 | 72 | @Setup(Level.Trial) 73 | public void doSetup() { 74 | try { 75 | intValField = Foo.class.getDeclaredField("intVal"); 76 | intValField.setAccessible(true); 77 | } catch (NoSuchFieldException | SecurityException e) { 78 | throw new RuntimeException(e); 79 | } 80 | } 81 | } 82 | 83 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 84 | @BenchmarkMode(Mode.AverageTime) 85 | @Benchmark 86 | public int testFieldAccessDirect(FieldAccessDirectState state) { 87 | return state.foo.intVal; 88 | } 89 | 90 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 91 | @BenchmarkMode(Mode.AverageTime) 92 | @Benchmark 93 | public int testFieldAccessFasterThanReflection(FieldAccessFasterThanReflectionState state) { 94 | return state.fieldAccess.getIntField(state.foo, state.fieldIndex); 95 | } 96 | 97 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 98 | @BenchmarkMode(Mode.AverageTime) 99 | @Benchmark 100 | public int testFieldAccessReflectAsm(FieldAccessReflectAsmState state) { 101 | return state.fieldAccess.getInt(state.foo, state.fieldIndex); 102 | } 103 | 104 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 105 | @BenchmarkMode(Mode.AverageTime) 106 | @Benchmark 107 | public int testFieldAccessReflectionApi(FieldAccessReflectionApiState state) 108 | throws IllegalArgumentException, IllegalAccessException { 109 | return state.intValField.getInt(state.foo); 110 | } 111 | 112 | /* END Field access */ 113 | 114 | /* START Property access */ 115 | 116 | @State(Scope.Thread) 117 | public static class PropertyAccessDirectState { 118 | public Foo foo = new Foo(); 119 | } 120 | 121 | @State(Scope.Thread) 122 | public static class PropertyAccessFasterThanReflectionState { 123 | public Foo foo = new Foo(); 124 | public PropertyAccess propertyAccess; 125 | public int propertyIndex; 126 | 127 | @Setup(Level.Trial) 128 | public void doSetup() { 129 | propertyAccess = ClassAccessFactory.get(Foo.class); 130 | propertyIndex = propertyAccess.propertyIndex("intVal"); 131 | } 132 | } 133 | 134 | @State(Scope.Thread) 135 | public static class PropertyAccessReflectAsmState { 136 | public Foo foo = new Foo(); 137 | public com.esotericsoftware.reflectasm.MethodAccess methodAccess; 138 | public int methodIndex; 139 | 140 | @Setup(Level.Trial) 141 | public void doSetup() { 142 | methodAccess = com.esotericsoftware.reflectasm.MethodAccess.get(Foo.class); 143 | methodIndex = methodAccess.getIndex("getIntVal"); 144 | } 145 | } 146 | 147 | @State(Scope.Thread) 148 | public static class PropertyAccessReflectionApiState { 149 | public Foo foo = new Foo(); 150 | public Method intValAccessorMethod; 151 | 152 | @Setup(Level.Trial) 153 | public void doSetup() { 154 | try { 155 | intValAccessorMethod = Foo.class.getDeclaredMethod("getIntVal"); 156 | intValAccessorMethod.setAccessible(true); 157 | } catch (NoSuchMethodException | SecurityException e) { 158 | throw new RuntimeException(e); 159 | } 160 | } 161 | } 162 | 163 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 164 | @BenchmarkMode(Mode.AverageTime) 165 | @Benchmark 166 | public int testPropertyAccessDirect(PropertyAccessDirectState state) { 167 | return state.foo.getIntVal(); 168 | } 169 | 170 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 171 | @BenchmarkMode(Mode.AverageTime) 172 | @Benchmark 173 | public int testPropertyAccessFasterThanReflection(PropertyAccessFasterThanReflectionState state) { 174 | return state.propertyAccess.getIntProperty(state.foo, state.propertyIndex); 175 | } 176 | 177 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 178 | @BenchmarkMode(Mode.AverageTime) 179 | @Benchmark 180 | public int testPropertyAccessReflectAsm(PropertyAccessReflectAsmState state) { 181 | return (int)state.methodAccess.invoke(state.foo, state.methodIndex); 182 | } 183 | 184 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 185 | @BenchmarkMode(Mode.AverageTime) 186 | @Benchmark 187 | public int testPropertyAccessReflectionApi(PropertyAccessReflectionApiState state) 188 | throws IllegalAccessException, InvocationTargetException { 189 | return (int)state.intValAccessorMethod.invoke(state.foo); 190 | } 191 | 192 | /* END Property access */ 193 | } 194 | -------------------------------------------------------------------------------- /faster-than-reflection-benchmark/src/main/java/com/github/javalbert/Foo.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright 2017 Albert Shun-Dat Chan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | *******************************************************************************/ 13 | package com.github.javalbert; 14 | 15 | public class Foo { 16 | protected int intVal; 17 | 18 | public int getIntVal() { 19 | return intVal; 20 | } 21 | 22 | public void setIntVal(int intVal) { 23 | this.intVal = intVal; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 13 | 16 | 4.0.0 17 | com.github.javalbert 18 | faster-than-reflection 19 | 1.1.0-SNAPSHOT 20 | 21 | 22 | 23 | Apache License 2.0 24 | https://opensource.org/licenses/Apache-2.0 25 | 26 | 27 | 28 | FasterThanReflection 29 | High performance field, property, and method access using bytecode generation 30 | https://github.com/javalbert/faster-than-reflection 31 | 32 | 33 | 34 | UTF-8 35 | 36 | 5.2 37 | 3.5 38 | 1.8 39 | 3.6.0 40 | 1.6 41 | 2.10.4 42 | 2.4.3 43 | 3.0.1 44 | 1.7.22 45 | 46 | 47 | 48 | 0.60 49 | 0.7.8 50 | 4.12 51 | 1.10.19 52 | 2.19.1 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.apache.commons 62 | commons-lang3 63 | ${commons-lang.version} 64 | 65 | 66 | 67 | org.ow2.asm 68 | asm 69 | ${asm.version} 70 | 71 | 72 | 73 | org.slf4j 74 | slf4j-api 75 | ${slf4j.version} 76 | 77 | 78 | 79 | 80 | 81 | junit 82 | junit 83 | ${junit.version} 84 | test 85 | 86 | 87 | 88 | org.mockito 89 | mockito-all 90 | ${mockito.version} 91 | test 92 | 93 | 94 | 95 | org.slf4j 96 | slf4j-simple 97 | ${slf4j.version} 98 | test 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | maven-compiler-plugin 110 | ${maven-compiler.version} 111 | 112 | ${jdk.version} 113 | ${jdk.version} 114 | 115 | 116 | 117 | 118 | org.apache.maven.plugins 119 | maven-shade-plugin 120 | ${maven-shade.version} 121 | 122 | 123 | package 124 | 125 | shade 126 | 127 | 128 | 129 | 130 | org.objectweb.asm 131 | com.github.javalbert.asm 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | org.apache.maven.plugins 141 | maven-surefire-plugin 142 | ${surefire-plugin.version} 143 | 144 | false 145 | 146 | **/*Test.java 147 | 148 | 149 | 150 | 151 | 152 | org.jacoco 153 | jacoco-maven-plugin 154 | ${jacoco.version} 155 | 156 | 157 | default-prepare-agent 158 | 159 | prepare-agent 160 | 161 | 162 | 163 | default-report 164 | prepare-package 165 | 166 | report 167 | 168 | 169 | 170 | default-check 171 | 172 | check 173 | 174 | 175 | 176 | 177 | BUNDLE 178 | 179 | 180 | COMPLEXITY 181 | COVEREDRATIO 182 | ${jacoco.coveredratio} 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | org.apache.maven.plugins 194 | maven-source-plugin 195 | ${maven-source.version} 196 | 197 | 198 | attach-sources 199 | 200 | jar-no-fork 201 | 202 | 203 | 204 | 205 | 206 | org.apache.maven.plugins 207 | maven-javadoc-plugin 208 | ${maven-javadoc.version} 209 | 210 | 211 | attach-javadocs 212 | 213 | jar 214 | 215 | 216 | 217 | 218 | 219 | org.apache.maven.plugins 220 | maven-gpg-plugin 221 | ${maven-gpg.version} 222 | 223 | 224 | sign-artifacts 225 | verify 226 | 227 | sign 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | https://github.com/javalbert/faster-than-reflection/tree/master 238 | scm:git:git://github.com/javalbert/faster-than-reflection.git 239 | scm:git:ssh://github.com:javalbert/faster-than-reflection.git 240 | 241 | 242 | 243 | 244 | Albert Shun-Dat Chan 245 | albert.shun.dat.chan@gmail.com 246 | Albert Shun-Dat Chan 247 | https://github.com/javalbert 248 | 249 | 250 | 251 | 252 | 253 | ossrh 254 | https://oss.sonatype.org/content/repositories/snapshots 255 | 256 | 257 | ossrh 258 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 259 | 260 | 261 | -------------------------------------------------------------------------------- /src/main/java/com/github/javalbert/bytecode/utils/AsmUtils.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright 2017 Albert Shun-Dat Chan 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in 5 | * compliance with the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is 10 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See 11 | * the License for the specific language governing permissions and limitations under the License. 12 | *******************************************************************************/ 13 | package com.github.javalbert.bytecode.utils; 14 | 15 | import static org.objectweb.asm.Opcodes.*; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | import org.objectweb.asm.ClassWriter; 21 | import org.objectweb.asm.Label; 22 | import org.objectweb.asm.MethodVisitor; 23 | 24 | public final class AsmUtils { 25 | /** 26 | * 27 | * @param defaultCaseLabel 28 | * @param cases should be a sorted int array 29 | * @return an array of {@link Label}s when calling {@link MethodVisitor#visitTableSwitchInsn(int, int, Label, Label...)} 30 | */ 31 | public static Label[] getTableSwitchLabels( 32 | Label defaultCaseLabel, 33 | int[] cases) { 34 | List