lua-users home
lua-l archive

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


On 2/16/2011 7:42 PM, Josh Haberman wrote:
Mike Pall<mikelu-1102<at>  mike.de>  writes:
Rule #437: Never hoist out constants (if possible)!
This seems like a big limitation to me -- is there any way to
work around it?

Hoisting calculations out of loops is a big efficiency win when
the calculation is based on any other variable.  Telling
programmers to hoist non-constant calculations but *not* hoist
constant calculations seems like too much mental burden, especially
when a hoisted constant calculation *looks* like it should be
at least as efficient as not hoisting.  There's also the
readability/maintainability benefits of giving constants a name.

I feel like you may be unduly influenced by the fact that
constant hoisting is currently not efficient, and extrapolating
from there that it is not good programming practice.  But I think
that it is widely recognized as good programming practice, at
least in my experience from numerous code reviews I have been
through.  Ideally there would be a way to make it efficient
instead of having to tell programmers over and over that the
efficiency of hoisting is the opposite of what they expect.

Is the debug API the issue here?  Without funny business going
on it seems that LJ should be able to recognize that a variable
is only assigned once.

Josh



What is "good programming practice" is often a question of what you want to achieve.

If you want (or need) to pull every trick in the book to make your code fast, you'll have to deal with the details of the compiler/runtime/os and/or hardware, and you might have to sacrifice some legibility to get there. Personally, I usually try to make code correct first, then make it clear, and then make it faster if necessary, but YMMV.

Of course, when implementing a JIT compiler speed is very important, but most code out there isn't performance critical...

Cheers,
Christian Tellefsen