Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Pool/ManagedPoolProvider.cs @ 15189

Last change on this file since 15189 was 15189, checked in by pkimmesw, 7 years ago

#2665 Fixed small issues, testet benchmark suite, added INX Expressions

File size: 4.1 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool {
4  using System;
5  using System.Collections.Concurrent;
6  using System.IO;
7  using System.Reflection;
8  using System.Runtime.Serialization.Formatters.Binary;
9
10  public interface IManagedPool<T> : IDisposable where T : class, IPooledObject {
11    T Get(bool reset = true);
12    void Release();
13  }
14
15  public class ManagedPoolProvider<T> where T : class, IPooledObject {
16    private readonly ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>();
17    private readonly ObjectPool<IManagedPool<T>> managedPools;
18    private readonly BinaryFormatter binaryFormatter = new BinaryFormatter();
19    private byte[] dummyPartition;
20    private static volatile object dummyCreationLockObject = new object();
21
22    private static readonly FieldInfo InternalListArrayProperty = typeof(List<T[]>).GetField(
23      "_items",
24      BindingFlags.NonPublic | BindingFlags.Instance);
25
26    private readonly Func<T> factory;
27
28    public readonly int PartitionSize;
29    public readonly int MaxPartitionCount;
30    public const int DefaultMaxInstanceCount = 65536;
31
32    public ManagedPoolProvider(int partitionSize, Func<T> factory, int? maxPartitionCount = null) {
33      PartitionSize = partitionSize;
34      MaxPartitionCount = maxPartitionCount ?? DefaultMaxInstanceCount / PartitionSize;
35      this.factory = factory;
36
37      managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool(this));
38    }
39    public int InstanceCount { get { return partitions.Count * PartitionSize; } }
40
41    public void Clear() {
42      dummyPartition = null;
43      managedPools.Clear();
44      partitions.Clear();
45    }
46
47    public void ReleasePartitions(List<T[]> releasedPartitions) {
48      if (partitions.Count < MaxPartitionCount)
49        partitions.PushRange((T[][])InternalListArrayProperty.GetValue(releasedPartitions), 0, releasedPartitions.Count);
50    }
51
52    private T[] GetPartition() {
53      T[] partition;
54      return partitions.TryPop(out partition) ? partition : CloneDummyPartition();
55    }
56
57    private T[] CloneDummyPartition() {
58      if (dummyPartition == null) {
59        lock (dummyCreationLockObject) {
60          if (dummyPartition == null) {
61            var temp = new T[PartitionSize];
62
63            for (var i = 0u; i < PartitionSize; i++) {
64              temp[i] = factory();
65            }
66
67            using (var memoryStream = new MemoryStream()) {
68              binaryFormatter.Serialize(memoryStream, temp);
69              dummyPartition = memoryStream.ToArray();
70            }
71          }
72        }
73      }
74
75      using (var memoryStream = new MemoryStream(dummyPartition)) {
76        memoryStream.Seek(0, SeekOrigin.Begin);
77
78        var result = (T[])binaryFormatter.Deserialize(memoryStream);
79
80        for (var i = 0; i < result.Length; i++)
81          result[i].Init();
82
83        return result;
84      }
85    }
86
87    public IManagedPool<T> CreatePool() {
88      return managedPools.Allocate();
89    }
90
91    private class ManagedPool : IManagedPool<T> {
92      private readonly ManagedPoolProvider<T> provider;
93      private readonly List<T[]> partitions = new List<T[]>();
94      private T[] currentPartition;
95      private int entryIndex;
96
97      public ManagedPool(ManagedPoolProvider<T> provider) {
98        this.provider = provider;
99        entryIndex = provider.PartitionSize;
100      }
101
102      public T Get(bool resetEntry = true) {
103        if (entryIndex == provider.PartitionSize) {
104          currentPartition = provider.GetPartition();
105          partitions.Add(currentPartition);
106          entryIndex = 0;
107        }
108
109        var entry = currentPartition[entryIndex++];
110        if (resetEntry) entry.Reset();
111        return entry;
112      }
113
114      public void Release() {
115        if (partitions.Count > 0) {
116          provider.ReleasePartitions(partitions);
117          partitions.Clear();
118          currentPartition = null;
119          entryIndex = provider.PartitionSize;
120        }
121      }
122
123      public void Dispose() {
124        Release();
125
126        provider.managedPools.Free(this);
127      }
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.