Log In  
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()
P#44641 2017-09-26 15:12 ( Edited 2017-09-27 05:51)

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.

P#44642 2017-09-26 15:47 ( Edited 2017-09-26 19:51)

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?).

P#44654 2017-09-27 01:51 ( Edited 2017-09-27 05:51)

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-05-25 19:59 | 0.015s | 2097k | Q:16