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