CORDA-1820: Reduce amount of logging/lookup performed by SerializerFactory.findCustomSerializer() (#3633)

* CORDA-1820: Reduce amount of logging/lookup performed by SerializerFactory.findCustomSerializer()

* CORDA-1820: Changes to make DJVM work.
This commit is contained in:
Viktor Kolomeyko 2018-07-17 21:04:04 +01:00 committed by GitHub
parent dafd979bde
commit 7e3687c306
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -21,6 +21,7 @@ private class DeterministicSerializerFactoryFactory : SerializerFactoryFactory {
serializersByType = mutableMapOf(),
serializersByDescriptor = mutableMapOf(),
customSerializers = ArrayList(),
customSerializersCache = mutableMapOf(),
transformsCache = mutableMapOf()
)
}

View File

@ -25,6 +25,8 @@ import javax.annotation.concurrent.ThreadSafe
data class SerializationSchemas(val schema: Schema, val transforms: TransformsSchema)
@KeepForDJVM
data class FactorySchemaAndDescriptor(val schemas: SerializationSchemas, val typeDescriptor: Any)
@KeepForDJVM
data class CustomSerializersCacheKey(val clazz: Class<*>, val declaredType: Type)
/**
* Factory of serializers designed to be shared across threads and invocations.
@ -56,6 +58,7 @@ open class SerializerFactory(
private val serializersByType: MutableMap<Type, AMQPSerializer<Any>>,
val serializersByDescriptor: MutableMap<Any, AMQPSerializer<Any>>,
private val customSerializers: MutableList<SerializerFor>,
private val customSerializersCache: MutableMap<CustomSerializersCacheKey, AMQPSerializer<Any>?>,
val transformsCache: MutableMap<String, EnumMap<TransformTypes, MutableList<Transform>>>,
private val onlyCustomSerializers: Boolean = false
) {
@ -74,6 +77,7 @@ open class SerializerFactory(
ConcurrentHashMap(),
CopyOnWriteArrayList(),
ConcurrentHashMap(),
ConcurrentHashMap(),
onlyCustomSerializers
)
@ -377,6 +381,13 @@ open class SerializerFactory(
}
internal fun findCustomSerializer(clazz: Class<*>, declaredType: Type): AMQPSerializer<Any>? {
return customSerializersCache.computeIfAbsent(CustomSerializersCacheKey(clazz, declaredType), ::doFindCustomSerializer)
}
private fun doFindCustomSerializer(key: CustomSerializersCacheKey): AMQPSerializer<Any>? {
val (clazz, declaredType) = key
// e.g. Imagine if we provided a Map serializer this way, then it won't work if the declared type is
// AbstractMap, only Map. Otherwise it needs to inject additional schema for a RestrictedType source of the
// super type. Could be done, but do we need it?
@ -389,7 +400,7 @@ open class SerializerFactory(
|| !customSerializer.isSerializerFor(declaredSuperClass)
|| !customSerializer.revealSubclassesInSchema
) {
logger.info ("action=\"Using custom serializer\", class=${clazz.typeName}, " +
logger.debug("action=\"Using custom serializer\", class=${clazz.typeName}, " +
"declaredType=${declaredType.typeName}")
@Suppress("UNCHECKED_CAST")