[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 |
|
---|
| 24 | InitStatefulExpressionPools();
|
---|
| 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 |
|
---|
| 35 | InitStatefulExpressionPools();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public IManagedPool<PushProgram> PushProgramPool { get; private set; }
|
---|
| 39 | public IManagedPool<LoopState> LoopStatePool { get; private set; }
|
---|
| 40 | public IManagedPool<PooledList<Expression>> ExpressionListPool { get; private set; }
|
---|
| 41 |
|
---|
| 42 | private void InitStatefulExpressionPools() {
|
---|
| 43 | for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) {
|
---|
| 44 | var type = ExpressionTable.StatefulExpressionTypes[i];
|
---|
| 45 | var name = ExpressionTable.TypeToNameTable[type];
|
---|
| 46 | var factory = ExpressionTable.StatefulExpressionFactory[name];
|
---|
| 47 |
|
---|
| 48 | statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider<Expression>(1024, factory));
|
---|
| 49 |
|
---|
| 50 | statefulExpressionPools.Add(type, null);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public IManagedPool<Expression> GetStatefulExpressionPool<T, StateT>() where T : StatefulExpression<StateT> {
|
---|
| 55 | return statefulExpressionPools[typeof(T)];
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public T GetStatefulExpression<T>() where T : Expression {
|
---|
| 59 | return (T)statefulExpressionPools[typeof(T)].Get();
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public void CreatePools() {
|
---|
| 63 | PushProgramPool = pushProgramPoolProvider.CreatePool();
|
---|
| 64 | LoopStatePool = loopStatePoolProvider.CreatePool();
|
---|
| 65 | ExpressionListPool = expressionListPoolProvider.CreatePool();
|
---|
| 66 |
|
---|
| 67 | for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) {
|
---|
| 68 | var type = ExpressionTable.StatefulExpressionTypes[i];
|
---|
| 69 | statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool();
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public void DisposePools() {
|
---|
| 74 | if (PushProgramPool != null) PushProgramPool.Dispose();
|
---|
| 75 | if (LoopStatePool != null) LoopStatePool.Dispose();
|
---|
| 76 | if (ExpressionListPool != null) ExpressionListPool.Dispose();
|
---|
| 77 |
|
---|
| 78 | for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) {
|
---|
| 79 | var type = ExpressionTable.StatefulExpressionTypes[i];
|
---|
| 80 | if (statefulExpressionPools[type] != null) statefulExpressionPools[type].Dispose();
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|