Java Reference
In-Depth Information
Here, the airportsVisited field is an array of all the airports I've ever flown to or from, in
the order in which I visited them. So certain airports, like JFK, appear frequently in the array;
SYD appears only once (so far).
Because it is expensive to write object references, this code would certainly perform faster
than the default serialization mechanism for that array: an array of 100,000 Point objects
takes 4.7 seconds to serialize on my machine and 6.9 seconds to deserialize. Using the above
“optimization,” it takes only 2 seconds to serialize and 1.7 seconds to deserialize.
This code, however, is incorrect. The references in the array that specify the location of JFK
all started out referring to the same object. That means when I discover that the location rep-
resented in that data is incorrect, the single JFK reference can be changed, and all objects in
the array will reflect that change (since they are references to the same object).
When the array is deserialized by the above code, those JFK references end up as separate,
different objects. Now when one of those objects is changed, only that object is changed, and
it ends up with different data than the remaining objects that refer to JFK.
This is a very important principle to keep in mind, because optimizing serialization is often
about performing special handling for object references. Done correctly, that can greatly in-
crease the performance of serialization code. Done incorrectly, it can introduce quite subtle
With that in mind, let's explore the serialization of the StockPriceHistory class to see how
serialization optimizations can be made. The fields in that class include the following:
public class
class StockPriceHistoryImpl
StockPriceHistoryImpl implements
implements StockPriceHistory {
private String symbol ;
protected SortedMap < Date , StockPrice > prices = new
new TreeMap <>();
protected Date firstDate ;
protected Date lastDate ;
protected boolean
boolean needsCalc = true
true ;
protected BigDecimal highPrice ;
protected BigDecimal lowPrice ;
protected BigDecimal averagePrice ;
protected BigDecimal stdDev ;
private Map < BigDecimal , ArrayList < Date >> histogram ;
public StockPriceHistoryImpl ( String s , Date firstDate , Date lastDate ) {
Search WWH ::

Custom Search