目前的Lua编译器是否足够智能,可以优化掉仅用于代码清晰度的局部变量?
local top = x - y
local bottom = x + y
someCall(top, bottom)
手动内联是否更快?
someCall(x - y, x + y)
目前的Lua编译器是否足够智能,可以优化掉仅用于代码清晰度的局部变量?
local top = x - y
local bottom = x + y
someCall(top, bottom)
someCall(x - y, x + y)
由于Lua通常会即时将源代码编译成字节码,因此它被设计为快速的一遍编译器。它确实会做一些常量折叠,但除此之外并没有太多优化。您通常可以通过执行luac -l -l -p file.lua
并查看生成的(反汇编的)字节码来检查编译器的操作。
在您的情况下,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
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
a
函数)有两个额外的MOVE
指令和两个额外的局部变量。如果您对操作码的细节感兴趣,可以查看lopcodes.h中的OpCode
枚举的注释。例如,OP_ADD
的操作码格式为:OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
因此,上面的2 [3] ADD 3 0 1
会将寄存器0和1(在这种情况下是本地变量x
和y
)的值相加,并将结果存储在寄存器3中。它是该函数中的第二个操作码,相应的源代码位于第3行。