public class Example {
public Example duplicate;
public void duplicateState() {
this.example = this.clone();
}
public void loadDuplicate() {
// implementation
}
}
Looking at the above example, you can see that I need to duplicate the Example instance. This is so that objects that want to modify Example must instead modify the duplicate, allowing the main instance to run routines without critical variables being changed. Periodically, the Example will load the duplicate’s values and so that it can perform the routines with updated variables. Performance is critical in this. Does anyone know how to implement the loadDuplicate function in the fastest possible way, or are there any other ways to approach this problem?
If you really need the fastest performance, then both
duplicateState()andloadDuplicate()should consist of a sequence of assignments of the fields ofExample.Both cloning and serialization will be significantly slower, though “how much” will depend on the number of fields and their types.
The other question is whether you need to do a deep or shallow copy of the state of an
Example. Clone (using the defaultclone()method will give you a shallow copy, and serialization will give you a (recursively) deep copy.(FWIW – the last point is a reason why serialization will be slower than cloning. Another is that serialization also needs to include metadata in the serial form … and that’s more data to be copied.)