[15771] | 1 | using System;
|
---|
| 2 | using HeuristicLab.Core; |
---|
| 3 | |
---|
| 4 | namespace HeuristicLab.Problems.ProgramSynthesis {
|
---|
[14744] | 5 | public class PushInterpreterPool {
|
---|
| 6 | private readonly ObjectPool<PooledPushInterpreter> pool;
|
---|
| 7 |
|
---|
[14777] | 8 | public readonly ManagedPoolProvider<PushProgram> PushProgramPoolProvider;
|
---|
| 9 | public readonly ManagedPoolProvider<PooledList<Expression>> ExpressionListPoolProvider;
|
---|
| 10 |
|
---|
| 11 | public PushInterpreterPool(IReadOnlyPushConfiguration config = null)
|
---|
[15032] | 12 | : this(Environment.ProcessorCount * 4, 1024, null, config) {
|
---|
[14744] | 13 | }
|
---|
| 14 |
|
---|
[14834] | 15 | public PushInterpreterPool(int size, int poolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) {
|
---|
[15017] | 16 | PushConfiguration = config ?? new PushConfiguration();
|
---|
[14744] | 17 |
|
---|
[14834] | 18 | PushProgramPoolProvider = new ManagedPoolProvider<PushProgram>(poolPartitionSize, () => new PushProgram(), maxPartitionCount);
|
---|
[15017] | 19 | ExpressionListPoolProvider = new ManagedPoolProvider<PooledList<Expression>>(poolPartitionSize, () => new PooledList<Expression>(), maxPartitionCount);
|
---|
[14747] | 20 |
|
---|
[15334] | 21 | pool = new ObjectPool<PooledPushInterpreter>(CreateInterpreter, size);
|
---|
[14744] | 22 | }
|
---|
| 23 |
|
---|
[15334] | 24 | private PooledPushInterpreter CreateInterpreter() {
|
---|
| 25 | var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, ExpressionListPoolProvider);
|
---|
| 26 | return new PooledPushInterpreter(this, PushConfiguration, poolContainer);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[15017] | 29 | public IReadOnlyPushConfiguration PushConfiguration { get; private set; }
|
---|
[14744] | 30 |
|
---|
[14777] | 31 | public PooledPushInterpreter Create(IRandom random = null) {
|
---|
[15341] | 32 | var interpreter = pool.Allocate();
|
---|
| 33 | interpreter.Reset(random);
|
---|
[14744] | 34 |
|
---|
| 35 | return interpreter;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public void Free(PooledPushInterpreter interpreter) {
|
---|
[15273] | 39 | interpreter.ClearStacks();
|
---|
[14834] | 40 | pool.Free(interpreter);
|
---|
[14744] | 41 | }
|
---|
[15017] | 42 |
|
---|
| 43 | public void Clear() {
|
---|
| 44 | PushProgramPoolProvider.Clear();
|
---|
| 45 | ExpressionListPoolProvider.Clear();
|
---|
[15189] | 46 | pool.Clear();
|
---|
[15017] | 47 | }
|
---|
[14744] | 48 | }
|
---|
| 49 | }
|
---|