[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Question about 'const' variable
- From: Hugo Musso Gualandi <hgualandi@...>
- Date: Fri, 30 Jul 2021 15:26:57 -0300
Actually, Lua 5.4 can optimize variables declared as <const>. The
problem is that it does not optimize the 1 == 1. This can be confusing
because, by coincidence, the generated code for 1 == 1 is the same as
the generated code for x == 1.
--- Lua example ---
local x<const> = "hello"
function f()
return x
end
function g()
local y<const> = 10
return y + y
end
function h()
if 1 == 1 then
return "hello"
else
return "world"
end
end
--- Generated bytecode ---
function f:
1 [3] LOADK 0 0 ; "hello"
2 [3] RETURN1 0
3 [4] RETURN0
function g:
1 [8] LOADI 0 20
2 [8] RETURN1 0
3 [9] RETURN0
function h:
1 [12] LOADI 0 1
2 [12] EQI 0 1 0
3 [12] JMP 3 ; to 7
4 [13] LOADK 0 0 ; "hello"
5 [13] RETURN1 0
6 [13] JMP 2 ; to 9
7 [15] LOADK 0 1 ; "world"
8 [15] RETURN1 0
9 [17] RETURN0