lua-users home
lua-l archive

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

Leo Razoumov wrote:
> On Sun, Feb 13, 2011 at 10:18, Mike Pall <> wrote:
> > Umm, you misunderstood: this out of the scope of what a compiler
> > automatically can or should do for you. C++ compilers don't write
> > templates for you, either. :-) It's pretty easy to do it yourself
> > in Lua, far easier than writing C++ templates.
> Sorry for my misunderstanding but I thought that LuaJIT-2 can unroll
> fixed length loops like for i=1,3 do ... end.

Yes, it even unrolls loops with unknown, but short iteration
counts. But not forever and not at a global scope. The heuristics
are just not set up for doing this dozens of times inside a single
root trace. This would really hurt in the general case.

> It is a pity that Lua does not have a concept of a constant variable
> so that when I write  for i=1,n do ... end I really mean that value of
> n cannot change. Is there any room to hint LuaJIT about certain
> logical constrains of the executing code to assist it in
> optimizations?

This is one meta-level higher than the compiler operates on. It's
not hard to do something like this for simple examples. But I see
some problems in real-world use cases: specialization and dispatch
for 'n' needs to be done as early as possible and not repeated.
But the original 'n' is probably loaded from somewhere else,
passed around and so on. That would lose its pseudo-const property
and kill later specializations. Not so easy ...