[15771] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic; |
---|
| 3 | |
---|
| 4 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
[14777] | 5 | public class InterpreterPoolContainer {
|
---|
| 6 | private readonly ManagedPoolProvider<PushProgram> pushProgramPoolProvider;
|
---|
| 7 | private readonly ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider;
|
---|
| 8 |
|
---|
| 9 | private readonly IDictionary<Type, ManagedPoolProvider<Expression>> statefulExpressionPoolProviders
|
---|
| 10 | = new Dictionary<Type, ManagedPoolProvider<Expression>>();
|
---|
| 11 |
|
---|
| 12 | private readonly IDictionary<Type, IManagedPool<Expression>> statefulExpressionPools
|
---|
| 13 | = new Dictionary<Type, IManagedPool<Expression>>();
|
---|
| 14 |
|
---|
| 15 | public InterpreterPoolContainer(int partitionSize = 512, int maxPartitionCount = 1024) {
|
---|
| 16 | pushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(partitionSize, () => new PushProgram(), maxPartitionCount);
|
---|
| 17 | expressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(partitionSize, () => new PooledList<Expression>(), maxPartitionCount);
|
---|
| 18 |
|
---|
[14834] | 19 | InitStatefulExpressionPools(partitionSize, maxPartitionCount);
|
---|
[14777] | 20 | }
|
---|
| 21 |
|
---|
| 22 | public InterpreterPoolContainer(
|
---|
| 23 | ManagedPoolProvider<PushProgram> pushProgramPoolProvider,
|
---|
| 24 | ManagedPoolProvider<PooledList<Expression>> expressionListPoolProvider) {
|
---|
| 25 | this.pushProgramPoolProvider = pushProgramPoolProvider;
|
---|
| 26 | this.expressionListPoolProvider = expressionListPoolProvider;
|
---|
| 27 |
|
---|
[14834] | 28 | InitStatefulExpressionPools(1024, 2048);
|
---|
[14777] | 29 | }
|
---|
| 30 |
|
---|
[14834] | 31 | private IManagedPool<PushProgram> pushProgramPool;
|
---|
[15771] | 32 | public IManagedPool<PushProgram> PushProgramPool {
|
---|
| 33 | get {
|
---|
[14834] | 34 | if (pushProgramPool == null) pushProgramPool = pushProgramPoolProvider.CreatePool();
|
---|
| 35 | return pushProgramPool;
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
[14777] | 38 |
|
---|
[14834] | 39 | private IManagedPool<PooledList<Expression>> expressionListPool;
|
---|
[15771] | 40 | public IManagedPool<PooledList<Expression>> ExpressionListPool {
|
---|
| 41 | get {
|
---|
[14834] | 42 | if (expressionListPool == null) expressionListPool = expressionListPoolProvider.CreatePool();
|
---|
| 43 | return expressionListPool;
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private void InitStatefulExpressionPools(int partitionSize, int maxPartitionCount) {
|
---|
[14952] | 48 | foreach (var type in ExpressionTable.StatefulExpressionTypes) {
|
---|
[14834] | 49 | statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>(
|
---|
| 50 | partitionSize,
|
---|
[14952] | 51 | ExpressionTable.StatefulExpressionFactory[type],
|
---|
[14834] | 52 | maxPartitionCount));
|
---|
[14777] | 53 |
|
---|
| 54 | statefulExpressionPools.Add(type, null);
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[14834] | 58 | public T GetStatefulExpression<T>() where T : Expression {
|
---|
| 59 | var type = typeof(T);
|
---|
| 60 | if (statefulExpressionPools[type] == null)
|
---|
| 61 | statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool();
|
---|
[14777] | 62 |
|
---|
[14834] | 63 | return (T)statefulExpressionPools[type].Get();
|
---|
[14777] | 64 | }
|
---|
| 65 |
|
---|
[14952] | 66 | public IManagedPool<Expression> GetStatefulExpressionPool<T>() where T : Expression {
|
---|
| 67 | var type = typeof(T);
|
---|
| 68 | return statefulExpressionPools[type] ?? (statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool());
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[14834] | 71 | public void DisposePools() {
|
---|
| 72 | if (pushProgramPool != null) {
|
---|
| 73 | pushProgramPool.Dispose();
|
---|
| 74 | pushProgramPool = null;
|
---|
| 75 | }
|
---|
[14777] | 76 |
|
---|
[14834] | 77 | if (expressionListPool != null) {
|
---|
| 78 | expressionListPool.Dispose();
|
---|
| 79 | expressionListPool = null;
|
---|
| 80 | }
|
---|
[14777] | 81 |
|
---|
[14875] | 82 | for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Count; i++) {
|
---|
[14777] | 83 | var type = ExpressionTable.StatefulExpressionTypes[i];
|
---|
[14834] | 84 | var pool = statefulExpressionPools[type];
|
---|
| 85 |
|
---|
| 86 | if (pool != null) {
|
---|
| 87 | pool.Dispose();
|
---|
| 88 | statefulExpressionPools[type] = null;
|
---|
| 89 | }
|
---|
[14777] | 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|