Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1893 for trunk/sources


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)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default
Files:
2 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
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlSerializer.cs

    r1853 r1893  
    1919    }
    2020
     21    private static Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>", RegexOptions.Singleline);
     22
    2123    public override string Parse(XmlString x) {
    2224      StringBuilder sb = new StringBuilder();
    23       Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>", RegexOptions.Singleline);
    2425      foreach (Match m in re.Matches(x.Data)) {
    2526        sb.Append(m.Groups[1]);
Note: See TracChangeset for help on using the changeset viewer.