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