Friday, June 20, 2008

Object Deep Cloning using IL in C# - version 1.1

Howdy,

Meanwhile i've been busy with the cloning stuff in IL, and on demand of mr. Sam
I've adapted the code so that it can also clone LINQ entities (EntitySet etc)

The code is not yet in a final stage but it is becoming more and more stable as I refine the code, so, if you have comments or suggestions, please let me know...

I've added in this version:

  • Deep cloning until 1 level (more level's, i'm not yet sure)

  • Use of attributes for defining which field should be deepcloned, shallowcloned or not be cloned at all. You can change the 'CloneAttribute' by a system attribute if you like, when you for example transfer stuff through WCF or something like that. Just map you own attribute to the cloning attribute in the function -> 'GetCloneTypeForField' and it's normally OK.

  • The cloning code is put together in a CloneHelper static generic class, so that you can use it in combination with the ICloneable interface for example, or just in some framework as possible cloning mechanism.



Now let's take a look at the code:

using System;
using 
System.Collections.Generic;
using 
System.Text;
using 
System.Reflection;
using 
System.Reflection.Emit;
using 
System.Threading;
using 
System.Collections;

namespace 
Cloning
{
    
/// <summary>
    /// Enumeration that defines the type of cloning of a field.
    /// Used in combination with the CloneAttribute
    /// </summary>
    
public enum CloneType
    {
        None,
        ShallowCloning,
        DeepCloning
    }

    
/// <summary>
    /// CloningAttribute for specifying the cloneproperties of a field.
    /// </summary>
    
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    
public class CloneAttribute : Attribute
    {
        
private CloneType _clonetype;
        public 
CloneAttribute()
        {

        }

        
public CloneType CloneType
        {
            
get return _clonetype}
            
set { _clonetype = value; }
        }
    }

    
/// <summary>
    /// Class that clones objects
    /// </summary>
    /// <remarks>
    /// Currently can deepclone to 1 level deep.
    /// Ex. Person.Addresses (Person.List<Address>) 
    /// -> Clones 'Person' deep
    /// -> Clones the objects of the 'Address' list deep
    /// -> Clones the sub-objects of the Address object shallow. (at the moment)
    /// </remarks>
    
public static class CloneHelper<T>
    where T : 
class
    
{
        
#region Declarations
        
// Dictionaries for caching the (pre)compiled generated IL code.
        
private static Dictionary<Type, Delegate> _cachedILShallow = new Dictionary<Type, Delegate>();
        private static 
Dictionary<Type, Delegate> _cachedILDeep = new Dictionary<Type, Delegate>();
        
// This is used for setting the fixed cloning, of this is null, then
        // the custom cloning should be invoked. (use Clone(T obj) for custom cloning)
        
private static CloneType? _globalCloneType CloneType.ShallowCloning;

        #endregion

        #region
 Public Methods

        
/// <summary>
        /// Clone an object with Deep Cloning or with a custom strategy 
        /// such as ShallowCloning and/or DeepCloning combined (use the CloneAttribute)
        /// </summary>
        /// <param name="obj">Object to perform cloning on.</param>
        /// <returns>Cloned object.</returns>
        
public static T Clone(T obj)
        {
            _globalCloneType 
= null;
            return 
CloneObjectWithILDeep(obj);
        
}

        
/// <summary>
        /// Clone an object with one strategy (DeepClone or ShallowClone)
        /// </summary>
        /// <param name="obj">Object to perform cloning on.</param>
        /// <param name="cloneType">Type of cloning</param>
        /// <returns>Cloned object.</returns>
        /// <exception cref="InvalidOperationException">When a wrong enum for cloningtype is passed.</exception>
        
public static T Clone(T obj, CloneType cloneType)
        {
            
if (_globalCloneType != null)
                _globalCloneType 
cloneType;
            switch 
(cloneType)
            {
                
case CloneType.None:
                    
throw new InvalidOperationException("No need to call this method?");
                case 
CloneType.ShallowCloning:
                    
return CloneObjectWithILShallow(obj);
                case 
CloneType.DeepCloning:
                    
return CloneObjectWithILDeep(obj);
                default
:
                    
break;
            
}
            
return default(T);
        
}

        
#endregion

        #region
 Private Methods

        
/// <summary>    
        /// Generic cloning method that clones an object using IL.    
        /// Only the first call of a certain type will hold back performance.    
        /// After the first call, the compiled IL is executed.    
        /// </summary>    
        /// <typeparam name="T">Type of object to clone</typeparam>    
        /// <param name="myObject">Object to clone</param>    
        /// <returns>Cloned object (shallow)</returns>    
        
private static T CloneObjectWithILShallow(T myObject)
        {
            Delegate myExec 
= null;
            if 
(!_cachedILShallow.TryGetValue(typeof(T), out myExec))
            {
                DynamicMethod dymMethod 
= new DynamicMethod("DoShallowClone"typeof(T), new Type[] { typeof(T) }, Assembly.GetExecutingAssembly().ManifestModule, true);
                
ConstructorInfo cInfo myObject.GetType().GetConstructor(new Type[] { });
                
ILGenerator generator dymMethod.GetILGenerator();
                
LocalBuilder lbf generator.DeclareLocal(typeof(T));
                
generator.Emit(OpCodes.Newobj, cInfo);
                
generator.Emit(OpCodes.Stloc_0);
                foreach 
(FieldInfo field in myObject.GetType().GetFields(System.Reflection.BindingFlags.Instance
                | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public))
                {
                    generator.Emit(OpCodes.Ldloc_0)
;
                    
generator.Emit(OpCodes.Ldarg_0);
                    
generator.Emit(OpCodes.Ldfld, field);
                    
generator.Emit(OpCodes.Stfld, field);
                
}
                generator.Emit(OpCodes.Ldloc_0)
;
                
generator.Emit(OpCodes.Ret);
                
myExec dymMethod.CreateDelegate(typeof(Func<T, T>));
                
_cachedILShallow.Add(typeof(T), myExec);
            
}
            
return ((Func<T, T>)myExec)(myObject);
        
}

        
/// <summary>
        /// Generic cloning method that clones an object using IL.
        /// Only the first call of a certain type will hold back performance.
        /// After the first call, the compiled IL is executed. 
        /// </summary>
        /// <param name="myObject">Type of object to clone</param>
        /// <returns>Cloned object (deeply cloned)</returns>
        
private static T CloneObjectWithILDeep(T myObject)
        {
            Delegate myExec 
= null;
            if 
(!_cachedILDeep.TryGetValue(typeof(T), out myExec))
            {
                
// Create ILGenerator            
                
DynamicMethod dymMethod = new DynamicMethod("DoDeepClone"typeof(T), new Type[] { typeof(T) }, Assembly.GetExecutingAssembly().ManifestModule, true);
                
ILGenerator generator dymMethod.GetILGenerator();
                
LocalBuilder cloneVariable generator.DeclareLocal(myObject.GetType());

                
ConstructorInfo cInfo myObject.GetType().GetConstructor(Type.EmptyTypes);
                
generator.Emit(OpCodes.Newobj, cInfo);
                
generator.Emit(OpCodes.Stloc, cloneVariable);

                foreach 
(FieldInfo field in typeof(T).GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public))
                {
                    
if (_globalCloneType == CloneType.DeepCloning)
                    {
                        
if (field.FieldType.IsValueType || field.FieldType == typeof(string))
                        {
                            generator.Emit(OpCodes.Ldloc, cloneVariable)
;
                            
generator.Emit(OpCodes.Ldarg_0);
                            
generator.Emit(OpCodes.Ldfld, field);
                            
generator.Emit(OpCodes.Stfld, field);
                        
}
                        
else if (field.FieldType.IsClass)
                        {
                            CopyReferenceType(generator, cloneVariable, field)
;
                        
}
                    }
                    
else
                    
{
                        
switch (GetCloneTypeForField(field))
                        {
                            
case CloneType.ShallowCloning:
                                {
                                    generator.Emit(OpCodes.Ldloc, cloneVariable)
;
                                    
generator.Emit(OpCodes.Ldarg_0);
                                    
generator.Emit(OpCodes.Ldfld, field);
                                    
generator.Emit(OpCodes.Stfld, field);
                                    break;
                                
}
                            
case CloneType.DeepCloning:
                                {
                                    
if (field.FieldType.IsValueType || field.FieldType == typeof(string))
                                    {
                                        generator.Emit(OpCodes.Ldloc, cloneVariable)
;
                                        
generator.Emit(OpCodes.Ldarg_0);
                                        
generator.Emit(OpCodes.Ldfld, field)
                                        
generator.Emit(OpCodes.Stfld, field);
                                    
}
                                    
else if (field.FieldType.IsClass)
                                        CopyReferenceType(generator, cloneVariable, field)
;
                                    break;
                                
}
                            
case CloneType.None:
                                {
                                    
// Do nothing here, field is not cloned.
                                
}
                                
break;
                        
}
                    }
                }
                generator.Emit(OpCodes.Ldloc_0)
;
                
generator.Emit(OpCodes.Ret);
                
myExec dymMethod.CreateDelegate(typeof(Func<T, T>));
                
_cachedILDeep.Add(typeof(T), myExec);
            
}
            
return ((Func<T, T>)myExec)(myObject);
        
}

        
/// <summary>
        /// Helper method to clone a reference type.
        /// This method clones IList and IEnumerables and other reference types (classes)
        /// Arrays are not yet supported (ex. string[])
        /// </summary>
        /// <param name="generator">IL generator to emit code to.</param>
        /// <param name="cloneVar">Local store wheren the clone object is located. (or child of)</param>
        /// <param name="field">Field definition of the reference type to clone.</param>
        
private static void CopyReferenceType(ILGenerator generator, LocalBuilder cloneVar, FieldInfo field)
        {
            
if (field.FieldType.IsSubclassOf(typeof(Delegate)))
            {
                
return;
            
}
            LocalBuilder lbTempVar 
generator.DeclareLocal(field.FieldType);

            if 
(field.FieldType.GetInterface("IEnumerable") != null && field.FieldType.GetInterface("IList") != null)
            {
                
if (field.FieldType.IsGenericType)
                {
                    Type argumentType 
field.FieldType.GetGenericArguments()[0];
                    
Type genericTypeEnum Type.GetType("System.Collections.Generic.IEnumerable`1[" + argumentType.FullName + "]");

                    
ConstructorInfo ci field.FieldType.GetConstructor(new Type[] { genericTypeEnum });
                    if 
(ci != null && GetCloneTypeForField(field) == CloneType.ShallowCloning)
                    {
                        generator.Emit(OpCodes.Ldarg_0)
;
                        
generator.Emit(OpCodes.Ldfld, field);
                        
generator.Emit(OpCodes.Newobj, ci);
                        
generator.Emit(OpCodes.Stloc, lbTempVar);
                        
generator.Emit(OpCodes.Ldloc, cloneVar);
                        
generator.Emit(OpCodes.Ldloc, lbTempVar);
                        
generator.Emit(OpCodes.Stfld, field);
                    
}
                    
else
                    
{
                        ci 
field.FieldType.GetConstructor(Type.EmptyTypes);
                        if 
(ci != null)
                        {
                            generator.Emit(OpCodes.Newobj, ci)
;
                            
generator.Emit(OpCodes.Stloc, lbTempVar);
                            
generator.Emit(OpCodes.Ldloc, cloneVar);
                            
generator.Emit(OpCodes.Ldloc, lbTempVar);
                            
generator.Emit(OpCodes.Stfld, field);
                            
CloneList(generator, field, argumentType, lbTempVar);
                        
}
                    }
                }
            }
            
else
            
{
                ConstructorInfo cInfo 
field.FieldType.GetConstructor(new Type[] { });
                
generator.Emit(OpCodes.Newobj, cInfo);
                
generator.Emit(OpCodes.Stloc, lbTempVar);
                
generator.Emit(OpCodes.Ldloc, cloneVar);
                
generator.Emit(OpCodes.Ldloc, lbTempVar);
                
generator.Emit(OpCodes.Stfld, field);
                foreach 
(FieldInfo fi in field.FieldType.GetFields(System.Reflection.BindingFlags.Instance
                    | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public))
                {
                    
if (fi.FieldType.IsValueType || fi.FieldType == typeof(string))
                    {
                        generator.Emit(OpCodes.Ldloc_1)
;
                        
generator.Emit(OpCodes.Ldarg_0);
                        
generator.Emit(OpCodes.Ldfld, field);
                        
generator.Emit(OpCodes.Ldfld, fi);
                        
generator.Emit(OpCodes.Stfld, fi);
                    
}
                }
            }
        }

        
/// <summary>
        /// Makes a deep copy of an IList of IEnumerable
        /// Creating new objects of the list and containing objects. (using default constructor)
        /// And by invoking the deepclone method defined above. (recursive)
        /// </summary>
        /// <param name="generator">IL generator to emit code to.</param>
        /// <param name="listField">Field definition of the reference type of the list to clone.</param>
        /// <param name="typeToClone">Base-type to clone (argument of List<T></param>
        /// <param name="cloneVar">Local store wheren the clone object is located. (or child of)</param>
        
private static void CloneList(ILGenerator generator, FieldInfo listField, Type typeToClone, LocalBuilder cloneVar)
        {
            Type genIEnumeratorTyp 
Type.GetType("System.Collections.Generic.IEnumerator`1[" + typeToClone.FullName + "]");
            
Type genIEnumeratorTypLocal Type.GetType(listField.FieldType.Namespace + "." + listField.FieldType.Name + "+Enumerator[[" + typeToClone.FullName + "]]");
            
LocalBuilder lbEnumObject generator.DeclareLocal(genIEnumeratorTyp);
            
LocalBuilder lbCheckStatement generator.DeclareLocal(typeof(bool));
            
Label checkOfWhile generator.DefineLabel();
            
Label startOfWhile generator.DefineLabel();
            
MethodInfo miEnumerator listField.FieldType.GetMethod("GetEnumerator");
            
generator.Emit(OpCodes.Ldarg_0);
            
generator.Emit(OpCodes.Ldfld, listField);
            
generator.Emit(OpCodes.Callvirt, miEnumerator);
            if 
(genIEnumeratorTypLocal != null)
            {
                generator.Emit(OpCodes.Box, genIEnumeratorTypLocal)
;
            
}
            generator.Emit(OpCodes.Stloc, lbEnumObject)
;
            
generator.Emit(OpCodes.Br_S, checkOfWhile);
            
generator.MarkLabel(startOfWhile);
            
generator.Emit(OpCodes.Nop);
            
generator.Emit(OpCodes.Ldloc, cloneVar);
            
generator.Emit(OpCodes.Ldloc, lbEnumObject);
            
MethodInfo miCurrent genIEnumeratorTyp.GetProperty("Current").GetGetMethod();
            
generator.Emit(OpCodes.Callvirt, miCurrent);
            
Type cloneHelper Type.GetType(typeof(CloneHelper<T>).Namespace + "." typeof(CloneHelper<T>).Name + "[" + miCurrent.ReturnType.FullName + "]");
            
MethodInfo miDeepClone cloneHelper.GetMethod("CloneObjectWithILDeep", BindingFlags.Static | BindingFlags.NonPublic);
            
generator.Emit(OpCodes.Call, miDeepClone);
            
MethodInfo miAdd listField.FieldType.GetMethod("Add");
            
generator.Emit(OpCodes.Callvirt, miAdd);
            
generator.Emit(OpCodes.Nop);
            
generator.MarkLabel(checkOfWhile);
            
generator.Emit(OpCodes.Nop);
            
generator.Emit(OpCodes.Ldloc, lbEnumObject);
            
MethodInfo miMoveNext = typeof(IEnumerator).GetMethod("MoveNext");
            
generator.Emit(OpCodes.Callvirt, miMoveNext);
            
generator.Emit(OpCodes.Stloc, lbCheckStatement);
            
generator.Emit(OpCodes.Ldloc, lbCheckStatement);
            
generator.Emit(OpCodes.Brtrue_S, startOfWhile);
        
}

        
/// <summary>
        /// Returns the type of cloning to apply on a certain field when in custom mode.
        /// Otherwise the main cloning method is returned.
        /// You can invoke custom mode by invoking the method Clone(T obj)
        /// </summary>
        /// <param name="field">Field to examine</param>
        /// <returns>Type of cloning to use for this field.</returns>
        
private static CloneType GetCloneTypeForField(FieldInfo field)
        {
            
object[] attributes field.GetCustomAttributes(typeof(CloneAttribute), true);
            if 
(attributes == null || attributes.Length == 0)
            {
                
if (!_globalCloneType.HasValue)
                    
return CloneType.ShallowCloning;
                else
                    return 
_globalCloneType.Value;
            
}
            
return (attributes[0as CloneAttribute).CloneType;
        
}

        
#endregion
    
}
}


If you have any remarks, please notify me, or when you can use this code, please notify me, you may use it as you whish :)

Regards,

F.

Wednesday, June 18, 2008

Object Deep Cloning using IL in C# - version 1.0

Howdy,

If you've read the rest of my blog, then you'll have noticed that I have another post that addresses cloning of objects using IL (Intermediate Language)
If you haven't, then you can find the post 'Object cloning using IL in C#'.

The reason I published this post is because the code in my previous post (mentioned above) makes a shallow copy of an object. It copies only references of classes, value-types are off course copied by value. So if you have a class with only value types or strings (immutable), then the shallow copy will act as a deep copy (sort of).

Now, imagine that you have (like in most projects) objects that contain other objects, like a person object that has one legal address and different optional addresses. Then you'll probably create a Person class and an Address class,
in the person class you'll make a property which has the type 'Address' and a property that's a list of Addresses.

If you would take a shallow clone, and you change an address in the clone, then it will also be changed in the original person.address object.

So, i've created a solution that clones the 'Person' object in this case, with the addresses, but it makes new instances of each address.

The only constraints however are that the 'to-clone' objects MUST have a default constructor for instantiating the class, if the system can make an instance, then the private fields will automatically be copied.

Like I mentioned in my previous post, you'll only loose performance the first time, because of all the lookups and the generating of the IL-code.
After that, the compiled code will be executed.
I tried to optimize the IL code as much as possible; maybe it can be optimized even more.

For example:

If you would clone the address of a person in normal C# code then you can do:

public Person Clone(Person p) 

    Person clone 
= new Person()
    
clone.Address = new Address()
    
// normally you should check on null, 
    // but let's assume that it's never null. 
    
clone.Address.ID p.Address.ID;
    return 
clone;
}

or you can do:

public Person Clone(Person p) 

    Person clone 
= new Person();
    
Address a = new Address();
    
clone.Address a;
    
// normally you should check on null, 
    // but let's assume that it's never null. 
    
a.ID p.Address.ID;
    return 
clone;
}

The performance of the second option is 'higher'.

You could ask, why?
Well, if you would disassemble these two code samples, then you'll notice that the second code needs fewer instructions.

First sample decompiled:

.locals init (
     [0] class Cloning.Person clone)
L_0000: nop
L_0001: newobj instance void Cloning.Person::.ctor()
L_0006: stloc.0
L_0007: ldloc.0
L_0008: newobj instance void Cloning.Address::.ctor()
L_000d: callvirt instance void Cloning.Person::set_Address(class Cloning.Address)
L_0012: nop
L_0013: ldloc.0
L_0014: callvirt instance class Cloning.Address Cloning.Person::get_Address()
L_0019: ldarg.1
L_001a: callvirt instance class Cloning.Address Cloning.Person::get_Address()
L_001f: callvirt instance int32 Cloning.Address::get_AddressID()
L_0024: callvirt instance void Cloning.Address::set_AddressID(int32)

L_0029: nop
L_002a: ldloc.0
L_002b: ret


Second sample decompiled:

.locals init (
     [0] class Cloning.Person clone,
     [1] class Cloning.Address a)
L_0000: nop
L_0001: newobj instance void Cloning.Person::.ctor()
L_0006: stloc.0
L_0007: newobj instance void Cloning.Address::.ctor()
L_000c: stloc.1
L_000d: ldloc.0
L_000e: ldloc.1
L_000f: callvirt instance void Cloning.Person::set_Address(class Cloning.Address)
L_0014: nop
L_0015: ldloc.1
L_0016: ldarg.1
L_0017: callvirt instance class Cloning.Address Cloning.Person::get_Address()
L_001c: callvirt instance int32 Cloning.Address::get_AddressID()
L_0021: callvirt instance void Cloning.Address::set_AddressID(int32)
L_0026: nop
L_0027: ldloc.0
L_0028: ret


As you see in the code of the first sample, you need 5 callvirt's, and it's more IL code,
in the second sample you have 4 callvirt's and less code, because we have a kind of shortcut to the address object
from the local store [1].

When you create an object, you push it onto the stack and then you store it in a local store (local variable).
If you use a.ID then you need to lookup the ID from the local store reference,
else if you use clone.Address.ID then you need to lookup the Address reference from the person object (also in a store), and then lookup the ID reference from the retrieved Address reference, and then you can do an action with it.
(lookat line L_0013 to L_0024 of first sample and line L_0015 to L_0021 in second sample)

In that manner I tried to optimize all the calls in the IL code, so that I can do the required job in as less instructions as possible.

Here is the result:

Now let's take a look at the code:

Our revisited Person class with an Address class:


using System;
using 
System.Collections.Generic;
using 
System.Text;

namespace 
Cloning
{
    
public class Person
    {
        
private int _id;
        private string 
_name;
        private string 
_firstName;
        private string 
_field1, _field2, _field3;

        public 
Person()
        {
            
this.Addresses = new List<Address>();
        
}

        
public int ID
        {
            
get return _id}
            
set { _id = value; }
        }

        
public string Name
        {
            
get return _name}
            
set { _name = value; }
        }

        
public string FirstName
        {
            
get return _firstName}
            
set { _firstName = value; }
        }

        
private Address _address;

        public 
Address Address
        {
            
get return _address}
            
set { _address = value; }
        }

        
private List<Address> _addresses;

        public 
List<Address> Addresses
        {
            
get return _addresses}
            
set { _addresses = value; }
        }

    }

    
public class Address
    {
        
public Address()
        {
            
this.AddressID -1;
        
}

        
public Address(int aid)
        {
            
this.AddressID aid;
        
}

        
private int _addressID;

        public int 
AddressID
        {
            
get return _addressID}
            
set { _addressID = value; }
        }

        
private string _street;

        public string 
Street
        {
            
get return _street}
            
set { _street = value; }
        }

        
private string _city;

        public string 
City
        {
            
get return _city}
            
set { _city = value; }
        }

    }
}


The Cloning class used for testing:

using System;
using 
System.Collections.Generic;
using 
System.Text;
using 
System.Reflection;
using 
System.Reflection.Emit;
using 
System.Threading;
using 
System.Collections;

namespace 
Cloning
{
    
/// <summary>    
    /// Delegate handler that's used to compile the IL to.    
    /// (This delegate is standard in .net 3.5)    
    /// </summary>    
    /// <typeparam name="T1">Parameter Type</typeparam>    
    /// <typeparam name="TResult">Return Type</typeparam>    
    /// <param name="arg1">Argument</param>    
    /// <returns>Result</returns>    
    
public delegate TResult Func<T1, TResult>(T1 arg1);

    public class 
Cloning
    {
        
/// <summary>    
        /// This dictionary caches the delegates for each 'to-clone' type.    
        /// </summary>    
        
private static Dictionary<Type, Delegate> _cachedIL = new Dictionary<Type, Delegate>();
        private static 
Dictionary<Type, Delegate> _cachedILDeep = new Dictionary<Type, Delegate>();
        private 
LocalBuilder _lbfTemp;

        
/// <summary>    
        /// Clone one person object with reflection    
        /// </summary>    
        /// <param name="p">Person to clone</param>    
        /// <returns>Cloned person</returns>    
        
public static Person CloneObjectWithReflection(Person p)
        {
            FieldInfo[] fis 
p.GetType().GetFields(System.Reflection.BindingFlags.Instance |
                System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic)
;
            
Person newPerson = new Person();
            foreach 
(FieldInfo fi in fis)
            {
                fi.SetValue(newPerson, fi.GetValue(p))
;
            
}
            
return newPerson;
        
}

        
/// <summary>    
        /// Clone a person object by manually typing the copy statements.    
        /// </summary>    
        /// <param name="p">Object to clone</param>    
        /// <returns>Cloned object</returns>    
        
public static Person CloneNormal(Person p)
        {
            Person newPerson 
= new Person();
            
newPerson.ID p.ID;
            
newPerson.Name p.Name;
            
newPerson.FirstName p.FirstName;
            
newPerson.Address = new Address();
            
newPerson.Address.AddressID p.Address.AddressID;
            
newPerson.Address.City p.Address.City;
            
newPerson.Address.Street p.Address.Street;
            if
(newPerson.Addresses!=null)
            {
                newPerson.Addresses 
= new List<Address>();
                foreach 
(Address a in newPerson.Addresses)
                {
                    newPerson.Addresses.Add(a)
;
                
}
            }
            
return newPerson;
        
}

        
/// <summary>    
        /// Generic cloning method that clones an object using IL.    
        /// Only the first call of a certain type will hold back performance.    
        /// After the first call, the compiled IL is executed.    
        /// </summary>    
        /// <typeparam name="T">Type of object to clone</typeparam>    
        /// <param name="myObject">Object to clone</param>    
        /// <returns>Cloned object</returns>    
        
public static T CloneObjectWithILShallow<T>(T myObject)
        {
            Delegate myExec 
= null;
            if 
(!_cachedIL.TryGetValue(typeof(T), out myExec))
            {
                
// Create ILGenerator (both DM declarations work)
                // DynamicMethod dymMethod = new DynamicMethod("DoClone", typeof(T), 
                //      new Type[] { typeof(T) }, true);
                
DynamicMethod dymMethod = new DynamicMethod("DoClone"typeof(T), 
                    
new Type[] { typeof(T) }, Assembly.GetExecutingAssembly().ManifestModule, true);
                
ConstructorInfo cInfo myObject.GetType().GetConstructor(new Type[] { });
                
ILGenerator generator dymMethod.GetILGenerator();
                
LocalBuilder lbf generator.DeclareLocal(typeof(T));
                
generator.Emit(OpCodes.Newobj, cInfo);
                
generator.Emit(OpCodes.Stloc_0);
                foreach 
(FieldInfo field in myObject.GetType().GetFields(
                        System.Reflection.BindingFlags.Instance 
                        | System.Reflection.BindingFlags.NonPublic 
                        | System.Reflection.BindingFlags.Public))
                {
                    generator.Emit(OpCodes.Ldloc_0)
;
                    
generator.Emit(OpCodes.Ldarg_0);
                    
generator.Emit(OpCodes.Ldfld, field);
                    
generator.Emit(OpCodes.Stfld, field);
                
}
                generator.Emit(OpCodes.Ldloc_0)
;
                
generator.Emit(OpCodes.Ret);
                
myExec dymMethod.CreateDelegate(typeof(Func<T, T>));
                
_cachedIL.Add(typeof(T), myExec);
            
}
            
return ((Func<T, T>)myExec)(myObject);
        
}

        
public T CloneObjectWithILDeep<T>(T myObject)
        {
            Delegate myExec 
= null;
            if 
(!_cachedILDeep.TryGetValue(typeof(T), out myExec))
            {
                
// Create ILGenerator (both DM declarations work)
                // DynamicMethod dymMethod = new DynamicMethod("DoClone", typeof(T), 
                //      new Type[] { typeof(T) }, true);
                
DynamicMethod dymMethod = new DynamicMethod("DoClone"typeof(T), 
                    
new Type[] { typeof(T) }, Assembly.GetExecutingAssembly().ManifestModule, true);
                
ConstructorInfo cInfo myObject.GetType().GetConstructor(new Type[] { });
                
ILGenerator generator dymMethod.GetILGenerator();
                
LocalBuilder lbf generator.DeclareLocal(typeof(T));
                
generator.Emit(OpCodes.Newobj, cInfo);
                
generator.Emit(OpCodes.Stloc_0);

                foreach 
(FieldInfo field in typeof(T).GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public))
                {
                    
if (field.FieldType.IsValueType || field.FieldType == typeof(string))
                        CopyValueType(generator, field)
;
                    else if 
(field.FieldType.IsClass)
                        CopyReferenceType(generator, field)
;
                
}
                generator.Emit(OpCodes.Ldloc_0)
;
                
generator.Emit(OpCodes.Ret);
                
myExec dymMethod.CreateDelegate(typeof(Func<T, T>));
                
_cachedILDeep.Add(typeof(T), myExec);
            
}
            
return ((Func<T, T>)myExec)(myObject);
        
}

        
private void CreateNewTempObject(ILGenerator generator, Type type)
        {
            ConstructorInfo cInfo 
type.GetConstructor(new Type[] { });
            
generator.Emit(OpCodes.Newobj, cInfo);
            
generator.Emit(OpCodes.Stloc, _lbfTemp);
        
}

        
private void CopyValueType(ILGenerator generator, FieldInfo field)
        {
            generator.Emit(OpCodes.Ldloc_0)
;
            
generator.Emit(OpCodes.Ldarg_0);
            
generator.Emit(OpCodes.Ldfld, field);
            
generator.Emit(OpCodes.Stfld, field);
        
}

        
private void CopyValueTypeTemp(ILGenerator generator, FieldInfo fieldParent, FieldInfo fieldDetail)
        {
            generator.Emit(OpCodes.Ldloc_1)
;
            
generator.Emit(OpCodes.Ldarg_0);
            
generator.Emit(OpCodes.Ldfld, fieldParent);
            
generator.Emit(OpCodes.Ldfld, fieldDetail);
            
generator.Emit(OpCodes.Stfld, fieldDetail);
        
}

        
private void PlaceNewTempObjInClone(ILGenerator generator, FieldInfo field)
        {
            
// Get object from custom location and store it in right field of location 0
            
generator.Emit(OpCodes.Ldloc_0);
            
generator.Emit(OpCodes.Ldloc, _lbfTemp);
            
generator.Emit(OpCodes.Stfld, field);
        
}

        
private void CopyReferenceType(ILGenerator generator, FieldInfo field)
        {
            
// We have a reference type.
            
_lbfTemp generator.DeclareLocal(field.FieldType);
            if 
(field.FieldType.GetInterface("IEnumerable") != null)
            {
                
// We have a list type (generic).
                
if (field.FieldType.IsGenericType)
                {
                    
// Get argument of list type
                    
Type argType field.FieldType.GetGenericArguments()[0];
                    
// Check that it has a constructor that accepts another IEnumerable.
                    
Type genericType Type.GetType("System.Collections.Generic.IEnumerable`1[" 
                            
+ argType.FullName + "]");
                    
                    
ConstructorInfo ci field.FieldType.GetConstructor(new Type[] { genericType });
                    if 
(ci != null)
                    {
                        
// It has! (Like the List<> class)
                        
generator.Emit(OpCodes.Ldarg_0);
                        
generator.Emit(OpCodes.Ldfld, field);
                        
generator.Emit(OpCodes.Newobj, ci);
                        
generator.Emit(OpCodes.Stloc, _lbfTemp);
                        
PlaceNewTempObjInClone(generator, field);
                    
}
                }
            }
            
else
            
{
                CreateNewTempObject(generator, field.FieldType)
;
                
PlaceNewTempObjInClone(generator, field);
                foreach 
(FieldInfo fi in field.FieldType.GetFields(System.Reflection.BindingFlags.Instance
                    | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public))
                {
                    
if (fi.FieldType.IsValueType || fi.FieldType == typeof(string))
                        CopyValueTypeTemp(generator, field, fi)
;
                    else if 
(fi.FieldType.IsClass)
                        CopyReferenceType(generator, fi)
;
                
}
            }
        }

    }
}


The Program class with the test code in it.

namespace Cloning
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
// Do some cloning tests...
            
Cloning.TestCloning tc = new Cloning.TestCloning();
            
tc.DoTest();
        
}
    }
}


Explanation:

As you see in the code, the model is a little bit expanded.
But if you shuffle a bit with the code, then you can put it in 1 method again,
I only did this to improve readability, so that you can understand what's happening, and how the code is generated.

I also left out the comments from the IL parts, if you want to know what those statements mean, then look at my other post regarding IL cloning (Object cloning using IL in C#), it's explained there.

Method list of cloning class with a short description:


  • Person CloneObjectWithReflection(Person p)
    This method clones an object using reflection (very slow).

  • Person CloneNormal(Person p)
    This method clones the person class manually.
    I think a lot of people will do/user this method :)


  • T CloneObjectWithILShallow(T myObject)
    Make a shallow copy of an object using IL in a generic method.

  • T CloneObjectWithILDeep(T myObject)
    Make a deep copy of an object using IL in a generic method.

  • void CreateNewTempObject(ILGenerator generator, Type type)
    Generate IL-code to create a new object of a certain type and store it in a local store. (local variable)

  • void CopyValueType(ILGenerator generator, FieldInfo field)
    Generate IL-code to copy the values of a value type to the clone.

  • void CopyValueTypeTemp(ILGenerator generator, FieldInfo fieldParent, FieldInfo fieldDetail)
    Generate IL-code to copy the values of a value type from a store location to the destination address in the clone.

  • void PlaceNewTempObjInClone(ILGenerator generator, FieldInfo field)
    Generate IL-code to reference an object from a store to an address in the clone.

  • void CopyReferenceType(ILGenerator generator, FieldInfo field)
    Generate IL-code to copy the values of a reference type (class) to the clone. Instantiate new objects if needed.


I didn't test LINQ support, i guess that an extra 'exception' has to be added for the IQueryable or something like that, and also Arrays should be added, but that's for in version 2.0 or something like that ;), you can see how the system works and expand it to your needs...

I hope that this post and piece of code is useful for some people, if so, please let me know through a comment, thanks ;)

Regards,
F.

Monday, March 17, 2008

Domain Specific Language (DSL) for Neural Network(s)

Howdy folks,

With this entry I would like to show the possibilities of DSL.
For those who don't know DSL, DSL as in Domain Specific Language,
is a modeling tool for modeling problems in an organization for example.

An example that everybody who's familiar with Visual Studio Standard Edition or higher knows is the Class Designer. It's a graphical tool within VS that allows you to model your classes, and it also generated code in the background.

Seeing this, I did some digging, and I came up on the subject of DSL's or DSL Tools.

As you already know probably, I've got a big interest in A.I., neural networks more specifically. Wouldn't it be cool that you could just draw you neural network structure and let VS generate all the network code for you?
In this way you can make balanced or unbalanced neural networks, which can have a big benefit.

I've made a little design of how the designer should look like.



The boxes in yellow are the DSL Domain classes, those classes are the domain model of the DSL, who describe the structure of the problem, those classes should be able to store all the 'problem information'.

The Red boxes are the shapes that are related to the domain model.
A neuron is represented by a Circle.
A Connection should be represented by a line in a certain color etc...
DSL let's you do lot's of fun things with it.

I’ve drawn in Visio how the designer itself should look like, this is an approximate drawing, but it's very close to the reality.
When my designer is finished, I’ll put some screenshots online, and maybe also some code.



This is how it looks like in visual studio (in the experimental hive):



This example shows the structure of a back propagation network that's able to solve the famous XOR problem. This problem is also addressed in another entry of mine regarding the Neural Network Simulator.

The Designer I’m building uses in fact the same framework, with the difference that there is an extra factory for creating the network, and the framework is extended with extended methods of the .NET 3.5 network.

Friday, March 14, 2008

Object Cloning Using IL in C#

This subject is inspired on a session I followed on the TechDays 2008,
that addressed the fact that IL (Intermediate Language) can be used to clone objects, among other things, and that it's not evil at all, and it can be pretty performant also.

You only have to see that you don't overuse it tho, because otherwise the readability of your code is reduced, which is not a good thing for the maintenance.
And wrong usage of reflection (what IL is, or at least uses) can also result in poor performance.

This being said, I tested this on my own, with some self written code and comments,
just to test that it was really true what Mr. Roy Osherove told me ;)

Below you can see a screenshot of a console application that does the tests.



As you see, sometimes the IL code is even faster the the normal cloning on a simple class Person with a couple of fields in it that are filled in with some random values.

Let's take a look at the code, first we declare a simple class Person
I've added comments in the code so that people that don't understand IL too much, can also understand what's happening.

Person class definition:

public class Person
{
private int _id;
private string _name;
private string _firstName;
private string _field1, _field2, _field3;

public Person()
{

}

public int ID
{
get { return _id; }
set { _id = value; }
}

public string Name
{
get { return _name; }
set { _name = value; }
}

public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}




TestCode:

The code below is a nice piece of code, read through the comments and you'll understand what's stated.


class Program
{
/// <summary>
/// Delegate handler that's used to compile the IL to.
/// (This delegate is standard in .net 3.5)
/// </summary>
/// <typeparam name="T1">Parameter Type</typeparam>
/// <typeparam name="TResult">Return Type</typeparam>
/// <param name="arg1">Argument</param>
/// <returns>Result</returns>
public delegate TResult Func<T1, TResult>(T1 arg1);
/// <summary>
/// This dictionary caches the delegates for each 'to-clone' type.
/// </summary>
static Dictionary<Type, Delegate> _cachedIL = new Dictionary<Type, Delegate>();

/// <summary>
/// The Main method that's executed for the test.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
DoCloningTest(1000);
DoCloningTest(10000);
DoCloningTest(100000);
//Commented because the test takes long ;)
//DoCloningTest(1000000);

Console.ReadKey();
}

/// <summary>
/// Do the cloning test and printout the results.
/// </summary>
/// <param name="count">Number of items to clone</param>
private static void DoCloningTest(int count)
{
// Create timer class.
HiPerfTimer timer = new HiPerfTimer();
double timeElapsedN = 0, timeElapsedR = 0, timeElapsedIL = 0;

Console.WriteLine("--> Creating {0} objects...", count);
timer.StartNew();
List<Person> personsToClone = CreatePersonsList(count);
timer.Stop();
Person temp = CloneObjectWithIL(personsToClone[0]);
temp = null;
Console.WriteLine("\tCreated objects in {0} seconds", timer.Duration);

Console.WriteLine("- Cloning Normal...");
List<Person> clonedPersons = new List<Person>(count);
timer.StartNew();
foreach (Person p in personsToClone)
{
clonedPersons.Add(CloneNormal(p));
}
timer.Stop();
timeElapsedN = timer.Duration;

Console.WriteLine("- Cloning IL...");
clonedPersons = new List<Person>(count);
timer.StartNew();
foreach (Person p in personsToClone)
{
clonedPersons.Add(CloneObjectWithIL<Person>(p));
}
timer.Stop();
timeElapsedIL = timer.Duration;

Console.WriteLine("- Cloning Reflection...");
clonedPersons = new List<Person>(count);
timer.StartNew();
foreach (Person p in personsToClone)
{
clonedPersons.Add(CloneObjectWithReflection(p));
}
timer.Stop();
timeElapsedR = timer.Duration;

Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("----------------------------------------");
Console.WriteLine("Object count:\t\t{0}", count);
Console.WriteLine("Cloning Normal:\t\t{0:00.0000} s", timeElapsedN);
Console.WriteLine("Cloning IL:\t\t{0:00.0000} s", timeElapsedIL);
Console.WriteLine("Cloning Reflection:\t{0:00.0000} s", timeElapsedR);
Console.WriteLine("----------------------------------------");
Console.ResetColor();
}

/// <summary>
/// Create a list of persons with random data and a given number of items.
/// </summary>
/// <param name="count">Number of persons to generate</param>
/// <returns>List of generated persons</returns>
private static List<Person> CreatePersonsList(int count)
{
Random r = new Random(Environment.TickCount);
List<Person> persons = new List<Person>(count);
for (int i = 0; i < count; i++)
{
Person p = new Person();
p.ID = r.Next();
p.Name = string.Concat("Slaets_", r.Next());
p.FirstName = string.Concat("Filip_", r.Next());
persons.Add(p);
}
return persons;
}

/// <summary>
/// Clone one person object with reflection
/// </summary>
/// <param name="p">Person to clone</param>
/// <returns>Cloned person</returns>
private static Person CloneObjectWithReflection(Person p)
{
// Get all the fields of the type, also the privates.
FieldInfo[] fis = p.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
// Create a new person object
Person newPerson = new Person();
// Loop through all the fields and copy the information from the parameter class
// to the newPerson class.
foreach (FieldInfo fi in fis)
{
fi.SetValue(newPerson, fi.GetValue(p));
}
// Return the cloned object.
return newPerson;
}

/// <summary>
/// Generic cloning method that clones an object using IL.
/// Only the first call of a certain type will hold back performance.
/// After the first call, the compiled IL is executed.
/// </summary>
/// <typeparam name="T">Type of object to clone</typeparam>
/// <param name="myObject">Object to clone</param>
/// <returns>Cloned object</returns>
private static T CloneObjectWithIL<T>(T myObject)
{
Delegate myExec = null;
if (!_cachedIL.TryGetValue(typeof(T), out myExec))
{
// Create ILGenerator
DynamicMethod dymMethod = new DynamicMethod("DoClone", typeof(T), new Type[] { typeof(T) }, true);
ConstructorInfo cInfo = myObject.GetType().GetConstructor(new Type[] { });

ILGenerator generator = dymMethod.GetILGenerator();

LocalBuilder lbf = generator.DeclareLocal(typeof(T));
//lbf.SetLocalSymInfo("_temp");

generator.Emit(OpCodes.Newobj, cInfo);
generator.Emit(OpCodes.Stloc_0);
foreach (FieldInfo field in myObject.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic))
{
// Load the new object on the eval stack... (currently 1 item on eval stack)
generator.Emit(OpCodes.Ldloc_0);
// Load initial object (parameter) (currently 2 items on eval stack)
generator.Emit(OpCodes.Ldarg_0);
// Replace value by field value (still currently 2 items on eval stack)
generator.Emit(OpCodes.Ldfld, field);
// Store the value of the top on the eval stack into the object underneath that value on the value stack.
// (0 items on eval stack)
generator.Emit(OpCodes.Stfld, field);
}

// Load new constructed obj on eval stack -> 1 item on stack
generator.Emit(OpCodes.Ldloc_0);
// Return constructed object. --> 0 items on stack
generator.Emit(OpCodes.Ret);

myExec = dymMethod.CreateDelegate(typeof(Func<T, T>));
_cachedIL.Add(typeof(T), myExec);
}
return ((Func<T, T>)myExec)(myObject);
}

/// <summary>
/// Clone a person object by manually typing the copy statements.
/// </summary>
/// <param name="p">Object to clone</param>
/// <returns>Cloned object</returns>
private static Person CloneNormal(Person p)
{
Person newPerson = new Person();
newPerson.ID = p.ID;
newPerson.Name = p.Name;
newPerson.FirstName = p.FirstName;
return newPerson;
}
}


The basic thing that it does is, create a DynamicMethod, get the ILGenerator, emit code in the method, compile it to a delegate, and execute the delegate.
The delegate is cached so that the IL is not generated each time a cloning should take place, so we loose only one time performance, when the first object is cloned (the IL has to be created and compiled at runtime).

Hopefully this article is of use for some people, if so, let me know.

Regards