Question about the performance & optimisation of Lua functions

Since we have some programming wizards at our midst, I thought to ask you guys for answers rather than trying to guess.

I’ve done a lot of optimisation in my days, and granted, in most cases this level of optimisation would be completely unnecessary.

However, I am not sure why method 1, see below, is around 2 to 2.5 times faster than method 2, see below. I would have expected that, since method 2’s function X contains function Y that it’d be at least as fast if not faster than method 1, where both functions are separate.

What is the reason behind method 1 being significantly faster than method 2? Is it some compiler optimisation?

-- METHOD 1: local function Y() return true end local function X() Y() return true end local getTimer = system.getTimer local startTime = getTimer() local iterations = 1000000 for i = 1, iterations do X() end print( "TIME:"..(getTimer()-startTime) )

-- METHOD 2: local function X() local function Y() return true end Y() return true end local getTimer = system.getTimer local startTime = getTimer() local iterations = 1000000 for i = 1, iterations do X() end print( "TIME:"..(getTimer()-startTime) )

Functions are actual objects in Lua. You’re instantiating such an object from a prototype and putting it in a variable named Y. There is some cost to this: allocation, hooking up debug info, closing any upvalues, and so on. See here for some of the relevant bits.

In #2, you do this every time. Also, at the end of X , Y goes out of scope without anything else referencing it and so becomes garbage, contributing to a collection at some point.

As far as i know it’ because Method 1 creates both functions only once, while method 2 creates a new Y function on every iteration. (Allocating new memory and assigning a new pointer to it)

As in lua functions are first class values and are passed by their reference, in method 1 Lua needs only to access the pointer in memory of the two functions to execute them while in method 2 it needs to create 1000000 different (but identical) functions.

Thank you for the answers, gents.

I found the Lua lfunc.c page especially interesting.

Functions are actual objects in Lua. You’re instantiating such an object from a prototype and putting it in a variable named Y. There is some cost to this: allocation, hooking up debug info, closing any upvalues, and so on. See here for some of the relevant bits.

In #2, you do this every time. Also, at the end of X , Y goes out of scope without anything else referencing it and so becomes garbage, contributing to a collection at some point.

As far as i know it’ because Method 1 creates both functions only once, while method 2 creates a new Y function on every iteration. (Allocating new memory and assigning a new pointer to it)

As in lua functions are first class values and are passed by their reference, in method 1 Lua needs only to access the pointer in memory of the two functions to execute them while in method 2 it needs to create 1000000 different (but identical) functions.

Thank you for the answers, gents.

I found the Lua lfunc.c page especially interesting.