Faster deep cloning
There is probably no full working cloning code made by IL Emit on the internet.
But IL Emit is of the same speed as code by Expression Trees, because both methods end up with similar compiled lambda copy functions. Expression Trees are approximately 4x faster than Reflection. The best thing is that Expression Trees general cloning function is available on the internet.
One implemetation by expression trees was already mentioned by Cygon. New thoroughly tested implementation can be found in the CodeProject article Fast Deep Copy by Expression Trees (C#).
Use the extension method by
var copy = originalObject.DeepCopyByExpressionTree();
If you are talking about an object tree/graph:
Writing specific IL to serialize an object is tricky. IMO, your best bet is to look at a full serialization, like how DataContractSerializer
would work - but not necessarily with that engine.
For example, protobuf-net has a Serializer.DeepClone<T>
method that might help. It should be faster than DataContractSerializer
, at least. At the current time, you need to add some clues for the serializer (even if just [ProtoContract(ImplicitFields=ImplicitFields.AllPublic)]
) - however, the current (incomplete) work-in-progress offers POCO support without attributes.
If you are talking about individual objects:
There are fairly simple things you can do here with Expression
in .NET 3.5; build a dynamic Expression
based on reflection, and call .Compile()
. MiscUtil has this already:
DestType clone = PropertyCopy<DestType>.CopyFrom(original);
With .NET 2.0/3.0 (without Expression
) you might consider HyperDescriptor for similar purposes.
I have written three deep clone methods for .NET some time ago:
One uses the well-known
BinaryFormatter
technique (though I tweaked it so that objects do not need to be serializable in order to be cloned). This was by far the slowest.For the second I used pure reflection. It was at least 6 times faster than cloning with the
BinaryFormatter
. This one could also be used on Silverlight and the .NET Compact Framework.The third one uses Linq Expression Trees (for runtime MSIL generation). It is 60 times faster than the
BinaryFormatter
technique but has a setup time of approximately 2 milliseconds for the first time each class is encountered.
The horizontal axis shows the number of objects cloned (though each cloned object includes several nested objects).
The BinaryFormatter
is labeled "Serialization" in the chart. The data series "Reflection" is a custom one that copies fields via GetField()
/SetField()
.
I published all three cloning methods as Open Source here:
http://blog.nuclex-games.com/mono-dotnet/fast-deep-cloning/
There are a lot of libraries that do this operation. You can see benchmark results here:
In short words, if you need peformance, do it manually, it really faster. Also, some libraries allows to peform shallow cloning (by the question, it is good variant for you), which is faster. And do not use BinaryFormatter
if you need any performance.
Also, @frakon mentions that Expressions trees have same speed as IL Emit, it is slightly incorrect. Expressions Tree is slightly slower, but it can be used in partially trusted app.
Manual 13ms
DeepCloner (IL Emit) 167ms
DeepCloner (Expressions) 267ms
CloneExtensions (Expressions) 560ms
NClone 901ms
Clone.Behave! 8551ms
GeorgeCloney 1996ms
Nuclex.Cloning n/a (Crashed)
FastDeepCloner 1882ms
BinaryFormatter 15000ms