lua-users home
lua-l archive

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


But,the function g is actually optimized.

Maybe Lua team may has some rule for these cases.

在 2021年7月31日星期六,Hugo Musso Gualandi <hgualandi@inf.puc-rio.br> 写道:
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