Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/15/10 12:25:55 (15 years ago)
Author:
epitzer
Message:

collect all storable caches into storable serializer to reduce prolonged memory usage (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable
Files:
1 deleted
4 edited

Legend:

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

    r3029 r3031  
    1616    /// Specify how members are selected for serialization.
    1717    /// </summary>
    18     public StorableClassType Type { get; set; }
     18    public StorableClassType Type { get; private set; }
    1919
    2020    /// <summary>
     
    3131    /// <see cref="StorableClassType.MarkedOnly"/>.
    3232    /// </summary>
    33     public StorableClassAttribute() {
    34     }
     33    public StorableClassAttribute() { }
    3534
    3635  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableHookAttribute.cs

    r3016 r3031  
    1919  /// </summary>
    2020  [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
    21   public sealed class StorableHookAttribute : Attribute {
    22 
    23     private sealed class HookDesignator {
    24       public Type Type { get; set; }
    25       public HookType HookType { get; set; }
    26       public HookDesignator() { }
    27       public HookDesignator(Type type, HookType hookType) {
    28         Type = type;
    29         HookType = HookType;
    30       }
    31     }
     21  public sealed class StorableHookAttribute : Attribute {   
    3222
    3323    private readonly HookType hookType;
     
    4030    }
    4131
    42 
    4332    /// <summary>
    4433    /// Mark method as <c>StorableSerializer</c> hook to be run
     
    4837    public StorableHookAttribute(HookType hookType) {
    4938      this.hookType = hookType;
    50     }
    51 
    52     private static readonly BindingFlags declaredInstanceMembers =
    53       BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly;
    54 
    55     private static readonly object[] emptyArgs = new object[] { };
    56 
    57     private static Dictionary<HookDesignator, List<MethodInfo>> hookCache =
    58       new Dictionary<HookDesignator, List<MethodInfo>>();
    59 
    60 
    61     /// <summary>
    62     /// Invoke <c>hookType</c> hook on <c>obj</c>.
    63     /// </summary>
    64     /// <param name="hookType">Type of the hook.</param>
    65     /// <param name="obj">The object.</param>
    66     public static void InvokeHook(HookType hookType, object obj) {
    67       if (obj == null)
    68         throw new ArgumentNullException("Cannot invoke hooks on null");
    69       foreach (MethodInfo mi in GetHooks(hookType, obj.GetType())) {
    70         mi.Invoke(obj, emptyArgs);
    71       }
    7239    }   
    73 
    74     private static IEnumerable<MethodInfo> GetHooks(HookType hookType, Type type) {
    75       lock (hookCache) {
    76         List<MethodInfo> hooks;
    77         var designator = new HookDesignator(type, hookType);
    78         hookCache.TryGetValue(designator, out hooks);
    79         if (hooks != null)
    80           return hooks;
    81         hooks = new List<MethodInfo>(CollectHooks(hookType, type));
    82         hookCache.Add(designator, hooks);
    83         return hooks;
    84       }
    85     }
    86 
    87     private static IEnumerable<MethodInfo> CollectHooks(HookType hookType, Type type) { 
    88       if (type.BaseType != null)
    89         foreach (var mi in CollectHooks(hookType, type.BaseType))
    90           yield return mi;
    91       foreach (MemberInfo memberInfo in type.GetMembers(declaredInstanceMembers)) {
    92         foreach (StorableHookAttribute hook in memberInfo.GetCustomAttributes(typeof(StorableHookAttribute), false)) {
    93           if (hook != null && hook.HookType == hookType) {
    94             MethodInfo methodInfo = memberInfo as MethodInfo;
    95             if (memberInfo.MemberType != MemberTypes.Method || memberInfo == null)
    96               throw new ArgumentException("Storable hooks must be methods");
    97             yield return methodInfo;
    98           }
    99         }
    100       }
    101     }
    102 
    10340  }
    10441}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableReflection.cs

    r3029 r3031  
    1717      BindingFlags.NonPublic |
    1818      BindingFlags.DeclaredOnly;
    19 
    2019
    2120    public static IEnumerable<StorableMemberInfo> GenerateStorableMembers(Type type, bool inherited) {
     
    4342    }
    4443
     44    public static bool IsEmptyOrStorableType(Type type, bool recusrive) {
     45      if (IsEmptyType(type, recusrive)) return true;
     46      if (!HasStorableClassAttribute(type)) return false;
     47      return !recusrive || type.BaseType == null || IsEmptyOrStorableType(type.BaseType, true);
     48    }
     49
     50    public static IEnumerable<MethodInfo> CollectHooks(HookType hookType, Type type) {
     51      if (type.BaseType != null)
     52        foreach (var mi in CollectHooks(hookType, type.BaseType))
     53          yield return mi;
     54      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
     55        foreach (StorableHookAttribute hook in memberInfo.GetCustomAttributes(typeof(StorableHookAttribute), false)) {
     56          if (hook != null && hook.HookType == hookType) {
     57            MethodInfo methodInfo = memberInfo as MethodInfo;
     58            if (memberInfo.MemberType != MemberTypes.Method || memberInfo == null)
     59              throw new ArgumentException("Storable hooks must be methods");
     60            yield return methodInfo;
     61          }
     62        }
     63      }
     64    }
     65
     66    #region [Storable] helpers
     67
    4568    private static void AddMarkedMembers(Type type, List<StorableMemberInfo> storableMembers) {
    4669      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
     
    6083    }
    6184
    62     private static StorableClassAttribute GetStorableClassAttribute(Type type) {
    63       return (StorableClassAttribute)type
    64         .GetCustomAttributes(typeof(StorableClassAttribute), false)
    65         .SingleOrDefault();
    66     }
    6785
    6886    private static IEnumerable<StorableMemberInfo> DisentangleNameMapping(
     
    107125    }
    108126
    109     public static bool IsEmptyOrStorableType(Type type, bool recusrive) {
    110       if (IsEmptyType(type, recusrive)) return true;
    111       if (!HastStorableClassAttribute(type)) return false;
    112       return !recusrive || type.BaseType == null || IsEmptyOrStorableType(type.BaseType, true);
     127    #endregion
     128
     129    #region [StorableClass] helpers
     130
     131    private static StorableClassAttribute GetStorableClassAttribute(Type type) {
     132      return (StorableClassAttribute)type
     133        .GetCustomAttributes(typeof(StorableClassAttribute), false)
     134        .SingleOrDefault();
     135    }   
     136
     137    private static bool HasStorableClassAttribute(Type type) {
     138      return type.GetCustomAttributes(typeof(StorableClassAttribute), false).Length > 0;
    113139    }
    114140
    115     private static bool HastStorableClassAttribute(Type type) {
    116       return type.GetCustomAttributes(typeof(StorableClassAttribute), false).Length > 0;
    117     }
     141    #endregion
     142
     143    #region other helpers
    118144
    119145    private static bool IsEmptyType(Type type, bool recursive) {
     
    136162      return pi.CanWrite;
    137163    }
     164
     165    #endregion
     166
    138167  }
    139168}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r3029 r3031  
    4343
    4444    public IEnumerable<Tag> CreateMetaInfo(object o) {
    45       StorableHookAttribute.InvokeHook(HookType.BeforeSerialization, o);
     45      InvokeHook(HookType.BeforeSerialization, o);
    4646      return new Tag[] { };
    4747    }
     
    7979        }
    8080      }
    81       StorableHookAttribute.InvokeHook(HookType.AfterDeserialization, instance);
     81      InvokeHook(HookType.AfterDeserialization, instance);
    8282    }
    8383
     
    8989      BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
    9090
     91    private static readonly object[] emptyArgs = new object[] { };
     92
    9193    private sealed class TypeQuery {
    9294      public Type Type { get; private set; }
     
    9597        this.Type = type;
    9698        this.Inherited = inherited;
     99      }
     100    }
     101
     102    private sealed class HookDesignator {
     103      public Type Type { get; set; }
     104      public HookType HookType { get; set; }
     105      public HookDesignator() { }
     106      public HookDesignator(Type type, HookType hookType) {
     107        Type = type;
     108        HookType = HookType;
    97109      }
    98110    }
     
    107119    private Dictionary<Type, ConstructorInfo> constructorCache =
    108120      new Dictionary<Type, ConstructorInfo>();
     121   
     122    private Dictionary<HookDesignator, List<MethodInfo>> hookCache =
     123      new Dictionary<HookDesignator, List<MethodInfo>>();
    109124
    110125    #endregion
     
    150165    }
    151166
     167    private void InvokeHook(HookType hookType, object obj) {
     168      if (obj == null)
     169        throw new ArgumentNullException("Cannot invoke hooks on null");
     170      foreach (MethodInfo mi in GetHooks(hookType, obj.GetType())) {
     171        mi.Invoke(obj, emptyArgs);
     172      }
     173    }
     174
     175    private IEnumerable<MethodInfo> GetHooks(HookType hookType, Type type) {
     176      lock (hookCache) {
     177        List<MethodInfo> hooks;
     178        var designator = new HookDesignator(type, hookType);
     179        hookCache.TryGetValue(designator, out hooks);
     180        if (hooks != null)
     181          return hooks;
     182        hooks = new List<MethodInfo>(StorableReflection.CollectHooks(hookType, type));
     183        hookCache.Add(designator, hooks);
     184        return hooks;
     185      }
     186    }
     187
    152188    #endregion
     189
     190   
    153191   
    154192  }
Note: See TracChangeset for help on using the changeset viewer.