816-8055
816-8055

Reputation: 557

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?

Upvotes: 4

Views: 2295

Answers (1)

Mud
Mud

Reputation: 28991

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.

Upvotes: 7

Related Questions