views:

84

answers:

2

We have implemented a general purpose deep copy mechanism using serialization.

import java.io.*;

public class CopyUtil {

    public static Object clone(Object source) {
        Object retVal = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(source);
            oos.flush();
            oos.close();

            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
            retVal = in.readObject();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        return retVal;
    }
}

There are a relatively large number of object classes, that are evolving all the time, to maintain - This was the reason why we proceeded with a general purpose cloning mechanism. We didn't relish the idea of maintaining readObject() and writeObject() on 200+ classes.

Unfortunately the serialization mechanism in Java is relatively slow and we are experiencing issues when our system is under peak load.

Are there any suggested approaches on how we can speed things up a bit or (in case we have carried this out incorrectly) alternative methods of cloning objects?

+1  A: 

You may want to check out Deep Cloning Library. I don't know how it's implemented, but you may find it's a faster solution.

Although it doesn't address speed, this question has some relevant resources that would be worth investigating.

Brian Agnew
it's implemented by reflection
Bozho
+1  A: 

A much faster alternative to serialization is implemented in Hibernate (specifically in the 2nd level cache); I don't know the details but you can check out the source code.

You may be aware that the clone() interface is broken, thus is better avoided, unless there is a really really compelling reason to use it. From Effective Java 2nd Edition, Item 11: Override clone judiciously

Given all of the problems associated with Cloneable, it’s safe to say that other interfaces should not extend it, and that classes designed for inheritance (Item 17) should not implement it. Because of its many shortcomings, some expert programmers simply choose never to override the clone method and never to invoke it except, perhaps, to copy arrays. If you design a class for inheritance, be aware that if you choose not to provide a well-behaved protected clone method, it will be impossible for subclasses to implement Cloneable.

Update: On shallow/deep cloning

From the clone() API:

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. [...]

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

So in fact, the convention is to do a deep copy.

Still, the preferred alternative is to define a copy constructor or an independent method instead of overriding clone().

Péter Török
`clone()` performs shallow clone.
Bozho
@Bozho, the default implementation, yes.
Péter Török
@Bozho: `Object.clone()` performs shallow clone, but any class that implements `Cloneable` should override that method to perform a deep clone.
Chris Jester-Young
well, in that case you don't need to call it `clone()`. It may be `copy()` or `deepClone()`
Bozho
@Bozho, unfortunately the situation is more complex than that - this is part of the reason why `clone` should not be overridden in new code. See my update.
Péter Török