forked from simoneguidi94/gopapageno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaopp.go
57 lines (45 loc) · 1.8 KB
/
aopp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package gopapageno
type AOPParser struct {
*OPParser
}
func NewAOPParser(g *Grammar, src []byte, opts *RunOptions) *AOPParser {
p := &AOPParser{
OPParser: &OPParser{
g: g,
concurrency: opts.Concurrency,
reductionStrategy: opts.ReductionStrategy,
workers: make([]*oppWorker, opts.Concurrency),
results: make([]*OPPStack, opts.Concurrency),
},
}
srcLen := len(src)
stackPoolBaseSize := stacksCountFactored[*Token](src, opts)
ntPoolBaseSize := srcLen / opts.AvgTokenLength / p.concurrency
// Initialize memory pools for stacks.
p.pools.stacks = make([]*Pool[stack[*Token]], p.concurrency)
// Initialize pools to hold pointers to tokens generated by the reduction steps.
p.pools.nonterminals = make([]*Pool[Token], p.concurrency)
stackMult := 1.0
if stackPoolBaseSize == 0 {
stackMult = 1.0 - (0.999 * opts.ParallelFactor)
}
stackLen := stackLengthFor[*Token](stackMult)
for thread := 0; thread < p.concurrency; thread++ {
p.pools.stacks[thread] = NewPool(stackPoolBaseSize+1, WithConstructor(newStackFactory[*Token](stackLen)))
p.pools.nonterminals[thread] = NewPool[Token](ntPoolBaseSize)
}
// If reduction is sweep or mixed, we create another stack and input for the final pass.
if p.concurrency > 1 && (p.reductionStrategy == ReductionSweep || p.reductionStrategy == ReductionMixed) {
inputPoolBaseSize := stacksCount[Token](src, p.concurrency, opts.AvgTokenLength)
p.pools.sweepInput = NewPool(inputPoolBaseSize, WithConstructor(newStack[Token]))
p.pools.sweepStack = NewPool(stackPoolBaseSize+1, WithConstructor(newStackFactory[*Token](stackLen)))
}
for thread := 0; thread < p.concurrency; thread++ {
p.workers[thread] = &oppWorker{
parser: p.OPParser,
id: thread,
ntPool: p.pools.nonterminals[thread],
}
}
return p
}