Implement lazy rebuild for itemList and enhance peripheral caching for improved performance
This commit is contained in:
@@ -597,6 +597,7 @@ local activeAlerts = {}
|
|||||||
local cache = {
|
local cache = {
|
||||||
catalogue = {}, -- itemName -> { {chest=name, total=N}, ... }
|
catalogue = {}, -- itemName -> { {chest=name, total=N}, ... }
|
||||||
itemList = {}, -- sorted list of { name, total }
|
itemList = {}, -- sorted list of { name, total }
|
||||||
|
itemListDirty = false, -- lazy rebuild flag for itemList
|
||||||
grandTotal = 0,
|
grandTotal = 0,
|
||||||
chestCount = 0,
|
chestCount = 0,
|
||||||
totalSlots = 0,
|
totalSlots = 0,
|
||||||
@@ -668,36 +669,66 @@ local function adjustCache(itemName, chestName, delta)
|
|||||||
end
|
end
|
||||||
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 itemList = {}
|
||||||
local grandTotal = 0
|
local grandTotal = 0
|
||||||
for name, sources in pairs(cat) do
|
for name, sources in pairs(cache.catalogue) do
|
||||||
local total = 0
|
local total = 0
|
||||||
for _, s in ipairs(sources) do total = total + s.total end
|
for _, s in ipairs(sources) do total = total + s.total end
|
||||||
grandTotal = grandTotal + total
|
grandTotal = grandTotal + total
|
||||||
table.insert(itemList, { name = name, total = total })
|
table.insert(itemList, { name = name, total = total })
|
||||||
end
|
end
|
||||||
table.sort(itemList, function(a, b) return a.total > b.total end)
|
table.sort(itemList, function(a, b) return a.total > b.total end)
|
||||||
|
|
||||||
cache.itemList = itemList
|
cache.itemList = itemList
|
||||||
cache.grandTotal = grandTotal
|
cache.grandTotal = grandTotal
|
||||||
|
cache.itemListDirty = false
|
||||||
end
|
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 function getChests()
|
||||||
|
local now = os.clock()
|
||||||
|
if cachedChests and (now - cachedChestsTime) < PERIPHERAL_CACHE_TTL then
|
||||||
|
return cachedChests
|
||||||
|
end
|
||||||
local chests = {}
|
local chests = {}
|
||||||
for _, name in ipairs(peripheral.getNames()) do
|
for _, name in ipairs(peripheral.getNames()) do
|
||||||
if peripheral.getType(name) == "minecraft:chest" then
|
if peripheral.getType(name) == "minecraft:chest" then
|
||||||
table.insert(chests, name)
|
table.insert(chests, name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
cachedChests = chests
|
||||||
|
cachedChestsTime = now
|
||||||
return chests
|
return chests
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getFurnaces()
|
local function getFurnaces()
|
||||||
|
local now = os.clock()
|
||||||
|
if cachedFurnaces and (now - cachedFurnacesTime) < PERIPHERAL_CACHE_TTL then
|
||||||
|
return cachedFurnaces
|
||||||
|
end
|
||||||
local furnaces = {}
|
local furnaces = {}
|
||||||
for _, ftype in ipairs(FURNACE_TYPES) do
|
for _, ftype in ipairs(FURNACE_TYPES) do
|
||||||
for _, name in ipairs(peripheral.getNames()) do
|
for _, name in ipairs(peripheral.getNames()) do
|
||||||
@@ -706,6 +737,8 @@ local function getFurnaces()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
cachedFurnaces = furnaces
|
||||||
|
cachedFurnacesTime = now
|
||||||
return furnaces
|
return furnaces
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -996,6 +1029,7 @@ end
|
|||||||
|
|
||||||
-- Get items filtered by search query
|
-- Get items filtered by search query
|
||||||
local function getFilteredItems()
|
local function getFilteredItems()
|
||||||
|
ensureItemList()
|
||||||
local filtered = {}
|
local filtered = {}
|
||||||
for _, item in ipairs(cache.itemList) do
|
for _, item in ipairs(cache.itemList) do
|
||||||
if searchQuery == "" then
|
if searchQuery == "" then
|
||||||
@@ -1358,6 +1392,7 @@ local function drawDashboard()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- ===== Footer (h-1) =====
|
-- ===== Footer (h-1) =====
|
||||||
|
ensureItemList()
|
||||||
monFill(h - 1, colors.gray)
|
monFill(h - 1, colors.gray)
|
||||||
local footerLeft = string.format(" Total: %d items | %d types ",
|
local footerLeft = string.format(" Total: %d items | %d types ",
|
||||||
cache.grandTotal, #cache.itemList)
|
cache.grandTotal, #cache.itemList)
|
||||||
@@ -2821,6 +2856,7 @@ end
|
|||||||
|
|
||||||
local function broadcastState()
|
local function broadcastState()
|
||||||
if not networkModem then return end
|
if not networkModem then return end
|
||||||
|
ensureItemList()
|
||||||
local state = {
|
local state = {
|
||||||
type = "state",
|
type = "state",
|
||||||
cache = {
|
cache = {
|
||||||
|
|||||||
Reference in New Issue
Block a user