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

Remove unnecessary cache #628

Merged
merged 3 commits into from
Feb 19, 2023
Merged
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
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Ilya Ryzhenkov (@orangy)

WrongWrong (@k163377)
* #627: Merge creator cache for Constructor and Method
* #628: Remove unnecessary cache

# 2.14.0

Expand Down
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Co-maintainers:
#580: Lazy load UNIT_TYPE
(contributed by Ilya R)
#627: Merge creator cache for Constructor and Method
#628: Remove unnecessary cache

2.14.2 (28-Jan-2023)
2.14.1 (21-Nov-2022)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c
private fun hasCreatorAnnotation(member: AnnotatedConstructor): Boolean {
// don't add a JsonCreator to any constructor if one is declared already

val kClass = cache.kotlinFromJava(member.declaringClass as Class<Any>)
val kClass = member.declaringClass.kotlin
.apply { if (this in ignoredClassesForImplyingJsonCreator) return false }
val kConstructor = cache.kotlinFromJava(member.annotated as Constructor<Any>) ?: return false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import com.fasterxml.jackson.databind.util.LRUMap
import java.lang.reflect.Constructor
import java.lang.reflect.Executable
import java.lang.reflect.Method
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.kotlinFunction


internal class ReflectionCache(reflectionCacheSize: Int) {
sealed class BooleanTriState(val value: Boolean?) {
class True : BooleanTriState(true)
Expand All @@ -34,17 +32,11 @@ internal class ReflectionCache(reflectionCacheSize: Int) {
}
}

private val javaClassToKotlin = LRUMap<Class<Any>, KClass<Any>>(reflectionCacheSize, reflectionCacheSize)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Class -> KClass conversion is cached on the Kotlin side as of 1.5.32, so there is no need to cache it in jackson-module-kotlin anymore.
https://github.com/JetBrains/kotlin/blob/v1.5.32/core/reflection.jvm/src/kotlin/reflect/jvm/internal/kClassCache.kt

private val javaConstructorToKotlin = LRUMap<Constructor<Any>, KFunction<Any>>(reflectionCacheSize, reflectionCacheSize)
private val javaMethodToKotlin = LRUMap<Method, KFunction<*>>(reflectionCacheSize, reflectionCacheSize)
private val javaExecutableToValueCreator = LRUMap<Executable, ValueCreator<*>>(reflectionCacheSize, reflectionCacheSize)
private val javaConstructorIsCreatorAnnotated = LRUMap<AnnotatedConstructor, Boolean>(reflectionCacheSize, reflectionCacheSize)
private val javaMemberIsRequired = LRUMap<AnnotatedMember, BooleanTriState?>(reflectionCacheSize, reflectionCacheSize)
private val kotlinGeneratedMethod = LRUMap<AnnotatedMethod, Boolean>(reflectionCacheSize, reflectionCacheSize)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cache was no longer being used.



fun kotlinFromJava(key: Class<Any>): KClass<Any> = javaClassToKotlin.get(key)
?: key.kotlin.let { javaClassToKotlin.putIfAbsent(key, it) ?: it }

fun kotlinFromJava(key: Constructor<Any>): KFunction<Any>? = javaConstructorToKotlin.get(key)
?: key.kotlinFunction?.let { javaConstructorToKotlin.putIfAbsent(key, it) ?: it }
Expand Down Expand Up @@ -89,7 +81,4 @@ internal class ReflectionCache(reflectionCacheSize: Int) {

fun javaMemberIsRequired(key: AnnotatedMember, calc: (AnnotatedMember) -> Boolean?): Boolean? = javaMemberIsRequired.get(key)?.value
?: calc(key).let { javaMemberIsRequired.putIfAbsent(key, BooleanTriState.fromBoolean(it))?.value ?: it }

fun isKotlinGeneratedMethod(key: AnnotatedMethod, calc: (AnnotatedMethod) -> Boolean): Boolean = kotlinGeneratedMethod.get(key)
?: calc(key).let { kotlinGeneratedMethod.putIfAbsent(key, it) ?: it }
}