[英]Does the Lua compiler optimize local vars?
Is the current Lua compiler smart enough to optimize away local variables that are used for clarity? 当前的Lua编译器是否足够智能以优化用于清晰的局部变量?
local top = x - y
local bottom = x + y
someCall(top, bottom)
Or does inlining things by hand run faster? 或者手动内联的速度更快?
someCall(x - y, x + y)
Since Lua often compiles source code into byte code on the fly, it is designed to be a fast single-pass compiler. 由于Lua经常将源代码编译成字节代码,因此它被设计为快速单通道编译器。 It does do some constant folding, but other than that there are not many optimizations. 它确实做了一些常量折叠,但除此之外没有很多优化。 You can usually check what the compiler does by executing luac -l -l -p file.lua
and looking at the generated (disassembled) byte code. 您通常可以通过执行luac -l -l -p file.lua
并查看生成的(反汇编的)字节代码来检查编译器的功能。
In your case the Lua code 在你的情况下Lua代码
function a( x, y )
local top = x - y
local bottom = x + y
someCall(top, bottom)
end
function b( x, y )
someCall(x - y, x + y)
end
results int the following byte code listing when run through luac5.3 -l -l -p file.lua
(some irrelevant parts skipped): 结果通过luac5.3 -l -l -p file.lua
运行后面的字节代码列表(跳过一些不相关的部分):
function <file.lua:1,5> (7 instructions at 0xcd7d30)
2 params, 7 slots, 1 upvalue, 4 locals, 1 constant, 0 functions
1 [2] SUB 2 0 1
2 [3] ADD 3 0 1
3 [4] GETTABUP 4 0 -1 ; _ENV "someCall"
4 [4] MOVE 5 2
5 [4] MOVE 6 3
6 [4] CALL 4 3 1
7 [5] RETURN 0 1
constants (1) for 0xcd7d30:
1 "someCall"
locals (4) for 0xcd7d30:
0 x 1 8
1 y 1 8
2 top 2 8
3 bottom 3 8
upvalues (1) for 0xcd7d30:
0 _ENV 0 0
function <file.lua:7,9> (5 instructions at 0xcd7f10)
2 params, 5 slots, 1 upvalue, 2 locals, 1 constant, 0 functions
1 [8] GETTABUP 2 0 -1 ; _ENV "someCall"
2 [8] SUB 3 0 1
3 [8] ADD 4 0 1
4 [8] CALL 2 3 1
5 [9] RETURN 0 1
constants (1) for 0xcd7f10:
1 "someCall"
locals (2) for 0xcd7f10:
0 x 1 6
1 y 1 6
upvalues (1) for 0xcd7f10:
0 _ENV 0 0
As you can see, the first variant (the a
function) has two additional MOVE
instructions, and two additional locals. 如您所见,第一个变体( a
函数)有两个额外的MOVE
指令和另外两个本地变量。
If you are interested in the details of the opcodes, you can check the comments for the OpCode
enum in lopcodes.h . 如果您对操作码的详细信息感兴趣,可以在lopcodes.h中查看OpCode
枚举的注释 。 Eg the opcode format for OP_ADD
is: 例如, OP_ADD
的操作码格式是:
OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
So the 2 [3] ADD 3 0 1
from above takes the values from registers 0 and 1 (the locals x
and y
in this case), adds them together, and stores the result in register 3. It is the second opcode in this function and the corresponding source code is on line 3. 所以上面的2 [3] ADD 3 0 1
从寄存器0和1(本例中的本地x
和y
中取值,将它们加在一起,并将结果存储在寄存器3中。这是第二个操作码功能和相应的源代码在第3行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.