Karol Kolenda
Karol Kolenda

Reputation: 1690

Faster deep cloning

Does anyone want a framework/class which allows me to clone by values .Net objects? I'm only interested with public read/write properties (namely DataContracts), and I don't care if references are resolved correctly (i.e. collecions which contains the same instance of item twice).

I tried serialization trick via DataContractSerializer (serialize to XML and back), wrote reflection-based cloning class (sometimes faster/sometimes slower), and was wondering if someone wrote a helper class which can do this via Emit and not reflection. As for now emitting IL is a little to much for my little brain, but I guess this would be the ultimate solution. Unless someone knows an alternative method which is faster than DataContractSerializer.

Upvotes: 26

Views: 21758

Answers (10)

Kanber Kav
Kanber Kav

Reputation: 1

Very fast:

Install Newtonsoft Json and use

public static class ExtensionMethods
{
    public static T DeepCopy<T>(this T self)
    {
        var serialized = JsonConvert.SerializeObject(self);
        return JsonConvert.DeserializeObject<T>(serialized);
    }
}

Upvotes: 0

Cygon
Cygon

Reputation: 9620

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.

Logarithmic scale illustrating cloning performance

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/

Upvotes: 25

force
force

Reputation: 141

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

Upvotes: 11

Alen.Toma
Alen.Toma

Reputation: 4870

Well! you could write your own Clone Method wich you could specify to ignore or include properties by its attributes. My new library in the link down, uses reflection and FieldInfo to clone the object recursively. i have added it to CodeProject so you will have access to its code soon which you could modify it to your needs.

Try it out its very fast and clean, you will love it.
https://www.nuget.org/packages/FastDeepCloner/1.0.1
or
PM> Install-Package FastDeepCloner

Upvotes: -1

frakon
frakon

Reputation: 2271

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

Upvotes: 5

Toxantron
Toxantron

Reputation: 2398

The CGbR Code Generator can generate an implementation of ICloneable for you. All you need is the nuget package and a partial class definition that implements ICloneable. The generator will do the rest for you:

public partial class Root : ICloneable
{
    public Root(int number)
    {
        _number = number;
    }
    private int _number;

    public Partial[] Partials { get; set; }

    public IList<ulong> Numbers { get; set; }

    public object Clone()
    {
        return Clone(true);
    }

    private Root()
    {
    }
} 

public partial class Root
{
    public Root Clone(bool deep)
    {
        var copy = new Root();
        // All value types can be simply copied
        copy._number = _number; 
        if (deep)
        {
            // In a deep clone the references are cloned 
            var tempPartials = new Partial[Partials.Length];
            for (var i = 0; i < Partials.Length; i++)
            {
                var value = Partials[i];
                value = value.Clone(true);
                tempPartials[i] = value;
            }
            copy.Partials = tempPartials;
            var tempNumbers = new List<ulong>(Numbers.Count);
            for (var i = 0; i < Numbers.Count; i++)
            {
                var value = Numbers[i];
                tempNumbers[i] = value;
            }
            copy.Numbers = tempNumbers;
        }
        else
        {
            // In a shallow clone only references are copied
            copy.Partials = Partials; 
            copy.Numbers = Numbers; 
        }
        return copy;
    }
}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1063433

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.

Upvotes: 15

Sam Saffron
Sam Saffron

Reputation: 131142

Dynamic Method based serialization will be fastest. (Generate a dynamic method using light weight codegen and use it for serialization)

You can do 1 method per property/field or one method for the whole object. From my benchmarking doing 1 per property does not give you too much of a performance hit.

See the following code, to see how I do this in Media Browser: http://code.google.com/p/videobrowser/source/browse/trunk/MediaBrowser/Library/Persistance/Serializer.cs

There are also some unit tests there.

There is fast reflection sample on theinstructionlimit that does exactly what you want.

see:

http://theinstructionlimit.com/?p=76

Upvotes: 1

Dirk Vollmar
Dirk Vollmar

Reputation: 176219

I don't know whether this suits your requirements exactly, but you could also create a deep clone using a BinaryFormatter. See this answer to a related question (by Binoj Antony):

public static class GenericCopier<T>
{
    public static T DeepCopy(object objectToCopy)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(memoryStream, objectToCopy);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return (T) binaryFormatter.Deserialize(memoryStream);
        }
    }
}

Upvotes: 3

Anton Gogolev
Anton Gogolev

Reputation: 115829

Try AutoMapper or BLToolkit Mapping

Upvotes: 3

Related Questions