lua-users home
lua-l archive

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



On 03/12/2017 01:35 PM, Egor Skriptunoff wrote:
> Hi!
> 
> Just an interesting observation:
> multiple assignment in Lua is more costly than
> equivalent single assignments:
> 
> $ luac -l -p -
> local a,b,c,d
> a, b = c, d
> 
> main <stdin:0,0> (5 instructions at 0x1acc780)
> 0+ params, 5 slots, 1 upvalue, 4 locals, 0 constants, 0 functions
>     1    [1]    LOADNIL      0 3
>     2    [2]    MOVE         4 2
>     3    [2]    MOVE         1 3
>     4    [2]    MOVE         0 4
>     5    [2]    RETURN       0 1
>   
> $ luac -l -p -
> local a,b,c,d
> a = c
> b = d
> 
> main <stdin:0,0> (4 instructions at 0x231e780)
> 0+ params, 4 slots, 1 upvalue, 4 locals, 0 constants, 0 functions
>     1    [1]    LOADNIL      0 3
>     2    [2]    MOVE         0 2
>     3    [3]    MOVE         1 3
>     4    [3]    RETURN       0 1
> 
> It seems that Lua does not make an attempt to optimize
> bytecode (probably to reduce LOC of Lua sources?)
> 
> -- Egor

Generally "a, b = c, d" is not equal to "a = c; b = d" as
variables ("a" and "d") or ("b" and "c") may point to one
location.

Simple example - swap: "a, b = b, a".

-- Martin