forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.go
227 lines (216 loc) · 5.59 KB
/
optimizer.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package template
import (
"bytes"
"fmt"
"gondola/util/generic"
)
type instructions []inst
func (i instructions) replace(idx int, count int, repl []inst) []inst {
// look for jumps before the block which need to be adjusted
for ii, v := range i[:idx] {
switch v.op {
case opJMP, opJMPT, opJMPF, opNEXT:
val := int(int32(v.val))
if ii+val > idx {
i[ii] = inst{v.op, valType(int32(val + len(repl) - count))}
}
}
}
// look for jumps after the block which need to be adjusted
start := idx + count
for ii, v := range i[start:] {
switch v.op {
case opJMP, opJMPT, opJMPF, opNEXT:
val := int(int32(v.val))
if ii+val < 0 {
i[ii+start] = inst{v.op, valType(int32(val - len(repl) + count))}
}
}
}
var ret []inst
ret = append(ret, i[:idx]...)
ret = append(ret, repl...)
ret = append(ret, i[idx+count:]...)
return ret
}
func (i instructions) replaceTemplateInvocation(idx int, repl []inst) []inst {
ti := i[idx]
if ti.op != opTEMPLATE {
panic(fmt.Errorf("OP at index %d is not opTEMPLATE", idx))
}
// Check if we can also remove the pipeline which provides arguments
// to the template invocation.
count := 1
if idx < len(i)-1 {
pi := i[idx+1]
if pi.op == opPOP {
// Remove the POP
count++
// This opPOP removes the arguments which were set up for
// the template call
if pi.val == 0 {
// POP until the mark
for {
count++
idx--
if i[idx].op == opMARK {
break
}
}
} else {
// Remove until we find as many pushes as this
// pop removes
stack := int(pi.val)
for stack > 0 {
count++
idx--
ii := i[idx]
stack -= ii.pushes()
stack += ii.pops()
}
}
}
}
return i.replace(idx, count, repl)
}
// Returns true iff the program references the byte slice determined
// by val
func (p *program) referencesBytes(val valType) bool {
for _, code := range p.code {
for _, pi := range code {
if pi.op == opWB && pi.val == val {
return true
}
}
}
return false
}
// removeInternedBytes removes the internet byte slice determined
// by val, adjusting all required instructions.
func (p *program) removeInternedBytes(val valType) {
idx := int(val)
p.bs = append(p.bs[:idx], p.bs[idx+1:]...)
for _, code := range p.code {
for ii, pi := range code {
if pi.op == opWB && pi.val >= val {
ni := pi
ni.val = pi.val - 1
code[ii] = ni
}
}
}
}
func (p *program) optimize() {
p.removeEmptyTemplateInvocations()
p.stitch()
p.mergeWriteBytes()
}
// mergeWriteBytes merges adjacent opWB operations into a single WB
func (p *program) mergeWriteBytes() {
removedReferences := make(map[valType]struct{})
for name, code := range p.code {
wb := -1
ii := 0
checkWBMerge := func() {
if wb >= 0 {
// We had a WB sequence, check its length
// and merge it if appropriate
count := ii - wb
if count > 1 {
var buf bytes.Buffer
var refs []valType
for c := wb; c < ii; c++ {
wbInst := code[c]
removedReferences[wbInst.val] = struct{}{}
refs = append(refs, wbInst.val)
buf.Write(p.bytesValue(wbInst.val))
}
repl := []inst{
inst{op: opWB, val: p.internBytes(buf.Bytes())},
}
code = instructions(code).replace(wb, count, repl)
compilerDebugf("merged %d WB instructions with byte slice refs %v at PC %d (new ref %d)\n",
count, refs, wb, repl[0].val)
}
}
}
for ; ii < len(code); ii++ {
v := code[ii]
if v.op == opWB {
if wb < 0 {
wb = ii
}
} else {
checkWBMerge()
wb = -1
}
}
checkWBMerge()
p.code[name] = code
}
// Sort references from higher to lower, so we
// don't invalidate them while they're being removed
sortedReferences := make([]valType, 0, len(removedReferences))
for r := range removedReferences {
sortedReferences = append(sortedReferences, r)
}
generic.SortFunc(sortedReferences, func(a, b valType) bool {
return a > b
})
compilerDebugln("candidates for byte slice removal", sortedReferences)
for _, r := range sortedReferences {
if !p.referencesBytes(r) {
compilerDebugln("will remove byte slice reference", r)
p.removeInternedBytes(r)
}
}
}
// Remove {{ template }} invocations which call into an
// empty template.
func (p *program) removeEmptyTemplateInvocations() {
for name, code := range p.code {
for ii := 0; ii < len(code); ii++ {
v := code[ii]
if v.op == opTEMPLATE {
_, t := decodeVal(v.val)
tmplName := p.strings[t]
tmplCode, ok := p.code[tmplName]
if !ok {
panic(fmt.Errorf("missing template %q", tmplName))
}
if len(tmplCode) == 0 {
// Empty template invocation, remove it
compilerDebugf("remove empty template invocation %q from %q\n", tmplName, name)
code = instructions(code).replaceTemplateInvocation(ii, nil)
}
}
}
p.code[name] = code
}
}
func (p *program) stitchTree(name string) {
// TODO: Save the name of the original template somewhere
// so we can recover it for error messages. Until we fix
// that problem we're only stitching trees which are just
// a WB. In most cases, this will inline the top and bottom
// hooks, giving already a nice performance boost.
code := p.code[name]
for ii := 0; ii < len(code); ii++ {
v := code[ii]
if v.op == opTEMPLATE {
_, t := decodeVal(v.val)
tmpl := p.strings[t]
repl := p.code[tmpl]
if len(repl) == 1 && repl[0].op == opWB {
// replace the tree
code = instructions(code).replaceTemplateInvocation(ii, repl)
compilerDebugf("replaced template %q invocation from %q with WB at PC %d\n", tmpl, name, ii)
ii--
}
}
}
p.code[name] = code
}
func (p *program) stitch() {
p.stitchTree(p.tmpl.root)
}