From a340c83e1aaabe4ac77608e693dafbb890f4147a Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Wed, 27 Apr 2022 17:07:24 +1000 Subject: [PATCH] perf(zones): assign inside count and index to local Reduce number of times the inside table needs to be indexed or have length checked. Also assign debug zones to table without having to be inside the zone. --- imports/zones/client.lua | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/imports/zones/client.lua b/imports/zones/client.lua index cca6e8d..ea45c55 100644 --- a/imports/zones/client.lua +++ b/imports/zones/client.lua @@ -21,20 +21,23 @@ local function debug(self) end local inside = {} +local insideCount local tick CreateThread(function() while true do - local coords = GetEntityCoords(cache.ped) Wait(300) + local coords = GetEntityCoords(cache.ped) table.wipe(inside) + insideCount = 0 for _, zone in pairs(zones) do local contains = zone.polygon:contains(coords, zone.thickness) if contains then if zone.inside then - inside[#inside + 1] = zone + insideCount += 1 + inside[insideCount] = zone end if zone.onEnter and not zone.insideZone then @@ -46,21 +49,29 @@ CreateThread(function() if zone.onExit then zone:onExit() end + elseif zone.debug then + insideCount += 1 + inside[insideCount] = zone end end if not tick then - if #inside > 0 then + if insideCount > 0 then tick = SetInterval(function() - for i = 1, #inside do - inside[i]:inside() - if inside[i].debug then - inside[i]:debug() + for i = 1, insideCount do + local zone = inside[i] + + if zone.insideZone then + zone:inside() + end + + if zone.debug then + zone:debug() end end end) end - elseif #inside == 0 then + elseif insideCount == 0 then tick = ClearInterval(tick) end end