Log In  


I've been rebuilding/refactoring a recent game in efforts to reduce clutter and increase performance, as well as get under the dreaded compression limit.

For whatever reason, I've started to nest functions...mostly as a way to better organize code. This works just fine but I'm wondering if there's any real downside to it?

I've been nesting functions that are relative only to code of the parent function, so as to reduce some code. Usually some math or condition checking or something...

As of yet, it doesn't seem to impact on my games at all. Just wondering...

function myParent()
  function myNested()
    ...often repeated task code...
  end

  if Something then myNested() end
  if Something and SomethingElse then myNested() end

  ...you get the idea...
end

myParent()


There is a performance impact—but it's fairly insignificant. In my opinion there's no reason to avoid nested functions unless you're trying to optimize something that's called hundreds or thousands of times per frame, e.g. a triangle rasterizer.


Have a look at your memory profile - I had a scenerio where the garbage collector could not keep up with allocations (inner function)/deallocation(out of scope).

Secondly, suggest to add local to your function decl. Not sure what lua will do (global?).



[Please log in to post a comment]