Is it worth to localize defolds callbacks?

init, update, on_message, etc are all global variables and accessed through global variable table what is considered slow. But this callbacks called every frame, even multiple times.
So, if we did:

local defold = _G function defold.update( ... ) end function defold.on_message( ... ) end ...

Will it faster?

I would assume that would be identical, they would still be placed in the global table and fetched from there. More importantly, there are also many other things happening when we invoke the update of a script, like setting up its context with instance data, etc. Essentially if you want speed, you should minimise the number of update functions being called every frame. Many things can be done reactively by triggering animations etc. so that should be preferred and can in many cases completely remove the need for update functions.
In the long term, we are providing an alternative means of doing perf intensive things at C speed. We still don’t know exactly in what form, but parts of it is being designed right now.

2 Likes

Localising functions that are called once per frame will have no noticeable effect on performance. What you need to be on the lookout for are things such as creating lots of tables each frame and running a lot of code each frame. Use the profiler to identify the amount of time spent in scripts. You can use collectgarbage to check Lua memory usage. And if you need to deep dive into the performance of your scripts you can use the pure Lua profiler ProFi.