-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneratehtml.lua
More file actions
1787 lines (1631 loc) · 55.5 KB
/
generatehtml.lua
File metadata and controls
1787 lines (1631 loc) · 55.5 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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local head = [[
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto|Ubuntu+Mono&display=swap" rel="stylesheet">
<title>LuaJIT Benchmark Tests</title>
<meta name="description" content="LuaJIT Benchmark tests page. Made for understanding the results and for optimization solutions.">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.5">
<meta name="keywords" content="Lua, LuaJIT, benchmark, benchmark tests, performance, LuaJIT vs Lua, LuaJIT performance, Lua performance">
<link rel="stylesheet" href="style.css">
<script>
function OpenTab(evt, tabname) {
var i, tabcontent, tablinks;
var n = evt.currentTarget.getAttribute("data-testid")
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
if (tabcontent[i].getAttribute("data-testid") === n) {
tabcontent[i].style.display = "none";
}
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
if (tablinks[i].getAttribute("data-testid") === n) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
}
document.getElementById(tabname).style.display = "block";
evt.currentTarget.className += " active";
}
function ToogleDiff(event,name) {
if (document.getElementById(name).style.maxHeight === "fit-content") {
document.getElementById(name).style.maxHeight = "0px";
} else {
document.getElementById(name).style.maxHeight = "fit-content";
}
}
</script>
</head>
<body>
<a name="top"></a>
<div id="g">
<div style="text-align: center; font-size: 34; padding-top: 50">LuaJIT Benchmarks</div>
</div>
]]
local file = io.open("index.html", "w")
local tags = {}
local function Add(v)
file:write(v)
end
local function N()
file:write("\n")
end
local function End()
file:write("</")
file:write(table.remove(tags))
file:write(">")
end
local function OpenTag(t)
tags[#tags + 1] = t
end
local function Link(link, text)
Add([[<a href="]])
Add(link)
Add([[">]])
if text then
Add(text)
end
Add("</a>")
end
local function AddHead()
Add(head)
end
local function EndHTML()
Add([[</body></html>]])
print("Unclosed tags: " .. #tags)
end
--style="padding-top: 3em;"
local function AddHeader(title)
Add([[<div id="header1">]])
Add(title)
Add([[</div>]])
N()
end
local function AddBottom()
Add([[
<script>
var buttons = document.getElementsByClassName("tablinks");
for (i = 0; i < buttons.length; i++) {
if (buttons[i].id === "defaultOpen") {
buttons[i].click()
}
}
</script>]])
Add([[<div id="bottom">]])
OpenTag("div")
Link("#top", "Up")
N()
Add([[<img src="http://hits.dwyl.io/GitSparTV/LuaJIT-Benchmarks/index.html.svg">]]) N()
Add([[Made by Spar (Spar#6665)]])
N()
Link("https://github.com/GitSparTV/LuaJIT-Benchmarks/", "New benchmark tests are welcome")
Add([[
Public Domain
2020]])
End()
end
local function Text()
Add([[<div id="text">]])
OpenTag("div")
end
local function Code(code)
Add([[<div id="code">]])
Add(code)
Add([[</div>]])
end
local function InlineCode(code)
Add([[<a class="inlcode">]])
Add(code)
Add([[</a>]])
end
local function TableStart()
Add([[<div id="tbldiv"><table>]])
end
local function TableEnd()
Add([[</table></div>]])
end
local function TableLine(style)
Add("<tr")
if style == 0 then
Add([[ style="background-color: #e56060;"]])
elseif style == 1 then
Add([[ style="background-color: #dff9df;"]])
end
Add(">")
OpenTag("tr")
end
local function TableHeader(c)
Add([[<th>]])
Add(c)
Add([[</th>]])
end
local function TableColumn(c)
Add([[<td>]])
Add(c)
Add([[</td>]])
end
local AnchorHeaders = {}
local function ConstructContent(t)
AnchorHeaders = t
Text()
for i = 1, #t do
Link("#test" .. i, i .. ". " .. t[i])
N()
end
End()
end
local function REDO() Add([[<div style="padding-bottom: 10px; white-space: pre; overflow: auto; padding-top: 5px;"><a id="yellowinline" class="inlcode">May be incorrect. Awaits recalculation.</a></div>]]) end
local CurrentTest = 1
local NCodes = 1
local function StartTest(n)
CurrentTest = n
NCodes = 1
AddHeader([[<div id="test]] .. n .. [[">]] .. n .. ". " .. AnchorHeaders[n] .. [[<a class="headinganchor" href="#test]] .. n .. [[">🔗︎</a></div>]])
end
local function Predefines()
Add([[<div id="subh">Predefines:</div>]])
end
local function TestCode(code, append)
Add([[<div id="subh">Code ]] .. NCodes .. (append and " (" .. append .. ")" or ""))
Add([[:</div>]])
NCodes = NCodes + 1
Code(code)
end
local function K(n)
if n >= 1000000 then
return math.floor(n / 1000000) .. "M"
elseif n >= 1000 then
return math.floor(n / 1000) .. "k"
end
return n
end
local function PropertySheet(samples, notplain)
Add([[<div id="subh">Results (]] .. K(samples) .. [[ iterations):</div>]])
Add(string.format([[
<div class="tab" id="property_sheet%d">
<button data-testid="%d" class="tablinks" onclick="OpenTab(event, 'jiton_test%d')" id="defaultOpen">LuaJIT</button>
<button data-testid="%d" class="tablinks" onclick="OpenTab(event, 'jitoff_test%d')">LuaJIT Interpreter</button>]] .. (notplain and "" or [[<button data-testid="%d" class="tablinks" onclick="OpenTab(event, 'plain_test%d')">Lua 5.1</button>]]) .. [[</div>]], CurrentTest, CurrentTest, CurrentTest, CurrentTest, CurrentTest, CurrentTest, CurrentTest, CurrentTest))
end
local function LuaJITOn()
Add([[<div data-testid="]].. CurrentTest .. [[" id="jiton_test]] .. CurrentTest .. [[" class="tabcontent">]])
OpenTag("div")
end
local function LuaJITOff()
Add([[<div data-testid="]].. CurrentTest .. [[" id="jitoff_test]] .. CurrentTest .. [[" class="tabcontent">]])
OpenTag("div")
end
local function PlainLua()
Add([[<div data-testid="]].. CurrentTest .. [[" id="plain_test]] .. CurrentTest .. [[" class="tabcontent">]])
OpenTag("div")
end
local function Diff(code)
local output = {}
for line in string.gmatch(code, "[^\n]+") do
if line:sub(1, 1) == "-" then
output[#output + 1] = [[<a class="delete">]] .. line .. [[</a>]]
elseif line:sub(1, 1) == "+" then
output[#output + 1] = [[<a class="insert">]] .. line .. [[</a>]]
else
output[#output + 1] = " " .. line
end
end
return table.concat(output, "\n")
end
local function Asm(lines, asm)
Add([[<div id="subh">Assembler Results:</div><ol>]])
for line in string.gmatch(lines, "[^\n]+") do
Add([[<li>]])
local addasm, red
if line:sub(1, 1) == "!" then
Add([[<a id="highlight">]])
line = line:sub(2)
red = true
end
local append = " "
line = line:gsub(" NYI2%.(%d)", function(n)
append = append .. [[<a id="redinline" class="inlcode">NYI on LuaJIT 2.]] .. n .. [[</a>]]
return ""
end)
line = line:gsub(" RET2%.(%d)", function(n)
append = append .. [[<a id="redinline" class="inlcode">Fallbacks to interpreter on LuaJIT 2.]] .. n .. [[</a>]]
return ""
end)
line = line:gsub(" STITCH", function(n)
append = append .. [[<a id="yellowinline" class="inlcode">Stitches on LuaJIT 2.1</a>]]
return ""
end)
-- line = line:gsub("DEF",function(n)
-- append = append .. [[<a id="yellowinline" class="inlcode">Requires changes in the optimization settings</a>]]
-- return ""
-- end)
if line:sub(-1, -1) == "?" then
line = line:sub(1, -3)
addasm = true
end
Add(line)
if line:find(": %d+") then
Add(" instructions total.")
end
if red then
Add([[</a>]])
end
if #append ~= 1 then
Add(append)
end
if addasm then
Add([[<div class="wrap-collabsible">
<input id="collapsible]] .. CurrentTest .. [[" class="toggle" type="checkbox" onclick="ToogleDiff(event,'diff]] .. CurrentTest .. [[')">
<label for="collapsible]] .. CurrentTest .. [[" class="lbl-toggle">Diff</label>
</div><div class="collapsible-content" id="diff]].. CurrentTest .. [[">
<div id="bytecode">]] .. Diff(asm) .. [[
</div>
</div>]])
end
Add([[</li>]])
end
Add([[</ol>]])
end
local function Benchmark(bench)
Add([[<div id="subh">Benchmark Results:</div>]])
TableStart()
TableLine()
TableHeader("#")
TableHeader("Name")
TableHeader("Median")
TableHeader("Minimum")
TableHeader("Maximum")
TableHeader("Average")
TableHeader("Percentage")
End()
local i = 1
for line in string.gmatch(bench, "[^\n]+") do
if line:sub(1, 1) == "!" then
TableLine(0)
line = line:sub(2)
elseif line:sub(1, 1) == "*" then
TableLine(1)
line = line:sub(2)
else
TableLine()
end
TableColumn(i)
i = i + 1
local Name, Med, Min, Max, Aver, Prefix = string.match(line, "^(.-): ([%d%.]+) %(Min: ([%d%.]+), Max: ([%d%.]+), Average: ([%d%.]+)%) second%(s%)(.+)")
-- print(Name, Med, Min, Max, Aver, Prefix) -- .. (#Times ~= 0 and " " .. Times:match("%((.-)%)") or "")
TableColumn(Name)
TableColumn(Med .. " sec(s)")
TableColumn(Min)
TableColumn(Max)
TableColumn(Aver)
local Times = string.match(Prefix, "%(%d+ times .-%)")
TableColumn(string.match(Prefix, " (%(.-%))") .. (Times and " " .. Times or ""))
End()
end
TableEnd()
end
local function Conclusion()
Add([[<div id="subh">Conclusion:</div><div style="padding: 10px 0px 10px 10px; white-space: pre; overflow: auto;">]])
OpenTag("div")
end
----------------------------------------------------------------
AddHead()
AddHeader("About")
Text()
Add([[These benchmark tests demonstrate the performance of LuaJIT compiler, LuaJIT interpreter and Lua 5.1.
LuaJIT stated that globals and locals now has the same performance unlike in plain Lua.
LuaJIT stated that it's faster than Lua. Even Lua suggests to use LuaJIT for more performance.
LuaJIT uses its own interpreter and compiler and many other optimizations to improve the performance. But is it really fast?]])
End()
AddHeader("About tests")
Text()
Add([[This site contains results and conclusions for LuaJIT compiler, LuaJIT interpreter and Lua 5.1.4.
LuaJIT interpreter is accounted because it's a useful information for functions in which you 100% sure they won't compile.
Or maybe you're using embedded LuaJIT 2.0 which aborts on any C function (And FFI is disabled).
Lua 5.1 is accounted for you decision in what to choose, or just out of curiosity.
First 14 benchmark tests were taken from ]]) Link("https://springrts.com/wiki/Lua_Performance","this page") N()
Link("https://github.com/GitSparTV/LuaJIT-Benchmarks/issues/new/","New benchmark tests are welcome.") N() N()
Add([[Specs: Intel i5-6500 3.20 GHz. 64-bit. LuaJIT 2.1.0-beta3. (Lua 5.1.4 for plain Lua tests) (LuaJIT 2.0.4 for LuaJIT 2.0 assembler tests)
(JIT: ON SSE2 SSE3 SSE4.1 BMI2 fold cse dce fwd dse narrow loop abc sink fuse)]])
End()
AddHeader("Benchmark Code")
Text()
Link("https://github.com/GitSparTV/LuaJIT-Benchmarks/blob/master/bench.lua","Source code") N()
Add([[For benchmark tests we use the median of 100 takes of the given amount of iterations of the code.]])
Code([[
for take = 1, 100 do
local START = os.clock()
for times = 1, iterations do
...
end
local END = os.clock()
end]])
Add([[For assembler tests we use ]]) InlineCode("luajit -jdump=+Arsa asmbench.lua") Add(".\n")
Add([[The total amount of instructions is based on maximum possible amount (Last jump or RET).
Bytecode size is used from -jdump, not -bl, so it also counts sub-functions instructions and headers.]]) N()
Link("https://github.com/GitSparTV/LuaJIT-Benchmarks/blob/master/asmbench.lua","Script for bytecode test") Add(".")
End()
AddHeader("Useful links")
Text()
Link("http://wiki.luajit.org/NYI","Things which are likely to cause NYI aborts from the JIT compiler") N()
Link("http://wiki.luajit.org/Numerical-Computing-Performance-Guide","Tips for writing performant Lua code") N()
Link("http://wiki.luajit.org/Bytecode-2.0","LuaJIT 2.0 Bytecode reference") N()
Link("https://luajit.org/","LuaJIT official site")
End()
AddHeader("Contents")
ConstructContent({
"Local vs Global",
"Local vs Global table indexing",
"Localized method (3 calls)",
"Unpack",
"Find and return maximum value",
"\"not a\" vs \"a or b\"",
"\"x ^ 2\" vs \"x * x\" vs \"math.pow\"",
"\"math.fmod\" vs \"%\" operator",
"Predefined function or anonymous function in the argument",
"for loops",
"Localizing table value for multiple usage",
"Array insertion",
"Table with and without pre-allocated size",
"Table initialization before or each time on insertion",
"String split (by character)",
"Empty string check",
"C array size (FFI)",
"String concatenation",
"String in a function",
"Taking a value from a function with multiple returns",
})
StartTest(1) -- Local vs Global
Text()
Predefines()
Code([[local t = type]])
TestCode([[type(3)]])
TestCode([[t(3)]])
PropertySheet(10000000)
LuaJITOn()
Asm([[
!Global: 29
Local: 18 ?
]],[[
mov dword [0x3e2d0410], 0x1
movsd xmm7, [rdx+0x40]
cvttsd2si eax, xmm7
xorps xmm6, xmm6
cvtsi2sd xmm6, eax
ucomisd xmm7, xmm6
jnz 0x7ffa543c0010 ->0
jpe 0x7ffa543c0010 ->0
cmp eax, 0x7ffffffe
jg 0x7ffa543c0010 ->0
cvttsd2si edi, [rdx+0x38]
cmp dword [rdx+0x14], -0x09
jnz 0x7ffa543c0010 ->0
cmp dword [rdx+0x10], 0x3e2d8228
- jnz 0x7ffa543c0010 ->0
- mov edx, [0x3e2d8230]
- cmp dword [rdx+0x1c], +0x3f
- jnz 0x7ffa543c0010 ->0
- mov ecx, [rdx+0x14]
- mov rsi, 0xfffffffb3e2d2f88
- cmp rsi, [rcx+0x5a8]
- jnz 0x7ffa543c0010 ->0
- cmp dword [rcx+0x5a4], -0x09
- jnz 0x7ffa543c0010 ->0
- cmp dword [rcx+0x5a0], 0x3e2d2ef0
jnz 0x7ffa543c0010 ->0
add edi, +0x01
cmp edi, eax
jg 0x7ffa543c0014 ->1]])
Conclusion()
Add([[Each global lookup can cost around 11 instructions. They both run almost on the same speed, but this benchmark tests only one global.
This is still a good practice to localize all variables you need.]])
End()
End()
LuaJITOff()
Benchmark([[
Global: 0.24571 (Min: 0.23929, Max: 0.29617, Average: 0.24856) second(s) (102.83%)
Local: 0.23894 (Min: 0.22918, Max: 0.32741, Average: 0.24434) second(s) (100%)]])
Conclusion()
Add([[JIT matches the performance of globals and upvalues. This is a good practice to localize all variables you need, upvalues are still faster.]])
End()
End()
PlainLua()
Benchmark([[
!Global: 0.9605 (Min: 0.937, Max: 1.075, Average: 0.97355) second(s) (111.42%)
Local: 0.862 (Min: 0.845, Max: 0.939, Average: 0.86418) second(s) (100%)]])
Conclusion()
Add([[Upvalues are faster than globals.]])
End()
End()
End()
StartTest(2) -- Local vs Global table indexing
Text()
Predefines()
Code([[local s = math.sin]])
TestCode([[math.sin(3.14)]])
TestCode([[s(3.14)]])
PropertySheet(10000000)
LuaJITOn()
Asm([[
!Global table indexing: 38
Local: 18 ?]],[[
mov dword [0x24660410], 0x1
movsd xmm7, [rdx+0x40]
cvttsd2si eax, xmm7
xorps xmm6, xmm6
cvtsi2sd xmm6, eax
ucomisd xmm7, xmm6
jnz 0x7ffa543c0010 ->0
jpe 0x7ffa543c0010 ->0
cmp eax, 0x7ffffffe
jg 0x7ffa543c0010 ->0
cvttsd2si edi, [rdx+0x38]
cmp dword [rdx+0x14], -0x09
- jnz 0x7ffa543c0010 ->0
- cmp dword [rdx+0x10], 0x2467f788
- jnz 0x7ffa543c0010 ->0
- mov ebp, [0x2467f790]
- cmp dword [rbp+0x1c], +0x3f
- jnz 0x7ffa543c0010 ->0
- mov ebx, [rbp+0x14]
- mov rsi, 0xfffffffb24665fd8
- cmp rsi, [rbx+0x518]
- jnz 0x7ffa543c0010 ->0
- cmp dword [rbx+0x514], -0x0c
jnz 0x7ffa543c0010 ->0
+ cmp dword [rdx+0x18], 0x2466aca0
- mov edx, [rbx+0x510]
- cmp dword [rdx+0x1c], +0x1f
- jnz 0x7ffa543c0010 ->0
- mov ecx, [rdx+0x14]
- mov rsi, 0xfffffffb24666548
- cmp rsi, [rcx+0x230]
- jnz 0x7ffa543c0010 ->0
- cmp dword [rcx+0x22c], -0x09
- jnz 0x7ffa543c0010 ->0
- cmp dword [rcx+0x228], 0x24666520
jnz 0x7ffa543c0010 ->0
add edi, +0x01
cmp edi, eax
jg 0x7ffa543c0014 ->1]])
Conclusion()
Add([[As the first test concluded, each table indexing can cost around 11 additional instructions. Localizing ]]) InlineCode("math") Add(" table won't help much. Localize your variables.")
End()
End()
LuaJITOff()
Benchmark([[
!Global table indexing: 0.36948 (Min: 0.36357, Max: 0.3908, Average: 0.37024) second(s) (146.27%)
Local: 0.25259 (Min: 0.24956, Max: 0.26611, Average: 0.25344) second(s) (100%)]])
Conclusion()
Add([[Localizing exact value will get you more performance.]])
End()
End()
PlainLua()
Benchmark([[
!Global table indexing: 0.9335 (Min: 0.884, Max: 1.039, Average: 0.93893) second(s) (120.84%)
Local: 0.7725 (Min: 0.733, Max: 0.889, Average: 0.77743) second(s) (100%)]])
Conclusion()
Add([[Localizing exact value will get you more performance.]])
End()
End()
End()
StartTest(3) -- Localized method (3 calls)
Text()
Predefines()
Code([[
local class = {
test = function() return 1 end
}]])
TestCode([[
class.test()
class.test()
class.test()]])
TestCode([[
local test = class.test
test()
test()
test()]])
PropertySheet(10000000)
LuaJITOn()
Asm([[
Direct call: 35
Localized call: 35]])
Conclusion()
Add([[LuaJIT compiles them with the same performance.
However, LuaJIT suggests not to second-guess the JIT compiler, because unnecessary localization can create more complicated code.
Localizing ]]) InlineCode("local c = a+b") Add([[ for ]]) InlineCode("z = x[a+b] + y[a+b]") Add([[ is redundant. JIT perfectly compiles such code as ]]) InlineCode("a[i][j] = a[i][j] * a[i][j+1]") Add(".")
End()
End()
LuaJITOff()
Benchmark([[
!Direct call: 0.5611 (Min: 0.55, Max: 0.6099, Average: 0.56474) second(s) (120.64%)
Localized call: 0.46508 (Min: 0.4563, Max: 0.5834, Average: 0.46996) second(s) (100%)]])
Conclusion()
Add([[Unlike JIT compiler, JIT interpreter still runs faster with localized functions due to ]]) InlineCode("MOV") Add([[ instruction.]])
End()
End()
PlainLua()
Benchmark([[
!Direct call: 1.6065 (Min: 1.516, Max: 1.843, Average: 1.62501) second(s) (120.38%)
Localized call: 1.3345 (Min: 1.297, Max: 1.647, Average: 1.35458) second(s) (100%)]])
Conclusion()
Add([[Localized function speeds up the code due to ]]) InlineCode("MOV") Add([[ instruction.]])
End()
End()
End()
StartTest(4) -- Unpack
Text()
Predefines()
Code([[
local min = math.min
local unpack = unpack
local a = {100, 200, 300, 400}
local function unpack4(a)
return a[1], a[2], a[3], a[4]
end]])
TestCode([[min(a[1], a[2], a[3], a[4])]])
TestCode([[min(unpack(a))]])
TestCode([[min(unpack4(a))]])
PropertySheet(100000000)
LuaJITOn()
Benchmark([[
Indexing and unpack4: 0.03403 (Min: 0.0316, Max: 0.05607, Average: 0.03531) second(s) (100%)
!unpack: 4.71555 (Min: 4.54112, Max: 5.6054, Average: 4.75909) second(s) (13858.80%) (138 times slower)]])
Asm([[
Indexing: 36
!unpack: 46 NYI2.0 RET2.1 ?
unpack4: 36]],[[
mov dword [0x30100410], 0x1
+ movsd xmm6, [0x30149c60]
movsd xmm7, [rdx+0x58]
cvttsd2si eax, xmm7
xorps xmm6, xmm6
cvtsi2sd xmm6, eax
ucomisd xmm7, xmm6
jnz 0x7ffa538b0010 ->0
jpe 0x7ffa538b0010 ->0
cmp eax, 0x7ffffffe
jg 0x7ffa538b0010 ->0
cvttsd2si edi, [rdx+0x50]
cmp dword [rdx+0x2c], -0x09
jnz 0x7ffa538b0010 ->0
cmp dword [rdx+0x28], 0x3010d498
jnz 0x7ffa538b0010 ->0
mov ebx, [0x30110c60]
add ebx, -0x18
cmp ebx, edx
jnz 0x7ffa538b0010 ->0
cmp dword [rdx+0x1c], -0x0c
jnz 0x7ffa538b0010 ->0
- mov edx, [rdx+0x18]
- cmp dword [rdx+0x18], +0x04
- jbe 0x7ffa538b0010 ->0
- mov ecx, [rdx+0x8]
- cmp dword [rcx+0xc], 0xfffeffff
- jnb 0x7ffa538b0010 ->0
- cmp dword [rcx+0x14], 0xfffeffff
- jnb 0x7ffa538b0010 ->0
- cmp dword [rcx+0x1c], 0xfffeffff
- jnb 0x7ffa538b0010 ->0
- cmp dword [rcx+0x24], 0xfffeffff
- jnb 0x7ffa538b0010 ->0
- add edi, +0x01
- cmp edi, eax
- jg 0x7ffa538b0014 ->1
+ xorps xmm7, xmm7
+ cvtsi2sd xmm7, esi
+ mov eax, [0x301004b0]
+ mov eax, [rax+0x20]
+ sub eax, edx
+ cmp eax, 0xa0
+ jb 0x7ffa538b0014 ->1
+ mov dword [rdx+0x94], 0xfffffff4
+ mov [rdx+0x90], edi
+ mov dword [rdx+0x8c], 0x30110bdc
+ mov dword [rdx+0x88], 0x301032c8
+ mov dword [rdx+0x84], 0xfffffff7
+ mov dword [rdx+0x80], 0x30106ae0
+ movsd [rdx+0x78], xmm6
+ mov dword [rdx+0x74], 0x30111748
+ mov dword [rdx+0x70], 0x3010c7a8
+ movsd [rdx+0x68], xmm7
+ movsd [rdx+0x50], xmm7
+ add edx, 0x90
+ mov eax, 0x2
+ mov esi, 0x301004a8
+ mov ebx, 0x30100fe0
+ jmp 0x7ffa297d43c1]])
Conclusion()
Add([[Avoid using ]]) InlineCode("unpack") Add([[ for small table with known size. As an alternative you can use this function:]])
Code([[
do
local concat = table.concat
local loadstring = loadstring
function createunpack(n)
local ret = {"local t = ... return "}
for k = 1, n do
ret[1 + (k-1) * 4] = "t["
ret[2 + (k-1) * 4] = k
ret[3 + (k-1) * 4] = "]"
if k ~= n then ret[4 + (k-1) * 4] = "," end
end
return loadstring(concat(ret))
end
end]])
Add([[This function has 1 limitation. The maximum number of returned values is 248. The limit of LuaJIT ]]) InlineCode([[unpack]]) Add([[ function is 7999 with default settings.]]) N()
Add([[At least ]]) InlineCode([[createunpack]]) Add([[ can create JIT-compiled unpack (]]) InlineCode([[unpack4]]) Add([[ is basically ]]) InlineCode([[createunpack(4)]]) Add([[)]])
End()
End()
LuaJITOff()
Benchmark([[
Indexing: 3.73678 (Min: 3.60006, Max: 4.61773, Average: 3.78408) second(s) (100%)
!unpack: 5.56231 (Min: 5.12473, Max: 6.89518, Average: 5.69063) second(s) (148.85%)
unpack4: 4.17394 (Min: 4.12066, Max: 4.90567, Average: 4.34065) second(s) (111.69%)]])
Conclusion()
Add([[Avoid using ]]) InlineCode("unpack") Add([[ for small table with known size. As an alternative you can use the function mentioned on LuaJIT tab.]])
End()
End()
PlainLua()
Benchmark([[
Indexing: 12.272 (Min: 11.929, Max: 14.207, Average: 12.38047) second(s) (115.97%)
unpack: 10.5815 (Min: 10, Max: 11.586, Average: 10.56572) second(s) (100%)
unpack4: 14.855 (Min: 14.491, Max: 18.836, Average: 15.07444) second(s) (140.38%)]])
Conclusion()
Add([[Any method is ok, ]]) InlineCode([[unpack4]]) Add([[ is the slowest probably because of the function call overhead.]])
End()
End()
End()
StartTest(5) -- Find and return maximum value
Text()
Predefines()
Code([[
local max = math.max
local num = 100
local y = 0]])
TestCode([[local x = max(num, y)]])
TestCode([[
if (num > y) then
local x = num
end]])
TestCode([[local x = num > y and num or x]])
PropertySheet(10000000)
LuaJITOn()
Asm([[
math.min: 18
if (num > y) then: 18
a and b or c: 18]])
Conclusion()
Add([[LuaJIT compiles them with the same performance.]])
End()
End()
LuaJITOff()
Benchmark([[
!math.min: 0.23708 (Min: 0.22686, Max: 0.28223, Average: 0.23841) second(s) (147.10%)
if (num > y) then: 0.16116 (Min: 0.15814, Max: 0.169, Average: 0.16173) second(s) (100%)
a and b or c: 0.18716 (Min: 0.18291, Max: 0.19907, Average: 0.18795) second(s) (116.13%)]])
Conclusion()
InlineCode([[math.min]]) Add([[ has a function overhead, which probably makes it slower.]])
End()
End()
PlainLua()
Benchmark([[
!math.min: 0.647 (Min: 0.621, Max: 0.731, Average: 0.65725) second(s) (134.93%)
if (num > y) then: 0.4795 (Min: 0.464, Max: 0.56, Average: 0.48323) second(s) (100%)
a and b or c: 0.528 (Min: 0.501, Max: 0.726, Average: 0.54099) second(s) (110.11%)]])
Conclusion()
InlineCode([[math.min]]) Add([[ has a function overhead, which probably makes it slower.]])
End()
End()
End()
StartTest(6) -- nil check (if vs a or b)
Text()
Predefines()
Code([[local y]])
TestCode([[if not y then
local x = 1
else
local x = y
end]])
TestCode([[local x = y or 1]])
PropertySheet(10000000)
LuaJITOn()
Asm([[
if: 24
a or b: 24]])
Conclusion()
Add([[LuaJIT compiles them with the same performance.]])
End()
End()
LuaJITOff()
Benchmark([[
if: 0.13572 (Min: 0.13033, Max: 0.19183, Average: 0.13831) second(s) (100%)
a or b: 0.13608 (Min: 0.12298, Max: 0.23668, Average: 0.13989) second(s) (100.26%)]])
Conclusion()
InlineCode([[a or b]]) Add([[ should be faster due to unary test and copy instructions ]]) InlineCode([[ISTC]]) Add([[ and ]]) InlineCode([[ISFC]]) Add([[.]])
End()
End()
PlainLua()
Benchmark([[
if: 0.398 (Min: 0.382, Max: 0.484, Average: 0.40146) second(s) (112.11%)
a or b: 0.355 (Min: 0.349, Max: 0.367, Average: 0.35508) second(s) (100%)]])
Conclusion()
InlineCode([[a or b]]) Add([[ should be faster due to ]]) InlineCode([[TESTSET]]) Add([[ instruction.]])
End()
End()
End()
StartTest(7) -- x^2 vs x*x vs math.pow
Text()
Predefines()
Code([[local x = 10
local pow = math.pow]])
TestCode([[local y = x ^ 2]])
TestCode([[local y = x * x]])
TestCode([[local y = pow(x, 2)]])
PropertySheet(10000000)
LuaJITOn()
Asm([[
x ^ 2: 18
x * x: 18
math.pow: 18]])
Conclusion()
Add([[LuaJIT compiles them with the same performance.]])
End()
End()
LuaJITOff()
Benchmark([[
x ^ 2: 0.60192 (Min: 0.5671, Max: 0.85234, Average: 0.61451) second(s) (442.13%) (4 times slower)
x * x: 0.13614 (Min: 0.13237, Max: 0.19182, Average: 0.13845) second(s) (100%)
math.pow: 0.69741 (Min: 0.59753, Max: 0.95067, Average: 0.70204) second(s) (512.26%) (5 times slower)
]])
Conclusion()
Add([[Use multiply instead of power if you know the exact exponent. ]]) InlineCode([[math.pow]]) Add([[ has a function overhead.]])
End()
End()
PlainLua()
Benchmark([[
x ^ 2: 1.044 (Min: 1.023, Max: 1.242, Average: 1.05641) second(s) (269.07%) (2 times slower)
x * x: 0.388 (Min: 0.376, Max: 0.456, Average: 0.39083) second(s) (100%)
math.pow: 1.3125 (Min: 1.211, Max: 1.529, Average: 1.32576) second(s) (338.27%) (3 times slower)]])
Conclusion()
Add([[Use multiply instead of power if you know the exact exponent. ]]) InlineCode([[math.pow]]) Add([[ has a function overhead.]])
End()
End()
End()
StartTest(8) -- math.fmod vs % operator
Text()
Predefines()
Code([[local fmod = math.fmod
local function jit_fmod(a, b)
if b < 0 then b = -b end
if a < 0 then
return -(-a % b)
else
return a % b
end
end]])
TestCode([[local x = fmod(times, 30)]])
TestCode([[local x = (times % 30)]])
TestCode([[local x = jit_fmod(times, 30)]])
PropertySheet(10000000)
LuaJITOn()
Asm([[
!fmod: 55 NYI2.0 STITCH
%: 18
JITed fmod: 20 ?]],[[
mov dword [0x2add0410], 0x4
movsd xmm7, [rdx+0x48]
cvttsd2si eax, xmm7
xorps xmm6, xmm6
cvtsi2sd xmm6, eax
ucomisd xmm7, xmm6
jnz 0x7ffa543c0010 ->0
jpe 0x7ffa543c0010 ->0
cmp eax, 0x7ffffffe
jg 0x7ffa543c0010 ->0
cvttsd2si edi, [rdx+0x40]
cmp dword [rdx+0x24], -0x09
jnz 0x7ffa543c0010 ->0
cmp dword [rdx+0x20], 0x2adf2c00
jnz 0x7ffa543c0010 ->0
+ test edi, edi
+ jl 0x7ffa543c0014 ->1
add edi, +0x01
cmp edi, eax
jg 0x7ffa543c0018 ->2]])
Benchmark([[
!fmod: 0.2961 (Min: 0.2885, Max: 0.4984, Average: 0.30364) second(s) (7670.98%) (76 times slower)
% and JITed fmod: 0.00386 (Min: 0.00305, Max: 0.00643, Average: 0.00401) second(s) (100%)]])
Conclusion()
Add([[Use ]]) InlineCode([[%]]) Add([[ for positive modulo. For negative or mixed modulo use JITed fmod.]])
End()
End()
LuaJITOff()
Benchmark([[
!fmod: 0.33687 (Min: 0.3147, Max: 0.42487, Average: 0.34024) second(s) (239.59%) (2 times slower)
%: 0.1406 (Min: 0.13166, Max: 0.21037, Average: 0.14226) second(s) (100%)
!JITed fmod: 0.35584 (Min: 0.34378, Max: 0.52199, Average: 0.36319) second(s) (253.07%) (2 times slower)]])
Conclusion()
Add([[JITed fmod solves compilation problem but it's slower in interpreter mode]])
End()
End()
PlainLua()
Benchmark([[
!fmod: 0.7055 (Min: 0.657, Max: 0.842, Average: 0.7135) second(s) (182.77%)
%: 0.386 (Min: 0.374, Max: 0.471, Average: 0.39029) second(s) (100%)
!JITed fmod: 0.858 (Min: 0.812, Max: 1.127, Average: 0.8753) second(s) (222.27%) (2 times slower)]])
Conclusion()
Add([[JITed fmod is not recommended for plain Lua. Use module operator for positive numbers and ]]) InlineCode([[math.fmod]]) Add([[ for negative and mixed.]])
End()
End()
End()
StartTest(9) -- Predefined function or anonymous function in argument
Text()
Predefines()
Code([[local func1 = function(a, b, func) return func(a + b) end
local func2 = function(a) return a * 2 end]])
TestCode([[local x = func1(1, 2, function(a) return a * 2 end)]])
TestCode([[local x = func1(1, 2, func2)]])
PropertySheet(10000000)
LuaJITOn()
Asm([[
!Function in argument: NYI2.1
Localized function: 18]])
Benchmark([[
!Function in argument: 0.61696 (Min: 0.57184, Max: 0.81876, Average: 0.62018) second(s) (18791.11%) (187 times slower)
Localized function: 0.00328 (Min: 0.00307, Max: 0.00733, Average: 0.00354) second(s) (100%)]])
Conclusion()
Add([[If it's possible, localize your function and re-use it. If you need to provide a local to the closure try different approach of passing values. Simple example is changing state iterator to stateless.
Example of different value passing:]])
Code([[
function func()
local a, b = 50, 10
timer.Simple(5, function()
print(a + b)
end)
end]])
Add([[In this example ]]) InlineCode([[timer.Simple]]) Add([[ can't pass arguments to the function, we can change the style of value passing from function upvalues to main chunk upvalues:]])
Code([[
local Ua, Ub
local function printAplusB()
print(Ua + Ub)
end
function func()
local a, b = 50, 10
Ua, Ub = a, b
timer.Simple(5, printAplusB)