Java Reference
In-Depth Information
Keeping track of that set of previously written objects, as well as all that recursion, adds a
small performance hit to object serialization. However, as demonstrated in the example with
an array of Point objects, it can't be avoided: code must keep track of the previously written
objects and reconstitute the correct object references. However, it is possible to perform
smart optimizations by suppressing object references that can be easily re-created when the
object is deserialized.
Figure 10-2. Simple TreeMap structure
Different collection classes handle this differently. The TreeMap class, for example, simply
iterates through the tree and writes only the keys and values; serialization discards all in-
formation about the relationship between the keys (i.e., their sort order). When the data has
been deserialized, the readObject() method then re-sorts the data to produce a tree. Al-
though sorting the objects again sounds like it would be expensive, it is not: that process is
about 20% faster on a set of 10,000 stock objects than using the default object serialization,
which chases all the object references.
The TreeMap class also benefits from this optimization because it can write out fewer ob-
jects. A node (or in JDK language, an Entry ) within a map contains two objects: the key and
the value. Because the map cannot contain two identical nodes, the serialization code doesn't
need to worry about preserving object references to nodes. In this case, it can skip writing the
Search WWH ::




Custom Search