lua-users home
lua-l archive

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


On Fri, Sep 2, 2011 at 8:25 AM, Mike Pall <mikelu-1109@mike.de> wrote:
> You're creating a new closure for Internal() on every call. Don't
> do that. LuaJIT specializes to each closure and there's a limit to
> that. Create the closure outside of Test().

Lua 5.2 beta can cache that function [1-2], but LuaJit is currently
based on 5.1 semantics with environments, which doesn't necessarily in
general allow the optimizer to safely pull that function definition
outside.  You can do your own caching as such if your prefer to keep
the function inline:

  local Internal
  function Test()
    Internal = Internal or function() end
    for i=0,10000000 do Internal() end
  end

[1] http://www.lua.org/work/doc/manual.html - "Closures with the same
reference are always equal. Closures with any detectable difference
(different behavior, different definition) are always different."
[2] http://lua-users.org/lists/lua-l/2010-07/msg00862.html