Skip to content

Commit 6883c16

Browse files
authored
Merge pull request #740 from k163377/reduce-cache
Reduce conversion cache from Executable to KFunction
2 parents b917694 + a8b3adc commit 6883c16

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

release-notes/CREDITS-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Contributors:
1818
# 2.17.0 (not yet released)
1919

2020
WrongWrong (@k163377)
21+
* #740: Reduce conversion cache from Executable to KFunction.
2122
* #738: Fix JacksonInject priority.
2223
* #732: SequenceSerializer removed.
2324
* #727: Fixed overriding findCreatorAnnotation instead of hasCreatorAnnotation

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Co-maintainers:
1818

1919
2.17.0 (not yet released)
2020

21+
#740: Reduce conversion cache from Executable to KFunction.
22+
This will reduce memory usage efficiency and total memory consumption, but may result in a minor performance degradation in use cases where a large number of factory functions are used as JsonCreator.
2123
#738: JacksonInject is now preferred over the default argument(fixes #722).
2224
#732: SequenceSerializer removed.
2325
#727: Fixed overriding findCreatorAnnotation instead of hasCreatorAnnotation.

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ internal class KotlinAnnotationIntrospector(
9595
if (!useJavaDurationConversion) return null
9696

9797
return (a as? AnnotatedParameter)?.let { param ->
98-
@Suppress("UNCHECKED_CAST")
9998
val function: KFunction<*> = when (val owner = param.owner.member) {
100-
is Constructor<*> -> cache.kotlinFromJava(owner as Constructor<Any>)
99+
is Constructor<*> -> cache.kotlinFromJava(owner)
101100
is Method -> cache.kotlinFromJava(owner)
102101
else -> null
103102
} ?: return@let null

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,12 @@ internal class KotlinNamesAnnotationIntrospector(
7979
}
8080
}
8181

82-
@Suppress("UNCHECKED_CAST")
8382
private fun hasCreatorAnnotation(member: AnnotatedConstructor): Boolean {
8483
// don't add a JsonCreator to any constructor if one is declared already
8584

8685
val kClass = member.declaringClass.kotlin
8786
.apply { if (this in ignoredClassesForImplyingJsonCreator) return false }
88-
val kConstructor = cache.kotlinFromJava(member.annotated as Constructor<Any>) ?: return false
87+
val kConstructor = cache.kotlinFromJava(member.annotated) ?: return false
8988

9089
// TODO: should we do this check or not? It could cause failures if we miss another way a property could be set
9190
// val requiredProperties = kClass.declaredMemberProperties.filter {!it.returnType.isMarkedNullable }.map { it.name }.toSet()

src/main/kotlin/com/fasterxml/jackson/module/kotlin/ReflectionCache.kt

+6-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import kotlin.reflect.jvm.kotlinFunction
1919
internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
2020
companion object {
2121
// Increment is required when properties that use LRUMap are changed.
22-
private const val serialVersionUID = 1L
22+
private const val serialVersionUID = 2L
2323
}
2424

2525
sealed class BooleanTriState(val value: Boolean?) {
@@ -42,8 +42,7 @@ internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
4242
}
4343
}
4444

45-
private val javaConstructorToKotlin = LRUMap<Constructor<Any>, KFunction<Any>>(reflectionCacheSize, reflectionCacheSize)
46-
private val javaMethodToKotlin = LRUMap<Method, KFunction<*>>(reflectionCacheSize, reflectionCacheSize)
45+
private val javaExecutableToKotlin = LRUMap<Executable, KFunction<*>>(reflectionCacheSize, reflectionCacheSize)
4746
private val javaExecutableToValueCreator = LRUMap<Executable, ValueCreator<*>>(reflectionCacheSize, reflectionCacheSize)
4847
private val javaConstructorIsCreatorAnnotated = LRUMap<AnnotatedConstructor, Boolean>(reflectionCacheSize, reflectionCacheSize)
4948
private val javaMemberIsRequired = LRUMap<AnnotatedMember, BooleanTriState?>(reflectionCacheSize, reflectionCacheSize)
@@ -57,11 +56,11 @@ internal class ReflectionCache(reflectionCacheSize: Int) : Serializable {
5756
private val valueClassBoxConverterCache: LRUMap<KClass<*>, ValueClassBoxConverter<*, *>> =
5857
LRUMap(0, reflectionCacheSize)
5958

60-
fun kotlinFromJava(key: Constructor<Any>): KFunction<Any>? = javaConstructorToKotlin.get(key)
61-
?: key.kotlinFunction?.let { javaConstructorToKotlin.putIfAbsent(key, it) ?: it }
59+
fun kotlinFromJava(key: Constructor<*>): KFunction<*>? = javaExecutableToKotlin.get(key)
60+
?: key.kotlinFunction?.let { javaExecutableToKotlin.putIfAbsent(key, it) ?: it }
6261

63-
fun kotlinFromJava(key: Method): KFunction<*>? = javaMethodToKotlin.get(key)
64-
?: key.kotlinFunction?.let { javaMethodToKotlin.putIfAbsent(key, it) ?: it }
62+
fun kotlinFromJava(key: Method): KFunction<*>? = javaExecutableToKotlin.get(key)
63+
?: key.kotlinFunction?.let { javaExecutableToKotlin.putIfAbsent(key, it) ?: it }
6564

6665
/**
6766
* return null if...

0 commit comments

Comments
 (0)