altoo-ag/akka-kryo-serialization

Unnecessary instantiation of `clazz` if `includeManifest` is disabled in `KryoSerializerBackend`

hveiga opened this issue ยท 4 comments

hveiga commented

Hello,

I am debugging an issue with Kryo deserialization using Artery and includeManifest is set to false. When deserializing any object I can find that manifest is "" and therefore clazz always becomes a Failure(ClassNotFoundException("")) since it cannot find a proper class for the value "". Since our system is network-heavy, we are seeing this consuming more CPU than expected (throwing exception is usually expensive and should be avoided if possible):

Screen Shot 2023-07-14 at 11 41 02 AM

The actual issue seems to be here:

override def fromBinary(buf: ByteBuffer, manifest: String): AnyRef = {
val buffer = getInput(buf)
val clazz = system.dynamicAccess.getClassFor[AnyRef](manifest)
if (includeManifest)
clazz match {
case Success(c) => kryo.readObject(buffer, c).asInstanceOf[AnyRef]
case _ => throw new RuntimeException("Object of unknown class cannot be deserialized")
}
else
kryo.readClassAndObject(buffer)
}

I am happy to open a PR to solve the issue once I get confirmation this should be changed.

Hi @hveiga
You're absolutely right - clazz should be initialized only within the if clause! Good catch.
Would be nice if you could supply a PR. Thx!

hveiga commented

I'll do it shortly, thanks for the confirmation!

hveiga commented

Thanks for responding and merging so quick, greatly appreciated. Could we get this change released on 2.5.1 some time soon?

Thx for finding the issue - this benefits everybody...
I guess we'll create a bugfix for this one right away - should be in maven central by tomorrow...