Changeset 14777 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Pool
- Timestamp:
- 03/23/17 01:11:18 (8 years ago)
- 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 8 8 using System.Runtime.Serialization.Formatters.Binary; 9 9 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); 12 12 } 13 13 14 public class ManagedPoolProvider<T> where T : class {14 public class ManagedPoolProvider<T> where T : class, IPooledObject { 15 15 private readonly ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>(); 16 16 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; 18 21 19 22 public readonly int PartitionSize; … … 21 24 public const int DefaultMaxInstanceCount = 65536; 22 25 23 public ManagedPoolProvider(int partitionSize, int? maxPartitionCount = null) {26 public ManagedPoolProvider(int partitionSize, Func<T> factory, int? maxPartitionCount = null) { 24 27 PartitionSize = partitionSize; 25 28 MaxParitionCount = maxPartitionCount ?? DefaultMaxInstanceCount / PartitionSize; 29 this.factory = factory; 26 30 27 managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool<T>(this) );31 managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool<T>(this), MaxParitionCount); 28 32 } 33 public int InstanceCount { get { return partitions.Count * PartitionSize; } } 29 34 30 p ublicvoid InitDummyPartition(Func<T> factory) {31 DummyPartition= new T[PartitionSize];35 private void InitDummyPartition(Func<T> factory) { 36 var temp = new T[PartitionSize]; 32 37 33 38 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(); 35 45 } 36 46 } 37 38 public int InstanceCount { get { return partitions.Count * PartitionSize; } }39 47 40 48 public void ReleasePartitions(params T[][] partition) { … … 45 53 private T[] GetPartition() { 46 54 T[] partition; 47 return partitions.TryPop(out partition) ? partition : DeepArrayCopy(DummyPartition);55 return partitions.TryPop(out partition) ? partition : CloneDummyPartition(); 48 56 } 49 57 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)) { 54 63 memoryStream.Seek(0, SeekOrigin.Begin); 55 64 … … 62 71 } 63 72 64 private class ManagedPool<T> : IManagedPool<T> where T : class {73 private class ManagedPool<T> : IManagedPool<T> where T : class, IPooledObject { 65 74 private readonly ManagedPoolProvider<T> provider; 66 75 private readonly IList<T[]> partitions = new List<T[]>(); … … 73 82 } 74 83 75 public T Get( ) {84 public T Get(bool reset = true) { 76 85 if (entryIndex == provider.PartitionSize) { 77 86 currentPartition = provider.GetPartition(); … … 80 89 } 81 90 82 return currentPartition[entryIndex++]; 91 var entry = currentPartition[entryIndex++]; 92 93 if (reset) entry.Reset(); 94 95 return entry; 83 96 } 84 97 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Pool/ObjectPool.cs
r14727 r14777 25 25 /// If there is no intent for reusing the object, do not use pool - just use "new". 26 26 /// </summary> 27 internalsealed class ObjectPool<T> where T : class {27 public sealed class ObjectPool<T> where T : class { 28 28 private struct Element { 29 29 internal T Value; … … 39 39 40 40 41 internalObjectPool(Func<T> factory)41 public ObjectPool(Func<T> factory) 42 42 : this(factory, Environment.ProcessorCount * 2) { } 43 43 44 internalObjectPool(Func<T> factory, int size) {44 public ObjectPool(Func<T> factory, int size) { 45 45 _factory = factory; 46 46 _items = new Element[size]; … … 60 60 /// reducing how far we will typically search. 61 61 /// </remarks> 62 internalT Allocate() {62 public T Allocate() { 63 63 var items = _items; 64 64 T inst; … … 90 90 /// reducing how far we will typically search in Allocate. 91 91 /// </remarks> 92 internalvoid Free(T obj) {92 public void Free(T obj) { 93 93 var items = _items; 94 94 for (int i = 0; i < items.Length; i++) {
Note: See TracChangeset
for help on using the changeset viewer.