Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/28/10 11:33:38 (15 years ago)
Author:
epitzer
Message:

replace repeated calls through reflection with generated code for a twofold speedup (#548)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableReflection.cs

    r3205 r3553  
    77using HeuristicLab.Persistence.Auxiliary;
    88using System.Text;
     9using System.Reflection.Emit;
    910
    1011namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
     
    1819      BindingFlags.DeclaredOnly;
    1920
    20     public static IEnumerable<StorableMemberInfo> GenerateStorableMembers(Type type, bool inherited) {
     21    private delegate void HookWrapper<T>(T o);
     22    public delegate void Hook(object o);
     23
     24    public static IEnumerable<StorableMemberInfo> GenerateStorableMembers(Type type) {
    2125      var storableMembers = new List<StorableMemberInfo>();
    22       if (inherited && type.BaseType != null)
    23         storableMembers.AddRange(GenerateStorableMembers(type.BaseType, true));
     26      if (type.BaseType != null)
     27        storableMembers.AddRange(GenerateStorableMembers(type.BaseType));
    2428
    2529      var storableClassAttribute = GetStorableClassAttribute(type);
     
    4751    }
    4852
    49     public static IEnumerable<MethodInfo> CollectHooks(HookType hookType, Type type) {
     53    public static IEnumerable<Hook> CollectHooks(HookType hookType, Type type) {
    5054      if (type.BaseType != null)
    5155        foreach (var mi in CollectHooks(hookType, type.BaseType))
     
    5660            MethodInfo methodInfo = memberInfo as MethodInfo;
    5761            if (memberInfo.MemberType != MemberTypes.Method || memberInfo == null)
    58               throw new ArgumentException("Storable hooks must be methods");
    59             yield return methodInfo;
     62              throw new ArgumentException("Storable hooks must be methods");       
     63            DynamicMethod dm = new DynamicMethod("", null, new[] { typeof(object) }, type);
     64            ILGenerator ilgen = dm.GetILGenerator();
     65            ilgen.Emit(OpCodes.Ldarg_1);
     66            ilgen.Emit(OpCodes.Call, methodInfo);
     67            ilgen.Emit(OpCodes.Ret);
     68            yield return (Hook)dm.CreateDelegate(typeof(Hook));
    6069          }
    6170        }
Note: See TracChangeset for help on using the changeset viewer.