Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/23/17 01:11:18 (8 years ago)
Author:
pkimmesw
Message:

#2665 simplifier, push solution results view, performance improvements, small bug fixes, ui fixes

Location:
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Pool
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Pool/ManagedPoolProvider.cs

    r14747 r14777  
    88  using System.Runtime.Serialization.Formatters.Binary;
    99
    10   public interface IManagedPool<T> : IDisposable where T : class {
    11     T Get();
     10  public interface IManagedPool<T> : IDisposable where T : class, IPooledObject {
     11    T Get(bool reset = true);
    1212  }
    1313
    14   public class ManagedPoolProvider<T> where T : class {
     14  public class ManagedPoolProvider<T> where T : class, IPooledObject {
    1515    private readonly ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>();
    1616    private readonly ObjectPool<IManagedPool<T>> managedPools;
    17     private T[] DummyPartition;
     17    private readonly BinaryFormatter binaryFormatter = new BinaryFormatter();
     18    private byte[] dummyPartition;
     19
     20    private readonly Func<T> factory;
    1821
    1922    public readonly int PartitionSize;
     
    2124    public const int DefaultMaxInstanceCount = 65536;
    2225
    23     public ManagedPoolProvider(int partitionSize, int? maxPartitionCount = null) {
     26    public ManagedPoolProvider(int partitionSize, Func<T> factory, int? maxPartitionCount = null) {
    2427      PartitionSize = partitionSize;
    2528      MaxParitionCount = maxPartitionCount ?? DefaultMaxInstanceCount / PartitionSize;
     29      this.factory = factory;
    2630
    27       managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool<T>(this));
     31      managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool<T>(this), MaxParitionCount);
    2832    }
     33    public int InstanceCount { get { return partitions.Count * PartitionSize; } }
    2934
    30     public void InitDummyPartition(Func<T> factory) {
    31       DummyPartition = new T[PartitionSize];
     35    private void InitDummyPartition(Func<T> factory) {
     36      var temp = new T[PartitionSize];
    3237
    3338      for (var i = 0; i < PartitionSize; i++) {
    34         DummyPartition[i] = factory();
     39        temp[i] = factory();
     40      }
     41
     42      using (var memoryStream = new MemoryStream()) {
     43        binaryFormatter.Serialize(memoryStream, temp);
     44        dummyPartition = memoryStream.ToArray();
    3545      }
    3646    }
    37 
    38     public int InstanceCount { get { return partitions.Count * PartitionSize; } }
    3947
    4048    public void ReleasePartitions(params T[][] partition) {
     
    4553    private T[] GetPartition() {
    4654      T[] partition;
    47       return partitions.TryPop(out partition) ? partition : DeepArrayCopy(DummyPartition);
     55      return partitions.TryPop(out partition) ? partition : CloneDummyPartition();
    4856    }
    4957
    50     private readonly BinaryFormatter binaryFormatter = new BinaryFormatter();
    51     private T[] DeepArrayCopy(object objectToCopy) {
    52       using (var memoryStream = new MemoryStream()) {
    53         binaryFormatter.Serialize(memoryStream, objectToCopy);
     58    private T[] CloneDummyPartition() {
     59      if (dummyPartition == null)
     60        InitDummyPartition(factory);
     61
     62      using (var memoryStream = new MemoryStream(dummyPartition)) {
    5463        memoryStream.Seek(0, SeekOrigin.Begin);
    5564
     
    6271    }
    6372
    64     private class ManagedPool<T> : IManagedPool<T> where T : class {
     73    private class ManagedPool<T> : IManagedPool<T> where T : class, IPooledObject {
    6574      private readonly ManagedPoolProvider<T> provider;
    6675      private readonly IList<T[]> partitions = new List<T[]>();
     
    7382      }
    7483
    75       public T Get() {
     84      public T Get(bool reset = true) {
    7685        if (entryIndex == provider.PartitionSize) {
    7786          currentPartition = provider.GetPartition();
     
    8089        }
    8190
    82         return currentPartition[entryIndex++];
     91        var entry = currentPartition[entryIndex++];
     92
     93        if (reset) entry.Reset();
     94
     95        return entry;
    8396      }
    8497
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Pool/ObjectPool.cs

    r14727 r14777  
    2525  ///    If there is no intent for reusing the object, do not use pool - just use "new".
    2626  /// </summary>
    27   internal sealed class ObjectPool<T> where T : class {
     27  public sealed class ObjectPool<T> where T : class {
    2828    private struct Element {
    2929      internal T Value;
     
    3939
    4040
    41     internal ObjectPool(Func<T> factory)
     41    public ObjectPool(Func<T> factory)
    4242        : this(factory, Environment.ProcessorCount * 2) { }
    4343
    44     internal ObjectPool(Func<T> factory, int size) {
     44    public ObjectPool(Func<T> factory, int size) {
    4545      _factory = factory;
    4646      _items = new Element[size];
     
    6060    /// reducing how far we will typically search.
    6161    /// </remarks>
    62     internal T Allocate() {
     62    public T Allocate() {
    6363      var items = _items;
    6464      T inst;
     
    9090    /// reducing how far we will typically search in Allocate.
    9191    /// </remarks>
    92     internal void Free(T obj) {
     92    public void Free(T obj) {
    9393      var items = _items;
    9494      for (int i = 0; i < items.Length; i++) {
Note: See TracChangeset for help on using the changeset viewer.