I (think I) have a decent idea of how to write code that the JIT will like, but the name-dependent performance thing was quite a surprise. Is there a good way to know that one's code is not falling into whatever memory-layout trap Mike mentioned? If there's a 25%+ differential to be had in a tight loop by optimizing some names, that would potentially be worth tracking down for some of my work. Or should I interpret Mike's comments about broader inefficiencies in that example to mean that, with suitably optimized code/data structures, the naming problem doesn't arise? Any thoughts would be appreciated :) On Mon, Jun 4, 2012 at 8:27 AM, Francesco Abbate <francesco.bbt@xxxxxxxxx>wrote: > Hi, > > I've actually some experience writing optimized code for LuaJIT2. > Usually you have to respect some rules to ensure that JIT code is > actually generated and it does not fallback to interpreter mode. > > Normally what you should avoid is: > - do not use global variables inside loops > * use local declarations to hold the variables > * use the 'strict' module to detect unintentional use of global variables > - use an imperative style of coding: > * smart functional code tend to spoil optimization if closures have > to be createad inside a loop > - avoid objects creation inside a loop > - do not use deeply nested tables to hold the data used inside loops > - write clean simple code > > If you respect these rules LuaJIT often make a very good job. > > It is somewhat inconvenient that sometimes "reasonable" code does not > get compiled at all but this is the way luajit works. It is not like a > C compiler that always produce fast code even if the code is slightly > suboptimal. > > Francesco > >