lua-users home
lua-l archive

Re: Question about 'const' variable

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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

AltStyle によって変換されたページ (->オリジナル) /