lua-users home
lua-l archive

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


On Sat, Sep 11, 2021 at 8:15 AM Philippe Verdy wrote:
A local <const> variable is not necessary initialized with a simple constant value, it may be initialized by the value of a complex _expression_ that needs to be evaluated only once, and its value stored somewhere.
Imagine you declare thousands of such local <const> variables

Please read a post attentively before answering.
I'm talking ONLY about local <const> variables initialized by a simple value (number or string).
Only these constants should not count in local variables quota.

Although some expressions are evaluated by Lua at compile time (for example, arithmetic on number literals), if you assign arbitrary _expression_ to unused constant, Lua may not optimize it completely:

local UNUSED <const> = "aaaaa".."bbbbb"

UNUSED is never used in the code, but both strings "aaaaa" and "bbbbb" are present in the bytecode and in Lua VM memory.  And CONCAT instruction is always executed.
So, there is no benefit here (at least, in the current implementation of Lua).
That's why I consider only local <const> variables initialized by a simple value.

 
Now imagine the compiler accepts thousands of such "local <const>" variable declarations in a function body. It will not know when parsing them if these variables are used or not: it will know that only after parsing the whole function body.

What does "not know when parsing them" mean?
The whole Lua source (including all constant definitions) must always be parsed  :-)

If a local <const> variable is initialized with a simple value, it will never occupy a VM register independently of whether the constant is used or unused in the code.
So, the compiler can set the flag "this local <const> variable should not be counted in local variables quota" while parsing the line where the constant is defined, there is no need to parse the whole function body.