lua-users home
lua-l archive

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

>> No, the code generator does not do constant folding nor 
>> common expression eliminations. It works hard on other 
>> optimizations instead.
> Any plans to implement those then?

No, I don't think these will ever be included in the core code generator
to avoid bloat. But that doesn't mean that they cannot be done in luac
eventually. (luac has in the past done some optimizations, specially
identification of duplicate constants, but this is now done in the core.)

>And while we're on the subject, what are some of the other optimizations?

Roberto will know this better than I do, but the core code generator does
jump optimization, that is, try avoids jumps to jumps, for instance. It also
does try to identify some constant expression in conditionals. For instance,
infinite loops generate code that does not evaluate the condition:

 [lua:/tmp/L/bin] cat i
 while true do
 [lua:/tmp/L/bin] luac -l i

 main <i:0> (6 instructions, 24 bytes at 0x805b678)
 0 params, 2 stacks, 0 upvalues, 0 locals, 2 constants, 0 functions
	 1       [1]     JMP             0 3     ; to 5
	 2       [2]     GETGLOBAL       0 0     ; print
	 3       [2]     GETGLOBAL       1 1     ; a
	 4       [2]     CALL            0 2 1
	 5       [1]     JMP             0 -4    ; to 2
	 6       [3]     RETURN          0 1 0

Note the unconditional JMP at line 5.

In general, the core code generator focuses on things that programmers cannot
optimize on their own. Constant folding and common expression eliminations are
easy to handle using locals (which are very efficient in Lua) and so are left
to programmers.

The one thing that core code generator does not currently do and which would
be nice to help programmers is dead code elimination. Before the introduction
of long comments (and after the removal of the preprocessor), the only way to
comment code out was by using things like "if nil do ... end" or by creating
strings that were never again used. But now this is easier to do with long
comments (--[[...]]) and carries no cost.