-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_complexity_test.go
More file actions
263 lines (251 loc) · 7.56 KB
/
Copy pathcompiler_complexity_test.go
File metadata and controls
263 lines (251 loc) · 7.56 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package ember
import (
"reflect"
"strconv"
"strings"
"testing"
)
var compilerComplexityProtoSink *Proto
func TestCompilerComplexityBudgets(t *testing.T) {
tests := []struct {
name string
source string
globals map[string]Value
want []Value
maxInstructions int
maxConstants int
maxRegisterSlots int
wantChildProtos int
maxWordcodeWords int64
}{
{
name: "branch_dense",
source: `local x = 1
if flag then
x = x + 2
else
x = x + 3
end
return x`,
globals: map[string]Value{"flag": BoolValue(false)},
want: []Value{NumberValue(4)},
maxInstructions: 7,
maxConstants: 4,
maxRegisterSlots: 2,
wantChildProtos: 0,
maxWordcodeWords: 8,
},
{
name: "closure_upvalue",
source: `local base = 4
local function add(x)
return base + x
end
return add(3)`,
want: []Value{NumberValue(7)},
maxInstructions: 9,
maxConstants: 2,
maxRegisterSlots: 7,
wantChildProtos: 1,
maxWordcodeWords: 10,
},
{
name: "vararg_multi_return",
source: `local function collect(...)
local a, b = ...
return a, b, select("#", ...)
end
return collect(1, 2, 3)`,
want: []Value{NumberValue(1), NumberValue(2), NumberValue(3)},
maxInstructions: 11,
maxConstants: 3,
maxRegisterSlots: 10,
wantChildProtos: 1,
maxWordcodeWords: 13,
},
{
name: "table_string_fields",
source: `local value = {name = "ember", hp = 10}
value.hp = value.hp + 5
return value.name, value.hp`,
want: []Value{StringValue("ember"), NumberValue(15)},
maxInstructions: 12,
maxConstants: 6,
maxRegisterSlots: 5,
wantChildProtos: 0,
maxWordcodeWords: 18,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
proto, err := Compile(tt.source)
if err != nil {
t.Fatalf("Compile returned error: %v", err)
}
results, err := RunWithGlobals(proto, tt.globals)
if err != nil {
t.Fatalf("RunWithGlobals returned error: %v", err)
}
assertCompilerComplexityResults(t, results, tt.want)
metrics := CompilerBenchmarkMetricsForTest(proto)
if metrics.Instructions > tt.maxInstructions {
t.Fatalf("%s has %d instructions, want at most %d", tt.name, metrics.Instructions, tt.maxInstructions)
}
if metrics.Constants > tt.maxConstants {
t.Fatalf("%s has %d constants, want at most %d", tt.name, metrics.Constants, tt.maxConstants)
}
if metrics.RegisterSlots > tt.maxRegisterSlots {
t.Fatalf("%s has %d register slots, want at most %d", tt.name, metrics.RegisterSlots, tt.maxRegisterSlots)
}
if metrics.ChildProtos != tt.wantChildProtos {
t.Fatalf("%s has %d child protos, want %d", tt.name, metrics.ChildProtos, tt.wantChildProtos)
}
wordcodeBytes := int64(reflect.TypeOf(wordcodeWord(0)).Size())
if got := metrics.WordcodeBytes / wordcodeBytes; got > tt.maxWordcodeWords {
t.Fatalf("%s has %d wordcode words, want at most %d", tt.name, got, tt.maxWordcodeWords)
}
// The replaced executable representation used one 16-byte packed
// instruction per logical instruction. AUX expansion must still leave
// the published word stream at least 60% smaller than that baseline.
legacyBytes := int64(metrics.Instructions) * 16
if legacyBytes > 0 && metrics.WordcodeBytes*100 > legacyBytes*40 {
t.Fatalf("%s wordcode uses %d bytes versus %d legacy bytes, want at least 60%% reduction", tt.name, metrics.WordcodeBytes, legacyBytes)
}
})
}
}
func TestCompileNestedClosuresAllocationBudget(t *testing.T) {
source := nestedClosureCompileSource(12)
proto, err := Compile(source)
if err != nil {
t.Fatalf("Compile returned error: %v", err)
}
results, err := Run(proto)
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
if len(results) != 1 {
t.Fatalf("Run returned %d results, want 1", len(results))
}
if got, ok := results[0].Number(); !ok || got != 2 {
t.Fatalf("Run result is %v (%t), want number 2", got, ok)
}
const maxAllocsPerCompile = 3800
allocs := testing.AllocsPerRun(25, func() {
compiled, err := Compile(source)
if err != nil {
t.Fatalf("Compile returned error: %v", err)
}
compilerComplexityProtoSink = compiled
})
if allocs > maxAllocsPerCompile {
t.Fatalf("nested closure Compile used %.0f allocs/op, want at most %d", allocs, maxAllocsPerCompile)
}
}
func TestCompileNestedClosurePreservesChildLineMetadata(t *testing.T) {
proto, err := Compile(`local function read(row)
return row.hp
end
return read({hp = 7})`)
if err != nil {
t.Fatalf("Compile returned error: %v", err)
}
if len(proto.prototypes) != 1 {
t.Fatalf("compiled root has %d child prototypes, want 1", len(proto.prototypes))
}
child := proto.prototypes[0]
childCode, err := protoDecodedInstructions(child)
if err != nil {
t.Fatalf("decode child wordcode: %v", err)
}
if len(child.lines) != len(childCode) {
t.Fatalf("child line table has %d entries for %d instructions", len(child.lines), len(childCode))
}
results, err := Run(proto)
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
if len(results) != 1 {
t.Fatalf("Run returned %d results, want 1", len(results))
}
if got, ok := results[0].Number(); !ok || got != 7 {
t.Fatalf("Run result is %v (%t), want number 7", got, ok)
}
}
func TestCompileNestedClosuresPreservesParentUpvalues(t *testing.T) {
proto, err := Compile(`local base = 4
local function outer(x)
local function middle(y)
local function inner(z)
return base + x + y + z
end
return inner(3)
end
return middle(2)
end
return outer(1)`)
if err != nil {
t.Fatalf("Compile returned error: %v", err)
}
results, err := Run(proto)
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
if len(results) != 1 {
t.Fatalf("Run returned %d results, want 1", len(results))
}
if got, ok := results[0].Number(); !ok || got != 10 {
t.Fatalf("Run result is %v (%t), want number 10", got, ok)
}
}
func nestedClosureCompileSource(depth int) string {
var source strings.Builder
for index := range depth {
source.WriteString(strings.Repeat(" ", index))
source.WriteString("local function f")
source.WriteString(strconv.Itoa(index))
source.WriteString("(x)\n")
}
source.WriteString(strings.Repeat(" ", depth))
source.WriteString("return x + 1\n")
for index := depth - 1; index >= 0; index-- {
source.WriteString(strings.Repeat(" ", index))
source.WriteString("end\n")
source.WriteString(strings.Repeat(" ", index))
source.WriteString("return f")
source.WriteString(strconv.Itoa(index))
if index == 0 {
source.WriteString("(1)\n")
} else {
source.WriteString("(x)\n")
}
}
return source.String()
}
func assertCompilerComplexityResults(t *testing.T, got []Value, want []Value) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("Run results have length %d, want %d: %#v", len(got), len(want), got)
}
for index := range want {
if got[index].Kind() != want[index].Kind() {
t.Fatalf("Run result %d has kind %s, want %s", index, got[index].Kind(), want[index].Kind())
}
switch want[index].Kind() {
case NumberKind:
gotNumber, _ := got[index].Number()
wantNumber, _ := want[index].Number()
if gotNumber != wantNumber {
t.Fatalf("Run result %d is number %v, want %v", index, gotNumber, wantNumber)
}
case StringKind:
gotString, _ := got[index].String()
wantString, _ := want[index].String()
if gotString != wantString {
t.Fatalf("Run result %d is string %q, want %q", index, gotString, wantString)
}
default:
t.Fatalf("Run result %d uses unsupported expected kind %s", index, want[index].Kind())
}
}
}