Tuesday, November 30, 2010

Java Best Practices - High performance Serialization

  • If you don't explicitly set a serialVersionUID class attribute the serialization mechanism has to compute it by going through all the fields and methods to generate a hash, which can be quite slow.
  • With the default serialization mechanism, all the serializing class description information is included in the stream, including descriptions of the instance, the class and all the serializable superclasses.
  • Externalization eliminates almost all the reflective calls used by Serialization mechanism and gives you complete control over the marshalling and demarshalling algorithms, resulting in dramatic performance improvements. However Externalization requires you to rewrite your marshalling and demarshalling code whenever you change your class definitions.
  • Use simpler data representations to serialize objects where possible, e.g. just the timestamp instead of a Date object.
  • You can eliminate serializing null values by serializing meta information about which fields are being serialized.
  • Google protobuf is an alternative serialization mechanism with good size advantages when using compression.

No comments: