|
| 1 | +package com.mapk.fastkfunction |
| 2 | + |
| 3 | +import com.mapk.fastkfunction.argumentbucket.ArgumentBucket |
| 4 | +import com.mapk.fastkfunction.benchmarktargets.Constructor5 |
| 5 | +import com.mapk.fastkfunction.benchmarktargets.topLevelExtensionFun5 |
| 6 | +import org.openjdk.jmh.annotations.Benchmark |
| 7 | +import org.openjdk.jmh.annotations.Scope |
| 8 | +import org.openjdk.jmh.annotations.State |
| 9 | +import java.lang.reflect.Method |
| 10 | +import kotlin.reflect.KFunction |
| 11 | +import kotlin.reflect.KParameter |
| 12 | +import kotlin.reflect.jvm.javaMethod |
| 13 | + |
| 14 | +@State(Scope.Benchmark) |
| 15 | +open class CallTopLevelExtensionFunBenchmark { |
| 16 | + private val receiverInstance = Constructor5(1, 2, 3, 4, 5) |
| 17 | + |
| 18 | + private val functionByMethodReference: KFunction<Constructor5> = receiverInstance::topLevelExtensionFun5 |
| 19 | + private val functionFromClass: KFunction<Constructor5> = Constructor5::topLevelExtensionFun5 |
| 20 | + |
| 21 | + private val argumentMap: Map<KParameter, Any?> = functionByMethodReference.parameters.associateWith { it.index + 1 } |
| 22 | + |
| 23 | + private val javaMethod: Method = functionByMethodReference.javaMethod!! |
| 24 | + |
| 25 | + private val fastKFunctionByMethodReferenceWithoutInstance: FastKFunction<Constructor5> = |
| 26 | + FastKFunction(functionByMethodReference) |
| 27 | + private val fastKFunctionByMethodReferenceWithInstance: FastKFunction<Constructor5> = |
| 28 | + FastKFunction(functionByMethodReference, receiverInstance) |
| 29 | + |
| 30 | + private val fastKFunctionFromClass: FastKFunction<Constructor5> = FastKFunction(functionFromClass, receiverInstance) |
| 31 | + |
| 32 | + private val argumentBucket: ArgumentBucket = fastKFunctionByMethodReferenceWithoutInstance.generateBucket() |
| 33 | + .apply { (0 until 5).forEach { this[it] = it + 1 } } |
| 34 | + |
| 35 | + @Benchmark |
| 36 | + fun normalCall(): Constructor5 = receiverInstance.topLevelExtensionFun5(1, 2, 3, 4, 5) |
| 37 | + |
| 38 | + @Benchmark |
| 39 | + fun functionByMethodReferenceCall(): Constructor5 = functionByMethodReference.call(1, 2, 3, 4, 5) |
| 40 | + |
| 41 | + @Benchmark |
| 42 | + fun functionByMethodReferenceCallBy(): Constructor5 = functionByMethodReference.callBy(argumentMap) |
| 43 | + |
| 44 | + @Benchmark |
| 45 | + fun functionFromClassCall(): Constructor5 = functionFromClass.call(1, 2, 3, 4, 5) |
| 46 | + |
| 47 | + @Benchmark |
| 48 | + fun functionFromClassCallBy(): Constructor5 = functionFromClass.callBy(argumentMap) |
| 49 | + |
| 50 | + @Benchmark |
| 51 | + fun javaMethod(): Constructor5 = javaMethod.invoke(null, receiverInstance, 1, 2, 3, 4, 5) as Constructor5 |
| 52 | + |
| 53 | + @Benchmark |
| 54 | + fun fastKFunctionByMethodReferenceWithoutInstanceCall(): Constructor5 = |
| 55 | + fastKFunctionByMethodReferenceWithoutInstance.call(1, 2, 3, 4, 5) |
| 56 | + |
| 57 | + @Benchmark |
| 58 | + fun fastKFunctionByMethodReferenceWithoutInstanceCallBy(): Constructor5 = |
| 59 | + fastKFunctionByMethodReferenceWithoutInstance.callBy(argumentBucket) |
| 60 | + |
| 61 | + @Benchmark |
| 62 | + fun fastKFunctionByMethodReferenceWithInstanceCall(): Constructor5 = |
| 63 | + fastKFunctionByMethodReferenceWithInstance.call(1, 2, 3, 4, 5) |
| 64 | + |
| 65 | + @Benchmark |
| 66 | + fun fastKFunctionByMethodReferenceWithInstanceCallBy(): Constructor5 = |
| 67 | + fastKFunctionByMethodReferenceWithInstance.callBy(argumentBucket) |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + fun fastKFunctionFromClassCall(): Constructor5 = |
| 71 | + fastKFunctionFromClass.call(1, 2, 3, 4, 5) |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + fun fastKFunctionFromClassCallBy(): Constructor5 = |
| 75 | + fastKFunctionFromClass.callBy(argumentBucket) |
| 76 | +} |
0 commit comments