lua-users home
lua-l archive

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


> I was wondering why Lua 5.2 does not preform the same optimisation for
> bytecode which 5.1 did when a there is not an operation before an
> OP_LOADNIL, for 5.1 the generator would remove the load nil
> instructions. For example a simple file which just contains a local
> which will be set to nil:
> $ cat ./op_loadnil.lua
> local a
> 
> For 5.1 produces the following:
> $ luac -l ./op_loadnil.lua
> main <./op_loadnil.lua:0,0> (1 instruction, 4 bytes at 0x100101050)
> 0+ params, 2 slots, 0 upvalues, 1 local, 0 constants, 0 functions
> 	1	[1]	RETURN   	0 1
> 
> Yet for 5.2 it leaves in the instruction:
> $ ./luac -l ./op_loadnil.lua
> main <./op_loadnil.lua:0,0> (2 instructions at 0x100100ee0)
> 0+ params, 2 slots, 1 upvalue, 1 local, 0 constants, 0 functions
> 	1	[1]	LOADNIL  	0 0
> 	2	[1]	RETURN   	0 1
> 
> Would someone please shine some light on this and any implications
> that may follow it.

Lua 5.1 fills with nils all slots of a function before calling it:

ldo.c:296:
    for (st = L->top; st < ci->top; st++)
      setnilvalue(st);

So, it does not need any initial LOADNIL instruction, because all slots
are already nil. On the ohter hand, it always pays the price, even if
these nils are nor necessary (e.g., when we declare our locals with
proper initialization).

Lua 5.2 fills with nils only the missing arguments:

ldo.c:329:
      for (; n < p->numparams; n++)
        setnilvalue(L->top++);  /* complete missing arguments */

So, slots are not nil when the function start and LOADNIL instructions
cannot be removed.

(This change also has some small implications for the garbage collector.)

-- Roberto