Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 PooledPushProgram reduces memory usage and increases performance

File size: 3.0 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.Linq;
8  using System.Runtime.Serialization.Formatters.Binary;
9
10  public interface IManagedPool<T> : IDisposable where T : class {
11    T Get();
12  }
13
14  public class ManagedPoolProvider<T> where T : class {
15    private readonly ConcurrentStack<T[]> partitions = new ConcurrentStack<T[]>();
16    private readonly ObjectPool<IManagedPool<T>> managedPools;
17    private T[] DummyPartition;
18
19    public readonly int PartitionSize;
20    public readonly int MaxParitionCount;
21    public const int DefaultMaxInstanceCount = 65536;
22
23    public ManagedPoolProvider(int partitionSize, int maxPartitionCount = -1) {
24      PartitionSize = partitionSize;
25
26      if (maxPartitionCount <= 0) {
27        MaxParitionCount = DefaultMaxInstanceCount / PartitionSize;
28      }
29
30      managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool2<T>(this));
31    }
32
33    public void InitDummyPartition(Func<T> factory) {
34      DummyPartition = new T[PartitionSize];
35
36      for (var i = 0; i < PartitionSize; i++) {
37        DummyPartition[i] = factory();
38      }
39    }
40
41    public int InstanceCount { get { return partitions.Count * PartitionSize; } }
42
43    public void ReleasePartitions(params T[][] partition) {
44      if (partitions.Count <= MaxParitionCount && partition.Length > 0)
45        partitions.PushRange(partition);
46    }
47
48    private T[] GetPartition() {
49      T[] partition;
50      return partitions.TryPop(out partition) ? partition : DeepArrayCopy(DummyPartition);
51    }
52
53    private readonly BinaryFormatter binaryFormatter = new BinaryFormatter();
54    private T[] DeepArrayCopy(object objectToCopy) {
55      using (var memoryStream = new MemoryStream()) {
56        binaryFormatter.Serialize(memoryStream, objectToCopy);
57        memoryStream.Seek(0, SeekOrigin.Begin);
58
59        return (T[])binaryFormatter.Deserialize(memoryStream);
60      }
61    }
62
63    public IManagedPool<T> CreatePool() {
64      return managedPools.Allocate();
65    }
66
67    private class ManagedPool2<T> : IManagedPool<T> where T : class {
68      private readonly ManagedPoolProvider<T> provider;
69      private readonly IList<T[]> partitions = new List<T[]>();
70      private T[] currentPartition;
71      private int entryIndex;
72
73      public ManagedPool2(ManagedPoolProvider<T> provider) {
74        this.provider = provider;
75        entryIndex = provider.PartitionSize;
76      }
77
78      public T Get() {
79        if (entryIndex == provider.PartitionSize) {
80          currentPartition = provider.GetPartition();
81          partitions.Add(currentPartition);
82          entryIndex = 0;
83        }
84
85        return currentPartition[entryIndex++];
86      }
87
88      public void Dispose() {
89        provider.ReleasePartitions(partitions.ToArray());
90        partitions.Clear();
91        entryIndex = provider.PartitionSize;
92        provider.managedPools.Free(this);
93      }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.