#lua tips for optimisation

1 messages · Page 1 of 1 (latest)

left meteor
#

some tips for ur lua code to have better performance

#1.1
instead each time calling global functions u can cache them into local ones

-- bad
local player = getPlayer()
-- good
local getPlayer = getPlayer -- here we caching `getPlayer` function so script won't call it from _G but from local scope => it's faster
local player = getPlayer()

#1.2
in addition u can cache class function

-- bad
local item_module = item:getModule()
local item_full_type = item:getFullType()
-- good
local item_base = __classmetatables[InventoryItem.class].__index
local item_getFullType = item_base.getFullType
local item_getModule = item_base.getModule

local item_full_type = item_getModule(item)
local item_module = item_getFullType(item)

#1.3
please cache textures too ded

-- bad
drawTexture(getTexture("media/.../img1.png"), ...)
drawTexture(getTexture("media/.../img2.png"), ...)
-- good
local textures = {
  [1] = getTexture("media/.../img1.png")
  [2] = getTexture("media/.../img2.png")
}
drawTexture(textures[1], ...)
drawTexture(textures[2], ...)

#2
instead checking if ur table have some value u can use value as keys and just index the table by this value

-- bad
local tbl = {"1", "ab", "cd",}
for k,v in pairs(tbl) if v == "cd" then ... end
-- good
local tbl = {["1"] = true, ["ab"] = true, ["cd"] = true,}
if tbl["cd"] then .. end
-- u can use objects same way
local tbl = {[getSpecificPlayer(...)] = true, [getSquare(...)] = true}
if tbl[getPlayer()] then ... end
if tbl[getPlayer():getCurrentSquare()] then ... end

P.S. u must ALWAYS AVOID loops when u can use table with value as key

#3
DON'T USE PAIRS for sequence table

local tbl = {"a", 5, "7", "sdfsdf", "bla"}
-- full shit
for k,v in pairs(tbl) do ... end
-- not full shit but still bad
for k,v in ipairs(tbl) do ... end
-- good
for k = 1, #tbl do v = tbl[k] ... end
#

#4
math.pow vs manual pow. spoiler: math.pow is slower for 3-4 times than x * x

-- worst
math.pow(x, 2)
-- better but still bad
x ^ 2
-- good
x * x

#5
table.insert vs manual by index. table.insert is slower for around 2 times

local tbl = {}
-- bad
table.insert(tbl, some_value)
-- good
tbl[i] = some_value
i = i + 1
-- if u doing insert only single time instead in loop then u can't use previous way so this way will be better than table.insert
tbl[#tbl + 1] = some_value

#6
the best ways to refill (reset) small/big tables

 -- for tables with len < 30-40
local tbl = {...}
for i = 1, #tbl do tbl[i] = nil end
... refill here
 -- for tables with len > 30-40
local tbl = {...}
local tbl = {}
... refill here

#7
concat

local to_concat = string.rep("1", 1234)
-- worst
local str = to_concat
str = str .. to_concat
str = str .. to_concat
str = str .. to_concat
str = str .. to_concat
-- worst #2
local str = to_concat
for _ = 1, 5 do str = str .. to_concat end
-- just bad
local tbl = {to_concat, to_concat, to_concat, to_concat, to_concat}
local str = table.concat(tbl)
-- just bad #2
local str = string.format("%s%s%s%s%s", to_concat, to_concat, to_concat, to_concat, to_concat)
-- good
local str = to_concat .. to_concat .. to_concat .. to_concat .. to_concat

#8
literal string vs cached

-- bad
cached = "bla"
function abc(a) return a == cached end
-- good
function abc(a) return a == "bla" end

#9
select return values

function abc() return 1,2 end
-- bad
local val = select(2, abc())
-- good
local _, val = abc()