Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/25/09 18:04:44 (16 years ago)
Author:
epitzer
Message:

Avoid recompiling regex during string parsing and cache storable attribues for faster saving. (#646)

File:
1 edited

Legend:

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

    r1852 r1893  
    2020      BindingFlags.DeclaredOnly;
    2121
     22    private static Dictionary<KeyValuePair<Type, bool>, IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>>> memberCache =
     23      new Dictionary<KeyValuePair<Type, bool>, IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>>>();
     24
    2225    public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>> GetStorableMembers(Type type) {
    2326      return GetStorableMembers(type, true);
     
    2629    public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>>
    2730        GetStorableMembers(Type type, bool inherited) {
     31      var query = new KeyValuePair<Type, bool>(type, inherited);
     32      if (memberCache.ContainsKey(query))
     33        return memberCache[query];
     34      IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>> storablesMembers = GenerateStorableMembers(type, inherited);
     35      memberCache[query] = storablesMembers;
     36      return storablesMembers;
     37    }
     38
     39    public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>>
     40        GenerateStorableMembers(Type type, bool inherited) {
     41      List<KeyValuePair<StorableAttribute, MemberInfo>> storableMembers =
     42        new List<KeyValuePair<StorableAttribute, MemberInfo>>();
    2843      if (inherited && type.BaseType != null)
    29         foreach (var pair in GetStorableMembers(type.BaseType))
    30           yield return pair;
     44        storableMembers.AddRange(GenerateStorableMembers(type.BaseType, true));       
    3145      foreach (MemberInfo memberInfo in type.GetMembers(instanceMembers)) {
    3246        foreach (object attribute in memberInfo.GetCustomAttributes(false)) {
     
    3448            attribute as StorableAttribute;
    3549          if (storableAttribute != null) {
    36             yield return new KeyValuePair<StorableAttribute, MemberInfo>(storableAttribute, memberInfo);
     50            storableMembers.Add(new KeyValuePair<StorableAttribute, MemberInfo>(storableAttribute, memberInfo));
    3751          }
    3852        }
    3953      }
     54      return storableMembers;
    4055    }
    4156
Note: See TracChangeset for help on using the changeset viewer.