Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Question about the performance & optimisation of Lua functions
Started by XeduR @Spyric Aug 16 2019 11:34 AM

- - - - -
3 replies to this topic

Best Answer StarCrunch , 16 August 2019 - 12:16 PM

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.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 893 posts
  • Corona SDK

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


[TOPIC: post.html]
#2

StarCrunch

[GLOBAL: userInfoPane.html]
StarCrunch
  • Contributor

  • 815 posts
  • Corona SDK

  Best Answer

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.


  • XeduR @Spyric likes this

[TOPIC: post.html]
#3

Arteficio

[GLOBAL: userInfoPane.html]
Arteficio
  • Enthusiast

  • 81 posts
  • Corona SDK

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.
  • XeduR @Spyric likes this

[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 893 posts
  • Corona SDK

Thank you for the answers, gents.

I found the Lua lfunc.c page especially interesting.




[topic_controls]
[/topic_controls]