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