@@ -37,35 +37,43 @@ val result: Sample = fastKFunction.generateBucket()
37
37
```
38
38
39
39
## How Fast?
40
- Calling the constructor is more than 1.2 times faster than calling ` KFunction ` with ` call ` ,
40
+ Calling the ` constructor ` is more than 1.2 times faster than calling ` KFunction ` with ` call ` ,
41
41
and more than 6 times faster than calling it with ` callBy ` .
42
42
43
43
You can get the same speed as ` reflection ` in ` Java ` .
44
44
45
- | | ops/s | Ratio|
46
- | -----------------------| ------------:| -----|
47
- | Java Constructor | ` 104267558.4 ` | ` 6.7 ` |
48
- | FastKFunction(call) | ` 102948283.4 ` | ` 6.6 ` |
49
- | FastKFunction(callBy) | ` 105609306.2 ` | ` 6.8 ` |
50
- | KFunction(call) | ` 77096714.2 ` | ` 5.0 ` |
51
- | KFunction(callBy) | ` 15519730.2 ` | ` 1 ` |
45
+ | | ops/s | Ratio|
46
+ | --------------------------- | ------------:| -----|
47
+ | ** Java Constructor** | ` 104267558.4 ` | ` 6.7 ` |
48
+ | ** FastKFunction(call)** | ` 102948283.4 ` | ` 6.6 ` |
49
+ | ** FastKFunction(callBy)** | ` 105609306.2 ` | ` 6.8 ` |
50
+ | ** KFunction(call)** | ` 77096714.2 ` | ` 5.0 ` |
51
+ | ** KFunction(callBy)** | ` 15519730.2 ` | ` 1 ` |
52
52
53
53
![ ConstructorBenchmarkResultGraph.png] ( ./pictures/ConstructorBenchmarkResultGraph.png )
54
54
55
- * This is the score I got on a ` Ryzen7 3700X ` in a ` Windows 10 ` environment, with [ 3b8687] ( https://github.com/ProjectMapK/FastKFunction/tree/3b8687da712319a49e4f58a38edbb016cc0c41b7 ) committed.
55
+ _ This score was measured with ` Ryzen7 3700X ` , ` Windows10 ` , [ 3b8687] ( https://github.com/ProjectMapK/FastKFunction/tree/3b8687da712319a49e4f58a38edbb016cc0c41b7 ) committed code and benchmark settings._
56
+ _ It is currently a little faster with small improvements._
56
57
57
58
### Raw data, and other comparisons
58
59
You can get full benchmark score and some other graphs [ here] ( https://docs.google.com/spreadsheets/d/1DJhf8KX1-BAxCGor5cZdlO3626AZbKeet-rkk26XGAE/ ) .
59
60
60
61
### Mechanism
62
+ ` FastKFunction ` realizes high speed by the following ingenuity.
63
+
64
+ - Call ` KFunction ` with ` call ` if the argument is fully initialized.
65
+ - If possible, call ` Java ` ` Method ` or ` Constructor ` directly for further speedup.
66
+ - Efficient retention of arguments and switching between ` call ` /` callBy ` calls by devising a data structure.
67
+ - Avoid using ` spread operator ` as much as possible.
68
+
61
69
I have a blog post on the mechanism of fast invocation (in Japanese).
62
70
63
71
- [ 【Kotlin】KFunctionを高速に呼び出す(前編) \- Qiita] ( https://qiita.com/wrongwrong/items/f7b15d54956191f471d1 )
64
72
- [ 【Kotlin】KFunctionを高速に呼び出す(後編) \- Qiita] ( https://qiita.com/wrongwrong/items/fe75bae3911eff319e68 )
65
73
66
74
### Benchmarking
67
75
You can run the benchmark with the ` ./gradlew jmh ` .
68
- Please note that it will take about 5 hours in total if executed with the default settings .
76
+ It takes about 45 minutes to run a complete benchmark .
69
77
70
78
``` bash
71
79
./gradlew jmh
@@ -78,46 +86,30 @@ Please see here for the introduction method.
78
86
79
87
- [ ProjectMapK / FastKFunction] ( https://jitpack.io/#ProjectMapK/FastKFunction )
80
88
81
- ## How to use FastKFunction.
89
+ ## How to use FastKFunction
82
90
83
- ### Instance parameter.
84
- If you call an instance function, you can expect a faster call with ` instance parameter ` .
85
-
86
- ``` kotlin
87
- data class Sample (
88
- val arg1 : Int ,
89
- val arg2 : Int
90
- ) {
91
- fun instanceFun (arg3 : Int ): Int = arg1 + arg2 + arg3
92
- }
91
+ ### Initialization
92
+ In some cases, ` instance parameter ` is required to initialize ` FastKFunction ` .
93
+ Even if the ` instance parameter ` is not required, passing it may speed up the process.
93
94
94
- val sample = Sample ( 1 , 2 )
95
+ The following is the correspondence table.
95
96
96
- val fastKFunction = FastKFunction .of(sample::instanceFun, sample)
97
- ```
97
+ | | instance parameter | description |
98
+ | :-----------------------------------:| :------------------:| ------------------------------------------------------------|
99
+ | ** Constructor** | Unnecessary | |
100
+ | ** Top level function** | Unnecessary | |
101
+ | ** Method reference from instance** | Optional | Passing the ` instance parameter ` will speed up the call. |
102
+ | ** Function defined for the object** | Optional | Passing ` instance parameter ` will speed up initialization. |
103
+ | ** Top level extension function** | Required | |
104
+ | ** Method reference from class** | Required | |
98
105
99
- Depending on how you get the ` KFunction ` , the ` instance parameter ` may be required.
100
- Even if the ` instance parameter ` is not required, passing an ` instance parameter ` will make the call faster.
106
+ Calling the ` constructor ` of an ` inner class ` or an ` extension function ` defined in an ` instance ` is currently not supported.
101
107
102
- ### How to call.
108
+ ### How to call
103
109
` FastKFunction ` supports two major types of calls.
104
110
105
- #### Call by vararg or Collection.
106
- Calling with ` vararg ` or ` Collection ` is faster if you don't need to use the default arguments and
107
- can get them in the order in which they are defined.
108
-
109
- ``` kotlin
110
- val fastKFunction: FastKFunction <Sample > = FastKFunction .of(function)
111
-
112
- // call by vararg
113
- val result: Sample = fastKFunction.call(1 , 2 , 3 , 4 , 5 )
114
-
115
- // call by Collection
116
- val result: Sample = fastKFunction.callByCollection(listOf (1 , 2 , 3 , 4 , 5 ))
117
- ```
118
-
119
- #### Call by ArgumentBucket.
120
- If the default argument is expected to be used, a call using ` ArgumentBucket ` is available.
111
+ #### Call by ArgumentBucket
112
+ If the ` default argument ` is expected to be used, a call using ` ArgumentBucket ` is available.
121
113
122
114
` ArgumentBucket ` has interfaces like ` MutableMap<KParameter, Any?> ` , which can be used, for example, as follows.
123
115
@@ -140,7 +132,21 @@ fun map(src: Map<String, Any?>): Sample {
140
132
}
141
133
```
142
134
143
- ### For functions that can be called from a single argument.
135
+ #### Call by vararg or Collection
136
+ Calling with ` vararg ` or ` Collection ` is faster if you don't need to use the ` default arguments ` and
137
+ can get them in the order in which they are defined.
138
+
139
+ ``` kotlin
140
+ val fastKFunction: FastKFunction <Sample > = FastKFunction .of(function)
141
+
142
+ // call by vararg
143
+ val result: Sample = fastKFunction.call(1 , 2 , 3 , 4 , 5 )
144
+
145
+ // call by Collection
146
+ val result: Sample = fastKFunction.callByCollection(listOf (1 , 2 , 3 , 4 , 5 ))
147
+ ```
148
+
149
+ ### For functions that can be called from a single argument
144
150
For a function that can be called with a single argument, you can use the ` SingleArgFastKFunction ` .
145
151
146
152
``` kotlin
0 commit comments