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 <> 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

[1] - "Closures with the same
reference are always equal. Closures with any detectable difference
(different behavior, different definition) are always different."