Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are localized functions faster in Lua?

TEST 1: Localize

Code:

local min = math.min

Results:

Non-local: 0.719 (158%)
Localized: 0.453 (100%)

Conclusion:

Yes, we should localize all standard lua and Spring API functions.

Source: https://springrts.com/wiki/Lua_Performance

 

What is the reason for that performance boost?

like image 900
816-8055 Avatar asked Mar 14 '23 14:03

816-8055


1 Answers

local min = math.min

Remember that table.name is just syntax sugar for table["name"] (they're exactly equivalent). And globals are just keys in the environment table, so math.min is _ENV["math"]["min"]. That's two hashtable lookups to get at the actual function value.

Copying the value into a local puts it in a VM register so there's no lookup.

like image 117
Mud Avatar answered Apr 07 '23 07:04

Mud