-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathmain.cpp
More file actions
509 lines (482 loc) · 19.4 KB
/
Copy pathmain.cpp
File metadata and controls
509 lines (482 loc) · 19.4 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include <benchmark/benchmark.h>
#include <algorithm>
#include <chrono>
#include "test_common.h"
extern "C" {
#include "PikaMain.h"
#include "PikaParser.h"
#include "PikaStdLib_MemChecker.h"
#include "PikaVM.h"
#include "dataArgs.h"
#include "dataMemory.h"
#include "dataStrs.h"
#include "pikaScript.h"
}
static void for_loop_10000(benchmark::State& state) {
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
pikaVM_run(pikaMain, (char *)
"a = 0\n"
"for i in range(0, 10000):\n"
" a = a + 1\n"
"\n");
obj_deinit(pikaMain);
}
}
BENCHMARK(for_loop_10000)->Unit(benchmark::kMillisecond);
static void while_loop_10000(benchmark::State& state) {
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
pikaVM_run(pikaMain, (char *)
"i = 0\n"
"while i < 10000:\n"
" i = i + 1\n"
"\n");
obj_deinit(pikaMain);
}
}
BENCHMARK(while_loop_10000)->Unit(benchmark::kMillisecond);
static void function_call_1000(benchmark::State& state) {
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(
buffs, (char*)
"def collect(a, b=2, *args, **kwargs):\n"
" return a + b\n"
"i = 0\n"
"while i < 1000:\n"
" collect(i, 3, 4, 5, alpha=6, beta=7)\n"
" i = i + 1\n"
"\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
obj_deinit(pikaMain);
}
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
}
BENCHMARK(function_call_1000)->Unit(benchmark::kMillisecond);
static void function_call_default_1000(benchmark::State& state) {
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(
buffs, (char*)
"def add(a, b=2):\n"
" return a + b\n"
"i = 0\n"
"while i < 1000:\n"
" add(i)\n"
" i = i + 1\n"
"\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
obj_deinit(pikaMain);
}
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
}
BENCHMARK(function_call_default_1000)->Unit(benchmark::kMillisecond);
static void function_call_kwargs_1000(benchmark::State& state) {
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(
buffs, (char*)
"def collect(a, **kwargs):\n"
" return a\n"
"i = 0\n"
"while i < 1000:\n"
" collect(a=i, alpha=1, beta=2)\n"
" i = i + 1\n"
"\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
obj_deinit(pikaMain);
}
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
}
BENCHMARK(function_call_kwargs_1000)->Unit(benchmark::kMillisecond);
static void function_call_kwargs_large_constpool_1000(
benchmark::State& state) {
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(
buffs, (char*)
"def collect(a, **kwargs):\n"
" return a\n"
"c0 = 'constant_00'\n"
"c1 = 'constant_01'\n"
"c2 = 'constant_02'\n"
"c3 = 'constant_03'\n"
"c4 = 'constant_04'\n"
"c5 = 'constant_05'\n"
"c6 = 'constant_06'\n"
"c7 = 'constant_07'\n"
"c8 = 'constant_08'\n"
"c9 = 'constant_09'\n"
"c10 = 'constant_10'\n"
"c11 = 'constant_11'\n"
"c12 = 'constant_12'\n"
"c13 = 'constant_13'\n"
"c14 = 'constant_14'\n"
"c15 = 'constant_15'\n"
"c16 = 'constant_16'\n"
"c17 = 'constant_17'\n"
"c18 = 'constant_18'\n"
"c19 = 'constant_19'\n"
"i = 0\n"
"while i < 1000:\n"
" collect(a=i, alpha=1, beta=2)\n"
" i = i + 1\n"
"\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
obj_deinit(pikaMain);
}
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
}
BENCHMARK(function_call_kwargs_large_constpool_1000)
->Unit(benchmark::kMillisecond);
static void function_call_starred_1000(benchmark::State& state) {
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(
buffs, (char*)
"def collect(a, b=2, *args, **kwargs):\n"
" return a + b\n"
"values = (3, 4, 5)\n"
"i = 0\n"
"while i < 1000:\n"
" collect(i, *values, alpha=6, beta=7)\n"
" i = i + 1\n"
"\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
obj_deinit(pikaMain);
}
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
}
BENCHMARK(function_call_starred_1000)->Unit(benchmark::kMillisecond);
static const char* fibonacci_source =
"def fib(n):\n"
" if n < 2:\n"
" return n\n"
" return fib(n - 1) + fib(n - 2)\n"
"result = fib(20)\n"
"\n";
static void fibonacci_recursive_20(benchmark::State& state) {
using clock = std::chrono::steady_clock;
auto nanoseconds = [](clock::duration duration) {
return std::chrono::duration_cast<std::chrono::nanoseconds>(duration)
.count();
};
auto compile_start = clock::now();
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(buffs, (char*)fibonacci_source);
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
auto compile_end = clock::now();
pikaMemAllocationCountReset();
auto root_create_start = clock::now();
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
auto root_create_end = clock::now();
uint64_t root_create_allocations = pikaMemAllocationCount();
uint32_t warm_heap_baseline_bytes = pikaMemNow();
uint64_t execute_ns = 0;
uint64_t execute_allocations = 0;
pikaMemMaxReset();
pikaMemAllocationCountReset();
for (auto _ : state) {
uint32_t before_execute_allocations = pikaMemAllocationCount();
auto execute_start = clock::now();
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
auto execute_end = clock::now();
execute_allocations +=
pikaMemAllocationCount() - before_execute_allocations;
if (6765 != obj_getInt(pikaMain, (char*)"result")) {
state.SkipWithError("recursive fibonacci result mismatch");
}
execute_ns += nanoseconds(execute_end - execute_start);
}
uint32_t heap_peak_bytes = pikaMemMax();
uint32_t before_destroy_allocations = pikaMemAllocationCount();
auto root_destroy_start = clock::now();
obj_deinit(pikaMain);
auto root_destroy_end = clock::now();
uint64_t root_destroy_allocations =
pikaMemAllocationCount() - before_destroy_allocations;
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
state.counters["compile_once_ns"] =
nanoseconds(compile_end - compile_start);
state.counters["setup_root_create_ns"] =
nanoseconds(root_create_end - root_create_start);
state.counters["warm_execute_ns"] = benchmark::Counter(
execute_ns, benchmark::Counter::kAvgIterations);
state.counters["teardown_root_destroy_ns"] =
nanoseconds(root_destroy_end - root_destroy_start);
state.counters["setup_root_create_allocations"] =
root_create_allocations;
state.counters["execute_allocations"] = benchmark::Counter(
execute_allocations, benchmark::Counter::kAvgIterations);
state.counters["teardown_root_destroy_allocations"] =
root_destroy_allocations;
state.counters["heap_allocations"] = benchmark::Counter(
execute_allocations, benchmark::Counter::kAvgIterations);
state.counters["heap_peak_bytes"] = heap_peak_bytes;
state.counters["warm_heap_baseline_bytes"] =
warm_heap_baseline_bytes;
state.counters["heap_residual_bytes"] = pikaMemNow();
state.counters["root_destroy_count"] = 1;
}
BENCHMARK(fibonacci_recursive_20)->Unit(benchmark::kMillisecond);
static void fibonacci_recursive_20_cold_start(benchmark::State& state) {
using clock = std::chrono::steady_clock;
auto nanoseconds = [](clock::duration duration) {
return std::chrono::duration_cast<std::chrono::nanoseconds>(duration)
.count();
};
uint64_t root_create_ns = 0;
uint64_t compile_ns = 0;
uint64_t first_execute_ns = 0;
uint64_t root_destroy_ns = 0;
uint64_t cleanup_ns = 0;
uint64_t root_create_allocations = 0;
uint64_t compile_allocations = 0;
uint64_t execute_allocations = 0;
uint64_t destroy_allocations = 0;
uint64_t cleanup_allocations = 0;
uint32_t heap_peak_bytes = 0;
uint32_t heap_residual_bytes = 0;
for (auto _ : state) {
pikaMemMaxReset();
pikaMemAllocationCountReset();
auto root_create_start = clock::now();
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
auto root_create_end = clock::now();
uint32_t after_root_allocations = pikaMemAllocationCount();
auto compile_start = clock::now();
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(buffs, (char*)fibonacci_source);
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
auto compile_end = clock::now();
uint32_t after_compile_allocations = pikaMemAllocationCount();
auto execute_start = clock::now();
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
auto execute_end = clock::now();
uint32_t after_execute_allocations = pikaMemAllocationCount();
if (6765 != obj_getInt(pikaMain, (char*)"result")) {
state.SkipWithError("cold fibonacci result mismatch");
}
auto root_destroy_start = clock::now();
obj_deinit(pikaMain);
auto root_destroy_end = clock::now();
uint32_t after_destroy_allocations = pikaMemAllocationCount();
auto cleanup_start = clock::now();
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
auto cleanup_end = clock::now();
uint32_t after_cleanup_allocations = pikaMemAllocationCount();
root_create_ns +=
nanoseconds(root_create_end - root_create_start);
compile_ns += nanoseconds(compile_end - compile_start);
first_execute_ns += nanoseconds(execute_end - execute_start);
root_destroy_ns +=
nanoseconds(root_destroy_end - root_destroy_start);
cleanup_ns += nanoseconds(cleanup_end - cleanup_start);
root_create_allocations += after_root_allocations;
compile_allocations +=
after_compile_allocations - after_root_allocations;
execute_allocations +=
after_execute_allocations - after_compile_allocations;
destroy_allocations +=
after_destroy_allocations - after_execute_allocations;
cleanup_allocations +=
after_cleanup_allocations - after_destroy_allocations;
heap_peak_bytes = std::max(heap_peak_bytes, pikaMemMax());
heap_residual_bytes = std::max(heap_residual_bytes, pikaMemNow());
}
state.counters["root_create_ns"] = benchmark::Counter(
root_create_ns, benchmark::Counter::kAvgIterations);
state.counters["compile_ns"] = benchmark::Counter(
compile_ns, benchmark::Counter::kAvgIterations);
state.counters["first_execute_ns"] = benchmark::Counter(
first_execute_ns, benchmark::Counter::kAvgIterations);
state.counters["root_destroy_ns"] = benchmark::Counter(
root_destroy_ns, benchmark::Counter::kAvgIterations);
state.counters["cleanup_ns"] = benchmark::Counter(
cleanup_ns, benchmark::Counter::kAvgIterations);
state.counters["root_create_allocations"] = benchmark::Counter(
root_create_allocations, benchmark::Counter::kAvgIterations);
state.counters["compile_allocations"] = benchmark::Counter(
compile_allocations, benchmark::Counter::kAvgIterations);
state.counters["execute_allocations"] = benchmark::Counter(
execute_allocations, benchmark::Counter::kAvgIterations);
state.counters["root_destroy_allocations"] = benchmark::Counter(
destroy_allocations, benchmark::Counter::kAvgIterations);
state.counters["cleanup_allocations"] = benchmark::Counter(
cleanup_allocations, benchmark::Counter::kAvgIterations);
state.counters["heap_allocations"] = benchmark::Counter(
root_create_allocations + compile_allocations +
execute_allocations + destroy_allocations +
cleanup_allocations,
benchmark::Counter::kAvgIterations);
state.counters["heap_peak_bytes"] = heap_peak_bytes;
state.counters["heap_residual_bytes"] = heap_residual_bytes;
}
BENCHMARK(fibonacci_recursive_20_cold_start)
->Unit(benchmark::kMillisecond);
static void embedded_control_loop_1000(benchmark::State& state) {
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(
buffs, (char*)
"class Controller:\n"
" def __init__(self):\n"
" self.integral = 0\n"
" self.previous = 0\n"
" self.output = 0\n"
" def update(self, target, measurement):\n"
" error = target - measurement\n"
" self.integral = self.integral + error\n"
" derivative = error - self.previous\n"
" output = error * 3 + self.integral + derivative\n"
" if output > 500:\n"
" output = 500\n"
" if output < -500:\n"
" output = -500\n"
" self.previous = error\n"
" self.output = output\n"
" return output\n"
"controller = Controller()\n"
"measurement = 0\n"
"checksum = 0\n"
"i = 0\n"
"while i < 1000:\n"
" target = 120\n"
" if i % 200 >= 100:\n"
" target = -80\n"
" output = controller.update(target, measurement)\n"
" if output > measurement:\n"
" measurement = measurement + 2\n"
" else:\n"
" measurement = measurement - 2\n"
" checksum = checksum + output\n"
" i = i + 1\n"
"\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
if (12 != obj_getInt(pikaMain, (char*)"measurement") ||
4790 != obj_getInt(pikaMain, (char*)"checksum")) {
state.SkipWithError("embedded control loop result mismatch");
}
obj_deinit(pikaMain);
}
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
}
BENCHMARK(embedded_control_loop_1000)->Unit(benchmark::kMillisecond);
void __platform_printf(char* fmt, ...) {}
static void for_print_1000(benchmark::State& state) {
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(buffs, (char*)
"for i in range(1000):\n"
" print(i)\n"
"\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
obj_deinit(pikaMain);
}
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
}
BENCHMARK(for_print_1000)->Unit(benchmark::kMillisecond);
static void prime_number_100(benchmark::State& state) {
int num = 0;
Args* buffs = New_strBuff();
char* pikaAsm = pika_lines2Asm(buffs, (char*)
"num = 0\n"
"i = 2\n"
"while i < 100:\n"
" is_prime = 1\n"
" j = 2\n"
" while j < i:\n"
" if i%j==0 :\n"
" is_prime = 0\n"
" break\n"
" j += 1 \n"
" if is_prime:\n"
" num = num + i\n"
" i += 1\n"
"\n");
ByteCodeFrame bytecode_frame;
byteCodeFrame_init(&bytecode_frame);
byteCodeFrame_appendFromAsm(&bytecode_frame, pikaAsm);
for (auto _ : state) {
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
pikaVM_runByteCodeFrame(pikaMain, &bytecode_frame);
num = obj_getInt(pikaMain, (char*)"num");
if (1060 != num) {
printf("Error: prime_number_100\r\n");
}
obj_deinit(pikaMain);
}
byteCodeFrame_deinit(&bytecode_frame);
args_deinit(buffs);
}
BENCHMARK(prime_number_100)->Unit(benchmark::kMillisecond);
static void prime_number_100_c(benchmark::State& state) {
int num = 0;
for (auto _ : state) {
num = 0;
/* run */
for (int i = 2; i < 100; i++) {
int is_prime = 1;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
is_prime = 0;
break;
}
}
if (is_prime) {
num = num + i;
}
}
if (1060 != num) {
printf("Error: prime_number_100\r\n");
}
}
}
BENCHMARK(prime_number_100_c)->Unit(benchmark::kMillisecond);
BENCHMARK_MAIN();