开发者

Speeding up java deep copy operations

开发者 https://www.devze.com 2023-01-13 23:27 出处:网络
We have implemented a general purpose deep copy mechanism using serialization. import java.io.*; public class CopyUtil {

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?


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().


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消