namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter { using System; using System.Collections.Generic; using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; public class InterpreterPoolContainer { private readonly ManagedPoolProvider pushProgramPoolProvider; private readonly ManagedPoolProvider loopStatePoolProvider; private readonly ManagedPoolProvider> expressionListPoolProvider; private readonly IDictionary> statefulExpressionPoolProviders = new Dictionary>(); private readonly IDictionary> statefulExpressionPools = new Dictionary>(); public InterpreterPoolContainer(int partitionSize = 512, int maxPartitionCount = 1024) { pushProgramPoolProvider = new ManagedPoolProvider(partitionSize, () => new PushProgram(), maxPartitionCount); loopStatePoolProvider = new ManagedPoolProvider(partitionSize, () => new LoopState(), maxPartitionCount); expressionListPoolProvider = new ManagedPoolProvider>(partitionSize, () => new PooledList(), maxPartitionCount); InitStatefulExpressionPools(); } public InterpreterPoolContainer( ManagedPoolProvider pushProgramPoolProvider, ManagedPoolProvider loopStatePoolProvider, ManagedPoolProvider> expressionListPoolProvider) { this.pushProgramPoolProvider = pushProgramPoolProvider; this.loopStatePoolProvider = loopStatePoolProvider; this.expressionListPoolProvider = expressionListPoolProvider; InitStatefulExpressionPools(); } public IManagedPool PushProgramPool { get; private set; } public IManagedPool LoopStatePool { get; private set; } public IManagedPool> ExpressionListPool { get; private set; } private void InitStatefulExpressionPools() { for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) { var type = ExpressionTable.StatefulExpressionTypes[i]; var name = ExpressionTable.TypeToNameTable[type]; var factory = ExpressionTable.StatefulExpressionFactory[name]; statefulExpressionPoolProviders.Add(type, new ManagedPoolProvider(1024, factory)); statefulExpressionPools.Add(type, null); } } public IManagedPool GetStatefulExpressionPool() where T : StatefulExpression { return statefulExpressionPools[typeof(T)]; } public T GetStatefulExpression() where T : Expression { return (T)statefulExpressionPools[typeof(T)].Get(); } public void CreatePools() { PushProgramPool = pushProgramPoolProvider.CreatePool(); LoopStatePool = loopStatePoolProvider.CreatePool(); ExpressionListPool = expressionListPoolProvider.CreatePool(); for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) { var type = ExpressionTable.StatefulExpressionTypes[i]; statefulExpressionPools[type] = statefulExpressionPoolProviders[type].CreatePool(); } } public void DisposePools() { if (PushProgramPool != null) PushProgramPool.Dispose(); if (LoopStatePool != null) LoopStatePool.Dispose(); if (ExpressionListPool != null) ExpressionListPool.Dispose(); for (var i = 0; i < ExpressionTable.StatefulExpressionTypes.Length; i++) { var type = ExpressionTable.StatefulExpressionTypes[i]; if (statefulExpressionPools[type] != null) statefulExpressionPools[type].Dispose(); } } } }