Skip to content

Commit

Permalink
CORDA-1820: Reduce amount of logging/lookup performed by SerializerFa…
Browse files Browse the repository at this point in the history
…ctory.findCustomSerializer() (corda#3633)

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

* CORDA-1820: Changes to make DJVM work.
  • Loading branch information
vkolomeyko authored Jul 17, 2018
1 parent dafd979 commit 7e3687c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private class DeterministicSerializerFactoryFactory : SerializerFactoryFactory {
serializersByType = mutableMapOf(),
serializersByDescriptor = mutableMapOf(),
customSerializers = ArrayList(),
customSerializersCache = mutableMapOf(),
transformsCache = mutableMapOf()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
) {
Expand All @@ -74,6 +77,7 @@ open class SerializerFactory(
ConcurrentHashMap(),
CopyOnWriteArrayList(),
ConcurrentHashMap(),
ConcurrentHashMap(),
onlyCustomSerializers
)

Expand Down Expand Up @@ -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?
Expand All @@ -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")
Expand Down

0 comments on commit 7e3687c

Please sign in to comment.