Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
779 views
in Technique[技术] by (71.8m points)

performance - Speeding up java deep copy operations

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...