Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/17/11 14:41:30 (14 years ago)
Author:
epitzer
Message:

use static cache of storable information (#1530)

Location:
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableClassAnalyzer.cs

    r6214 r6222  
    3232  /// Analyzes storables classes and creates a datastructure containing all required
    3333  /// reflection information to serialize and deserialized these types.
    34   /// </summary>
    35   [StorableClass]
    36   public sealed class StorableClassAnalyzer {
     34  /// </summary> 
     35  public static class StorableClassAnalyzer {
    3736
    38     [Storable]
    39     private static Dictionary<Type, TypeDescriptor> typeDescriptors;
     37    private static Dictionary<Type, TypeDescriptor> typeDescriptors = new Dictionary<Type, TypeDescriptor>();
     38    private static object typeDescriptorLock = new object();
    4039
    4140    /// <summary>
    4241    /// A list of all types that have been analyzed by this instance.
    4342    /// </summary>
    44     public IEnumerable<Type> KnownTypes { get { return typeDescriptors.Keys; } }
    45 
    46     /// <summary>
    47     /// Creates a new instance of the StorableClassAnalyzer.
    48     /// </summary>
    49     public StorableClassAnalyzer() {
    50       typeDescriptors = new Dictionary<Type, TypeDescriptor>();
    51     }
     43    public static IEnumerable<Type> KnownTypes { get { lock (typeDescriptorLock) { return typeDescriptors.Keys; } } }
    5244
    5345    /// <summary>
     
    5850    /// <param name="type">The type to be analyzed.</param>
    5951    /// <returns>A TypeDescriptor object that encapsulates all necessary reflection information.</returns>
    60     public TypeDescriptor this[Type type] {
    61       get {
    62         TypeDescriptor typeDescriptor = null;
     52    public static TypeDescriptor GetDescriptor(Type type) {
     53      TypeDescriptor typeDescriptor = null;
     54      lock (typeDescriptorLock) {
    6355        typeDescriptors.TryGetValue(type, out typeDescriptor);
    64         if (typeDescriptor != null)
    65           return typeDescriptor;
    66         typeDescriptor = new TypeDescriptor(type);
    67         AddMembers(type, typeDescriptor);
    68         if (!typeDescriptor.IsInvalid) {
    69           typeDescriptor.Disentangle();
     56      }
     57      if (typeDescriptor != null)
     58        return typeDescriptor;
     59      typeDescriptor = new TypeDescriptor(type);
     60      AddMembers(type, typeDescriptor);
     61      if (!typeDescriptor.IsInvalid) {
     62        typeDescriptor.Disentangle();
     63        lock (typeDescriptorLock) {
    7064          typeDescriptors[type] = typeDescriptor;
    7165        }
    72         return typeDescriptor;
    7366      }
     67      return typeDescriptor;
    7468    }
    7569
    76     private void AddMembers(Type type, TypeDescriptor typeDescriptor) {
     70    private static void AddMembers(Type type, TypeDescriptor typeDescriptor) {
    7771      if (type.BaseType != null) {
    78         typeDescriptor.BaseTypeDescriptor = this[type.BaseType];
     72        typeDescriptor.BaseTypeDescriptor = StorableClassAnalyzer.GetDescriptor(type.BaseType);
    7973        if (typeDescriptor.BaseTypeDescriptor.IsInvalid) {
    8074          typeDescriptor.IsInvalid = true;
  • branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r6214 r6222  
    4141  public sealed class StorableSerializer : ICompositeSerializer {
    4242
    43     private StorableClassAnalyzer analyzer;
    44 
    45     public StorableSerializer() {
    46       analyzer = new StorableClassAnalyzer();
    47     }
     43    public StorableSerializer() { }
    4844
    4945    [StorableConstructor]
     
    6864    /// </returns>
    6965    public bool CanSerialize(Type type) {
    70       TypeDescriptor desc = analyzer[type];
     66      TypeDescriptor desc = StorableClassAnalyzer.GetDescriptor(type);
    7167      if (desc.IsInvalid)
    7268        return false;
     
    9187    public string JustifyRejection(Type type) {
    9288      StringBuilder sb = new StringBuilder();
    93       TypeDescriptor desc = analyzer[type];
     89      TypeDescriptor desc = StorableClassAnalyzer.GetDescriptor(type);
    9490      if (desc.IsInvalid)
    9591        sb.Append("class is not marked [StorableClass] but has mutable members.");
     
    120116    /// <returns>An enumerable of <see cref="Tag"/>s.</returns>
    121117    public IEnumerable<Tag> Decompose(object obj) {
    122       foreach (var kvp in analyzer[obj.GetType()].Decompose(obj)) {
     118      foreach (var kvp in StorableClassAnalyzer.GetDescriptor(obj.GetType()).Decompose(obj)) {
    123119        yield return new Tag(kvp.Key, kvp.Value);
    124120      }
     
    133129    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    134130      try {
    135         return analyzer[type].CreateInstance();
     131        return StorableClassAnalyzer.GetDescriptor(type).CreateInstance();
    136132      } catch (TargetInvocationException x) {
    137133        throw new PersistenceException(
     
    148144    /// <param name="type">The type.</param>
    149145    public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
    150       analyzer[type].Populate(instance, objects.ToDictionary(kvp => kvp.Name, kvp => kvp.Value));
     146      StorableClassAnalyzer.GetDescriptor(type).Populate(instance, objects.ToDictionary(kvp => kvp.Name, kvp => kvp.Value));
    151147    }
    152148
Note: See TracChangeset for help on using the changeset viewer.