Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3030 for trunk/sources


Ignore:
Timestamp:
03/15/10 12:02:56 (15 years ago)
Author:
epitzer
Message:

prevent excessive caching of reflection information by re-instantiating serializers for every run (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Configuration.cs

    r3017 r3030  
    11using System;
     2using System.Linq;
    23using System.Collections.Generic;
    34using HeuristicLab.Persistence.Interfaces;
     
    1112  /// <c>ConfigurationService</c>.
    1213  /// </summary>
    13   [StorableClass]   
     14  [StorableClass]
    1415  public class Configuration {
    1516
     
    2728    [Storable]
    2829    public IFormat Format { get; private set; }
    29    
    30     private Configuration() {
     30
     31    [StorableConstructor]
     32    private Configuration(bool isDeserializing) {
    3133      compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
     34      if (isDeserializing)
     35        return;
     36      primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
     37      compositeSerializers = new List<ICompositeSerializer>();
    3238    }
    3339
     
    4046    public Configuration(IFormat format,
    4147        IEnumerable<IPrimitiveSerializer> primitiveSerializers,
    42         IEnumerable<ICompositeSerializer> compositeSerializers) {
     48        IEnumerable<ICompositeSerializer> compositeSerializers)
     49      : this(false) {
    4350      this.Format = format;
    44       this.primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
    45       foreach (IPrimitiveSerializer primitiveSerializer in primitiveSerializers) {
    46         if (primitiveSerializer.SerialDataType != format.SerialDataType) {
    47           throw new ArgumentException("All primitive serializers must have the same IFormat.");
    48         }
     51      this.compositeSerializers.AddRange(compositeSerializers);
     52      foreach (var primitiveSerializer in primitiveSerializers) {
     53        if (primitiveSerializer.SerialDataType != format.SerialDataType)
     54          throw new ArgumentException(string.Format(
     55            "primitive serializer's ({0}) serialized data type ({1}) " + Environment.NewLine +
     56            "is not compatible with selected format's ({2}) seriali data type ({3})",
     57            primitiveSerializers.GetType().FullName, primitiveSerializer.SerialDataType.FullName,
     58            format.Name, format.SerialDataType.FullName));           
    4959        this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer);
    50       }
    51       this.compositeSerializers = new List<ICompositeSerializer>(compositeSerializers);
    52       compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
     60      }     
    5361    }
    5462
     
    97105      return null;
    98106    }
     107
     108    /// <summary>
     109    /// Copies this configuration and re-instantiates all serializers.
     110    /// </summary>
     111    /// <returns>A new <see cref="Configuration"/></returns>
     112    public Configuration Copy() {
     113      var config = new Configuration(false);
     114      config.Format = Format;
     115      foreach (var ps in primitiveSerializers)
     116        config.primitiveSerializers.Add(
     117          ps.Key,
     118          (IPrimitiveSerializer)Activator.CreateInstance(ps.Value.GetType()));
     119      foreach (var cs in compositeSerializers)
     120        config.compositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(cs.GetType()));
     121      return config;
     122    }
     123
    99124  }
    100125
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs

    r3016 r3030  
    242242
    243243    /// <summary>
    244     /// Get a configuration for a certain format. This returns a custom configuration
     244    /// Get a configuration for a certain format. This returns a fresh copy of a custom configuration,
    245245    /// if defined, otherwise returns the default (automatically discovered) configuration.
    246246    /// </summary>
     
    249249    public Configuration GetConfiguration(IFormat format) {
    250250      if (customConfigurations.ContainsKey(format))
    251         return customConfigurations[format];
     251        return customConfigurations[format].Copy();
    252252      return GetDefaultConfig(format);
    253253    }
     
    258258    /// <param name="configuration">The new configuration.</param>
    259259    public void DefineConfiguration(Configuration configuration) {
    260       customConfigurations[configuration.Format] = configuration;
     260      customConfigurations[configuration.Format] = configuration.Copy();
    261261      SaveSettings();
    262262    }
Note: See TracChangeset for help on using the changeset viewer.