Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/TestPooling/Pool/ManagedPoolProvider.cs @ 14745

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

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

File size: 2.6 KB
Line 
1using System;
2
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;
9
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;
15
16    public int InstanceCount { get { return partitions.Count * PartitionSize; } }
17
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];
24
25      for (var i = 0; i < PartitionSize; i++) {
26        DummyPartition[i] = factory();
27      }
28    }
29
30    public void ReleasePartitions(params T[][] partition) {
31      if (partition.Length > 0) partitions.PushRange(partition);
32    }
33
34    private T[] GetPartition() {
35      T[] partition;
36      return partitions.TryPop(out partition) ? partition : DeepArrayCopy(DummyPartition);
37    }
38
39    private static T[] DeepArrayCopy(object objectToCopy) {
40      using (var memoryStream = new MemoryStream()) {
41        var binaryFormatter = new BinaryFormatter();
42
43        binaryFormatter.Serialize(memoryStream, objectToCopy);
44        memoryStream.Seek(0, SeekOrigin.Begin);
45
46        return (T[])binaryFormatter.Deserialize(memoryStream);
47      }
48    }
49
50    public IManagedPool<T> CreatePool() {
51      return managedPools.Allocate();
52    }
53
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 TracBrowser for help on using the repository browser.