From ec77a57f42794df87fb2d8afa41c373324db37f8 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Fri, 25 Oct 2024 17:56:51 +0800 Subject: [PATCH] Stop reusing holes in areas IDs This eliminates the need of iterating the whole list for every protection operations. Note that the highest index isn't cached, i.e. the first or few (if there are many holes) operations would still suffer from the lag. --- internal.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal.lua b/internal.lua index f018084..f23fb3c 100644 --- a/internal.lua +++ b/internal.lua @@ -83,19 +83,19 @@ function areas:populateStore() self.store_ids = store_ids end --- Finds the first usable index in a table --- Eg: {[1]=false,[4]=true} -> 2 -local function findFirstUnusedIndex(t) - local i = 0 - repeat i = i + 1 - until t[i] == nil - return i +-- Guarentees returning an unused index in areas.areas +local index_cache = 0 +local function findFirstUnusedIndex() + local t = areas.areas + repeat index_cache = index_cache + 1 + until t[index_cache] == nil + return index_cache end --- Add an area. -- @return The new area's ID. function areas:add(owner, name, pos1, pos2, parent) - local id = findFirstUnusedIndex(self.areas) + local id = findFirstUnusedIndex() self.areas[id] = { name = name, pos1 = pos1,