Not to discourage you, but I'd see inlining and the string.rep kind of optimizations easily doable via syntax modding (aka token filtering). It will still be a good thing to find such optimization points (btw, I've never needed string.rep myself...) but the eventual implementation could be merged with one of the filtering tools (MetaLua, luaSuper, ...).
Humberto S. N. dos Anjos kirjoitti 19.11.2007 kello 20:27:
Actually, no dynamic language (think Lua, Python, Ruby and the like) is friendly to static optimizations, because almost everything is left to runtime. This allows for some extremely cool stuff, but the code generator has to assume worst-case scenarios for pretty much everything.
But on most programs, very little truly dynamic code (things such as debug.setmetatable, loadstring with a string built at runtime, explicit setfenv stuff) is actually used, although any uses made are vital to the program's semantics (this is a paraphrase from a paper offering a type inferencer for Python, but the same reasoning applies here). So the main purpose of this compiler is to see how many static optimizations can be reasonably used.
Of course, some assumptions have to be made to make static optimizing viable. I came up with some, although now they seem a bit too strong:
- the debug library isn't used. The Lua manual recommends against it, because it can violate some Lua assumptions (local variables can't be accessed outside of their scope, numbers and functions can't have metatables), and it can be slow. So I assume no hackery of this sort is going on.
- basic modules and functions won't be modified, or at least not in a way that alters their semantics. With this I can rely on documented behavior to precompile functions calls, like compiling string.rep("a", 3) directly to "aaa". This might appear with some frequency when function calls are inlined.
On Nov 19, 2007 2:33 PM, Mark Hamburg <firstname.lastname@example.org
Given metamethods, how much can actually be optimized? Do you have examples
of the sorts of code your optimizer can improve? The prospect of code
optimization is certainly attractive, but my understanding had been that Lua
wasn't particularly friendly to such optimizations.
Humberto S. N. dos Anjos
<insert witty quote here>