Why Java's serialization slower than 3rd party APIs?

和自甴很熟 提交于 2021-02-06 02:14:34

问题


During working on sockets and serializing objects over them, I noticed that there are some 3rd party libraries for faster object serialization on Java such as Kryo and FST. Up to now, I expected that Java's serialization is optimized and the fastest. Because, it is language dependent and gives a low level solution that is expected to be faster. However, the considered libraries claim that they are faster than Java.

Can someone explain why Java could not provide the fastest serialization solution? For the sake of what does it give up a better performance?

Thanks in advance.


回答1:


There are several reasons (i am the author of http://code.google.com/p/fast-serialization/)

Reasons:

  • crawls up the Class hierarchy for each Object doing several calls to read/writeObject per Object in case.
  • Partially poor coding (improved with 1.7)
  • Some often used classes make use of old slow + outdated serialization features such as putfield/getfield etc.
  • Too much temporary Object allocation
  • A lot of validation (versioning, implemented interfaces)
  • Slow Java Input/Output streams
  • Reflection to set/get field values.
  • use of JDK collections requiring "big numbers" such as Integer or Long instead of primitives.
  • implementation lacks certain algorithmic optimizations :-)
  • primitives are reordered into network byte order (in java code, not native) on x86.

In order to give better performance, they would have to give up support of old versioning schemes (e.g. the way read/writeObject currently works is suboptimal), and make some things such as versioning support optional or choose more performance sensitive approaches to that (would be possible). Additionally HotSpot might add some intrinsics to improve low level handling of primitives. One needs to have performance in mind when designing an API, which was probably not the case with JDK Serialization.




回答2:


Java serialization is slow because it uses reflection. JDK serialization does a lot of backward compatibility checking and strict type checking. But java serialization garneted 100% same object after deserialization in most of the case.



来源:https://stackoverflow.com/questions/19447623/why-javas-serialization-slower-than-3rd-party-apis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!