Implement lazy rebuild for itemList and enhance peripheral caching for improved performance

This commit is contained in:
MayaTheShy
2026-03-16 18:56:34 -04:00
parent 10edbe8fff
commit 594d8c954c

View File

@@ -597,6 +597,7 @@ local activeAlerts = {}
local cache = {
catalogue = {}, -- itemName -> { {chest=name, total=N}, ... }
itemList = {}, -- sorted list of { name, total }
itemListDirty = false, -- lazy rebuild flag for itemList
grandTotal = 0,
chestCount = 0,
totalSlots = 0,
@@ -668,36 +669,66 @@ local function adjustCache(itemName, chestName, delta)
end
end
-- 2) Rebuild itemList from catalogue (lightweight — just totals)
-- 2) Mark itemList as needing rebuild (deferred until actually read)
cache.itemListDirty = true
-- 3) Update grandTotal incrementally
cache.grandTotal = cache.grandTotal + delta
end
--- Rebuild itemList from catalogue if dirty (lazy rebuild)
local function ensureItemList()
if not cache.itemListDirty then return end
local itemList = {}
local grandTotal = 0
for name, sources in pairs(cat) do
for name, sources in pairs(cache.catalogue) do
local total = 0
for _, s in ipairs(sources) do total = total + s.total end
grandTotal = grandTotal + total
table.insert(itemList, { name = name, total = total })
end
table.sort(itemList, function(a, b) return a.total > b.total end)
cache.itemList = itemList
cache.grandTotal = grandTotal
cache.itemListDirty = false
end
-------------------------------------------------
-- Inventory helpers
-- Inventory helpers (cached peripheral lists)
-------------------------------------------------
local PERIPHERAL_CACHE_TTL = 5 -- seconds
local cachedChests = nil
local cachedChestsTime = 0
local cachedFurnaces = nil
local cachedFurnacesTime = 0
local function invalidatePeripheralCaches()
cachedChests = nil
cachedFurnaces = nil
end
local function getChests()
local now = os.clock()
if cachedChests and (now - cachedChestsTime) < PERIPHERAL_CACHE_TTL then
return cachedChests
end
local chests = {}
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "minecraft:chest" then
table.insert(chests, name)
end
end
cachedChests = chests
cachedChestsTime = now
return chests
end
local function getFurnaces()
local now = os.clock()
if cachedFurnaces and (now - cachedFurnacesTime) < PERIPHERAL_CACHE_TTL then
return cachedFurnaces
end
local furnaces = {}
for _, ftype in ipairs(FURNACE_TYPES) do
for _, name in ipairs(peripheral.getNames()) do
@@ -706,6 +737,8 @@ local function getFurnaces()
end
end
end
cachedFurnaces = furnaces
cachedFurnacesTime = now
return furnaces
end
@@ -996,6 +1029,7 @@ end
-- Get items filtered by search query
local function getFilteredItems()
ensureItemList()
local filtered = {}
for _, item in ipairs(cache.itemList) do
if searchQuery == "" then
@@ -1358,6 +1392,7 @@ local function drawDashboard()
end
-- ===== Footer (h-1) =====
ensureItemList()
monFill(h - 1, colors.gray)
local footerLeft = string.format(" Total: %d items | %d types ",
cache.grandTotal, #cache.itemList)
@@ -2821,6 +2856,7 @@ end
local function broadcastState()
if not networkModem then return end
ensureItemList()
local state = {
type = "state",
cache = {