Free cookie consent management tool by TermsFeed Policy Generator

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.