Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/11/17 12:42:31 (7 years ago)
Author:
pkimmesw
Message:

#2665 Merged ExecExpandExpression and PushProgram due to performance reasons, Tested managed object pooling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/TestPooling/Pool/ManagedPoolProvider.cs

    r14744 r14745  
    1 //using System;
     1using System;
    22
    3 //namespace TestPooling.Pool {
    4 //  using System.Collections.Concurrent;
    5 //  using System.Threading.Tasks;
     3namespace TestPooling.Pool {
     4  using System.Collections.Concurrent;
     5  using System.Collections.Generic;
     6  using System.IO;
     7  using System.Linq;
     8  using System.Runtime.Serialization.Formatters.Binary;
    69
    7 //  public class ManagedPoolProvider<T> where T : class {
    8 //    private static readonly volatile ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>();
    9 //    private readonly Func<T> Factory;
    10 //    public readonly int PartitionSize;
     10  public class ManagedPoolProvider<T> where T : class {
     11    private readonly ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>();
     12    private readonly ObjectPool<IManagedPool<T>> managedPools;
     13    private T[] DummyPartition;
     14    public readonly int PartitionSize;
    1115
    12 //    public ManagedPoolProvider(int partitionSize, Func<T> factory) {
    13 //      PartitionSize = partitionSize;
    14 //      Factory = factory;
    15 //    }
     16    public int InstanceCount { get { return partitions.Count * PartitionSize; } }
    1617
    17 //    public T[] GetPartition() {
    18 //      T[] partition;
     18    public ManagedPoolProvider(int partitionSize) {
     19      PartitionSize = partitionSize;
     20      managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool2<T>(this));
     21    }
     22    public void InitDummyPartition(Func<T> factory) {
     23      DummyPartition = new T[PartitionSize];
    1924
    20 //      return partitions.TryPop(out partition) ? partition : CreatePartition();
    21 //    }
     25      for (var i = 0; i < PartitionSize; i++) {
     26        DummyPartition[i] = factory();
     27      }
     28    }
    2229
    23 //    public void ReleasePartitions(params T[][] partition) {
    24 //      partitions.PushRange(partition);
    25 //    }
     30    public void ReleasePartitions(params T[][] partition) {
     31      if (partition.Length > 0) partitions.PushRange(partition);
     32    }
    2633
    27 //    public T CreateEntry() {
    28 //      return Factory();
    29 //    }
     34    private T[] GetPartition() {
     35      T[] partition;
     36      return partitions.TryPop(out partition) ? partition : DeepArrayCopy(DummyPartition);
     37    }
    3038
    31 //    private T[] CreatePartition() {
    32 //      //return new T[PartitionSize];
     39    private static T[] DeepArrayCopy(object objectToCopy) {
     40      using (var memoryStream = new MemoryStream()) {
     41        var binaryFormatter = new BinaryFormatter();
    3342
    34 //      var partition = new T[PartitionSize];
     43        binaryFormatter.Serialize(memoryStream, objectToCopy);
     44        memoryStream.Seek(0, SeekOrigin.Begin);
    3545
    36 //      Parallel.For(0, PartitionSize, i => partition[i] = Factory());
     46        return (T[])binaryFormatter.Deserialize(memoryStream);
     47      }
     48    }
    3749
    38 //      return partition;
    39 //    }
     50    public IManagedPool<T> CreatePool() {
     51      return managedPools.Allocate();
     52    }
    4053
    41 //    public ManagedPool<T> CreatePool() {
    42 //      return new ManagedPool<T>(this);
    43 //    }
    44 //  }
    45 //}
     54    private class ManagedPool2<T> : IManagedPool<T> where T : class {
     55      private readonly ManagedPoolProvider<T> provider;
     56      private readonly IList<T[]> partitions = new List<T[]>();
     57      private T[] currentPartition;
     58      private int entryIndex;
     59
     60      public ManagedPool2(ManagedPoolProvider<T> provider) {
     61        this.provider = provider;
     62        entryIndex = provider.PartitionSize;
     63      }
     64
     65      public T Get() {
     66        if (entryIndex == provider.PartitionSize) {
     67          currentPartition = provider.GetPartition();
     68          partitions.Add(currentPartition);
     69          entryIndex = 0;
     70        }
     71
     72        return currentPartition[entryIndex++];
     73      }
     74
     75      public void Dispose() {
     76        provider.ReleasePartitions(partitions.ToArray());
     77        partitions.Clear();
     78        provider.managedPools.Free(this);
     79      }
     80    }
     81  }
     82}
Note: See TracChangeset for help on using the changeset viewer.