Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/29/13 14:39:02 (11 years ago)
Author:
mkommend
Message:

#1427: Moved actual value caching and execution contexts from Paramater to LookupParameter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Parameters/3.3/LookupParameter.cs

    r7259 r9195  
    2121
    2222using System;
     23using System.Threading;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    3132  [Item("LookupParameter", "A parameter whose value is retrieved from or written to a scope.")]
    3233  [StorableClass]
    33   public class LookupParameter<T> : Parameter, ILookupParameter<T> where T : class, IItem {
     34  public class LookupParameter<T> : Parameter, IStatefulItem, ILookupParameter<T> where T : class, IItem {
    3435    [Storable]
    3536    private string actualName;
     
    5960    }
    6061
     62    private Lazy<ThreadLocal<IItem>> cachedActualValues;
     63    private IItem CachedActualValue {
     64      get { return cachedActualValues.Value.Value; }
     65    }
     66
     67    private Lazy<ThreadLocal<IExecutionContext>> executionContexts;
     68    public IExecutionContext ExecutionContext {
     69      get { return executionContexts.Value.Value; }
     70      set {
     71        if (value != executionContexts.Value.Value) {
     72          executionContexts.Value.Value = value;
     73          cachedActualValues.Value.Value = null;
     74        }
     75      }
     76    }
     77
    6178    [StorableConstructor]
    62     protected LookupParameter(bool deserializing) : base(deserializing) { }
     79    protected LookupParameter(bool deserializing)
     80      : base(deserializing) {
     81      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     82      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     83    }
    6384    protected LookupParameter(LookupParameter<T> original, Cloner cloner)
    6485      : base(original, cloner) {
    6586      actualName = original.actualName;
     87      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     88      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    6689    }
    6790    public LookupParameter()
     
    6992      this.actualName = Name;
    7093      this.Hidden = true;
     94      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     95      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    7196    }
    7297    public LookupParameter(string name)
     
    7499      this.actualName = Name;
    75100      this.Hidden = true;
     101      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     102      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    76103    }
    77104    public LookupParameter(string name, string description)
     
    79106      this.actualName = Name;
    80107      this.Hidden = true;
     108      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     109      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    81110    }
    82111    public LookupParameter(string name, string description, string actualName)
     
    84113      this.actualName = string.IsNullOrWhiteSpace(actualName) ? Name : actualName;
    85114      this.Hidden = true;
     115      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     116      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    86117    }
    87118
     
    132163    }
    133164    protected override IItem GetActualValue() {
     165      if (CachedActualValue != null) return CachedActualValue;
    134166      string name;
    135167      // try to get value from context stack
     
    146178                          typeof(T).GetPrettyName())
    147179          );
     180        cachedActualValues.Value.Value = var.Value;
    148181        return var.Value;
    149182      }
     
    156189                        typeof(T).GetPrettyName())
    157190        );
     191      cachedActualValues.Value.Value = value;
     192
    158193      // try to set value in context stack
    159194      string name;
     
    175210    }
    176211
     212    public virtual void InitializeState() {
     213    }
     214    public virtual void ClearState() {
     215      if (cachedActualValues.IsValueCreated) {
     216        cachedActualValues.Value.Dispose();
     217        cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     218      }
     219      if (executionContexts.IsValueCreated) {
     220        executionContexts.Value.Dispose();
     221        executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     222      }
     223    }
     224
    177225    public event EventHandler ActualNameChanged;
    178226    protected virtual void OnActualNameChanged() {
Note: See TracChangeset for help on using the changeset viewer.