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