lua-users home
lua-l archive

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


Le ven. 15 mai 2020 à 19:07, Andrea <andrea.l.vitali@gmail.com> a écrit :
Francisco,

always considered a local variable could not be collected until the
scope containing it ended, from manual reading, RI told me more or
less not to count on it ( IIRC, implementation does not collect, but I
saw that l8r, I didn't rely on source, I relied on manual reading ), I
noted it and acted accordingly.

 This is interesting. Can you be more specific? How can a local variable be collected if it is still in the scope?
 
It could be garbage collected if the scope of the variable was more precisely analyzed (not just "lexically" where you may want to reuse its value by adding code within that scope, but by effective area of use in that lexical scope).

Unfortunately, there's no tracking of the last use in the Lua compiler, scopes are only lexical and local variables reside in the table of local variables until the full table for the lexical scope is removed.
One way to avoid this is to unset the variable value explicitly in the Lua code (i.e. setting these variables explicitly to nil where we no longer need their value), but this is error prone, and a programmer should not have to manage that explicitly in the source code.

This automatic unsetting of unused variable could be made in the compiler itself (like what Java does for its class compiler: local varaibles are represented in an integer indexed array, and positions that are no longer needed can be reused for other local variables, as you can see if you look at the Java bytecode; this analysis also occurs in C/C++ compilers, still not in Lua).
And the Lua compiler could even eliminate "dead code" trying to assign a value to a variable that is never reused later.

Such optimizations may be disabled in a debugging/interactive session using reflection mechanisms (where the user of the debugger may locally insert code, or may want to see the content of a local variable still in lexical scope even if it's no longer used in the existing code being debugged.)


_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org