Improve KotlinJsonAdapter performance by invoke KFunction by "call". (#1286)

This commit is contained in:
wrongwrong
2021-03-30 09:46:42 +09:00
committed by GitHub
parent bcfce60577
commit 37f34e16f0

View File

@@ -94,21 +94,27 @@ internal class KotlinJsonAdapter<T>(
reader.endObject() reader.endObject()
// Confirm all parameters are present, optional, or nullable. // Confirm all parameters are present, optional, or nullable.
var isFullInitialized = allBindings.size == constructorSize
for (i in 0 until constructorSize) { for (i in 0 until constructorSize) {
if (values[i] === ABSENT_VALUE && !constructor.parameters[i].isOptional) { if (values[i] === ABSENT_VALUE) {
if (!constructor.parameters[i].type.isMarkedNullable) { when {
throw Util.missingProperty( constructor.parameters[i].isOptional -> isFullInitialized = false
constructor.parameters[i].type.isMarkedNullable -> values[i] = null // Replace absent with null.
else -> throw Util.missingProperty(
constructor.parameters[i].name, constructor.parameters[i].name,
allBindings[i]?.jsonName, allBindings[i]?.jsonName,
reader reader
) )
} }
values[i] = null // Replace absent with null.
} }
} }
// Call the constructor using a Map so that absent optionals get defaults. // Call the constructor using a Map so that absent optionals get defaults.
val result = constructor.callBy(IndexedParameterMap(constructor.parameters, values)) val result = if (isFullInitialized) {
constructor.call(*values)
} else {
constructor.callBy(IndexedParameterMap(constructor.parameters, values))
}
// Set remaining properties. // Set remaining properties.
for (i in constructorSize until allBindings.size) { for (i in constructorSize until allBindings.size) {