Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance by invoke KFunction by "call". #408

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
Expand Down Expand Up @@ -167,6 +166,10 @@
<configuration>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<includes>
<include>${project.basedir}/src/main/kotlin</include>
<include>${project.basedir}/src/main/java</include>
</includes>
</configuration>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fasterxml.jackson.module.kotlin;

import kotlin.reflect.KFunction;

class SpreadWrapper {
// Wrapper to avoid costly calls using spread operator.
static <T> T call(KFunction<T> function, Object[] args) {
return function.call(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.fasterxml.jackson.module.kotlin

import java.util.AbstractMap.SimpleEntry as Entry
import kotlin.reflect.KParameter

internal class ArgumentBucket(private val parameters: List<KParameter>) : Map<KParameter, Any?> {
// This array is sorted by KParameter::index.
val valueArray: Array<Any?> = arrayOfNulls(parameters.size)
private val initializationStatuses: BooleanArray = BooleanArray(parameters.size)
private var count = 0

operator fun set(key: KParameter, value: Any?): Any? {
return valueArray[key.index].apply {
valueArray[key.index] = value

if (!initializationStatuses[key.index]) {
initializationStatuses[key.index] = true
count++
}
}
}

fun isFullInitialized(): Boolean = parameters.size == count

override val entries: Set<Map.Entry<KParameter, Any?>>
get() = valueArray.withIndex()
.filter { (i, _) -> initializationStatuses[i] }
.map { (i, arg) -> Entry(parameters[i], arg) }
.toSet()
override val keys: Set<KParameter>
get() = parameters
.filterIndexed { i, _ -> initializationStatuses[i] }
.toSet()
override val size: Int
get() = count
override val values: Collection<Any?>
get() = valueArray.filterIndexed { i, _ -> initializationStatuses[i] }

override fun containsKey(key: KParameter): Boolean = initializationStatuses[key.index]

override fun containsValue(value: Any?): Boolean = valueArray.withIndex()
.any { (i, arg) -> initializationStatuses[i] && value == arg }

override fun get(key: KParameter): Any? = valueArray[key.index]

override fun isEmpty(): Boolean = count == 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,18 @@ internal class KotlinValueInstantiator(
) {
callable.isAccessible = true
}
val callableParametersByName = linkedMapOf<KParameter, Any?>()
callableParameters.mapIndexed { idx, paramDef ->
if (paramDef != null) {
callableParametersByName[paramDef] = jsonParamValueList[idx]
ArgumentBucket(callable.parameters).apply {
callableParameters.forEachIndexed { idx, paramDef ->
if (paramDef != null) {
this[paramDef] = jsonParamValueList[idx]
}
}
}.let {
if (it.isFullInitialized())
SpreadWrapper.call(callable, it.valueArray)
else
callable.callBy(it)
}
callable.callBy(callableParametersByName)
}

}
Expand Down