1 | using System.Collections.Generic;
|
---|
2 |
|
---|
3 | namespace 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 | }
|
---|
13 |
|
---|
14 | public class ManagedPoolProvider<T> where T : class, IPooledObject {
|
---|
15 | private readonly ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>();
|
---|
16 | private readonly ObjectPool<IManagedPool<T>> managedPools;
|
---|
17 | private readonly BinaryFormatter binaryFormatter = new BinaryFormatter();
|
---|
18 | private byte[] dummyPartition;
|
---|
19 |
|
---|
20 | private static readonly FieldInfo InternalListArrayProperty = typeof(List<T[]>).GetField(
|
---|
21 | "_items",
|
---|
22 | BindingFlags.NonPublic | BindingFlags.Instance);
|
---|
23 |
|
---|
24 | private readonly Func<T> factory;
|
---|
25 |
|
---|
26 | public readonly int PartitionSize;
|
---|
27 | public readonly int MaxPartitionCount;
|
---|
28 | public const int DefaultMaxInstanceCount = 65536;
|
---|
29 |
|
---|
30 | public ManagedPoolProvider(int partitionSize, Func<T> factory, int? maxPartitionCount = null) {
|
---|
31 | PartitionSize = partitionSize;
|
---|
32 | MaxPartitionCount = maxPartitionCount ?? DefaultMaxInstanceCount / PartitionSize;
|
---|
33 | this.factory = factory;
|
---|
34 |
|
---|
35 | managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool(this));
|
---|
36 | }
|
---|
37 | public int InstanceCount { get { return partitions.Count * PartitionSize; } }
|
---|
38 |
|
---|
39 | private void InitDummyPartition(Func<T> factory) {
|
---|
40 | var temp = new T[PartitionSize];
|
---|
41 |
|
---|
42 | for (var i = 0u; i < PartitionSize; i++) {
|
---|
43 | temp[i] = factory();
|
---|
44 | }
|
---|
45 |
|
---|
46 | using (var memoryStream = new MemoryStream()) {
|
---|
47 | binaryFormatter.Serialize(memoryStream, temp);
|
---|
48 | dummyPartition = memoryStream.ToArray();
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void Clear() {
|
---|
53 | partitions.Clear();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void ReleasePartitions(List<T[]> releasedPartitions) {
|
---|
57 | if (partitions.Count < MaxPartitionCount)
|
---|
58 | partitions.PushRange((T[][])InternalListArrayProperty.GetValue(releasedPartitions), 0, releasedPartitions.Count);
|
---|
59 | }
|
---|
60 |
|
---|
61 | private T[] GetPartition() {
|
---|
62 | T[] partition;
|
---|
63 | return partitions.TryPop(out partition) ? partition : CloneDummyPartition();
|
---|
64 | }
|
---|
65 |
|
---|
66 | private T[] CloneDummyPartition() {
|
---|
67 | if (dummyPartition == null)
|
---|
68 | InitDummyPartition(factory);
|
---|
69 |
|
---|
70 | using (var memoryStream = new MemoryStream(dummyPartition)) {
|
---|
71 | memoryStream.Seek(0, SeekOrigin.Begin);
|
---|
72 |
|
---|
73 | return (T[])binaryFormatter.Deserialize(memoryStream);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public IManagedPool<T> CreatePool() {
|
---|
78 | return managedPools.Allocate();
|
---|
79 | }
|
---|
80 |
|
---|
81 | private class ManagedPool : IManagedPool<T> {
|
---|
82 | private readonly ManagedPoolProvider<T> provider;
|
---|
83 | private readonly List<T[]> partitions = new List<T[]>();
|
---|
84 | private T[] currentPartition;
|
---|
85 | private int entryIndex;
|
---|
86 |
|
---|
87 | public ManagedPool(ManagedPoolProvider<T> provider) {
|
---|
88 | this.provider = provider;
|
---|
89 | entryIndex = provider.PartitionSize;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public T Get(bool resetEntry = true) {
|
---|
93 | if (entryIndex == provider.PartitionSize) {
|
---|
94 | currentPartition = provider.GetPartition();
|
---|
95 | partitions.Add(currentPartition);
|
---|
96 | entryIndex = 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | var entry = currentPartition[entryIndex++];
|
---|
100 | if (resetEntry) entry.Reset();
|
---|
101 | return entry;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void Dispose() {
|
---|
105 | if (partitions.Count > 0) {
|
---|
106 | provider.ReleasePartitions(partitions);
|
---|
107 | partitions.Clear();
|
---|
108 | currentPartition = null;
|
---|
109 | entryIndex = provider.PartitionSize;
|
---|
110 | }
|
---|
111 |
|
---|
112 | provider.managedPools.Free(this);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|