Skip to content

Commit e1c4040

Browse files
Cidanclaude
andcommitted
Revert "reworked search caching so it's less intensive when looting items"
This reverts commit 0788a31. The incremental search cache optimization is causing issues with search category assignments. Reverting to the original full-rebuild approach until the cache implementation can be fixed. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 41c4e61 commit e1c4040

File tree

2 files changed

+20
-265
lines changed

2 files changed

+20
-265
lines changed

PERFORMANCE_OPTIMIZATION.md

Lines changed: 0 additions & 191 deletions
This file was deleted.

data/items.lua

Lines changed: 20 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,7 @@ function items:LoadItems(ctx, kind, dataCache, equipmentCache, callback)
776776
if ctx:GetBool("wipe") then
777777
self:WipeSlotInfo(kind)
778778
end
779-
-- Note: We no longer wipe the search cache here. Instead, RefreshSearchCache
780-
-- will incrementally update only changed items (or rebuild all items on wipe/redraw).
779+
self:WipeSearchCache(kind)
781780

782781
if equipmentCache then
783782
self.equipmentCache = equipmentCache
@@ -896,7 +895,7 @@ function items:LoadItems(ctx, kind, dataCache, equipmentCache, callback)
896895
end
897896

898897
-- Refresh the search cache.
899-
self:RefreshSearchCache(kind, ctx)
898+
self:RefreshSearchCache(kind)
900899

901900
-- Get the categories for each item.
902901
for _, currentItem in pairs(slotInfo:GetCurrentItems()) do
@@ -926,89 +925,36 @@ function items:WipeSearchCache(kind)
926925
end
927926

928927
---@param kind BagKind
929-
---@param item ItemData
930-
function items:RemoveItemFromSearchCache(kind, item)
931-
if item and item.slotkey then
932-
self.searchCache[kind][item.slotkey] = nil
933-
end
934-
end
935-
936-
---@param kind BagKind
937-
---@param item ItemData
938-
function items:UpdateSearchCacheForItem(kind, item)
939-
if not item or item.isItemEmpty then
940-
return
941-
end
942-
928+
function items:RefreshSearchCache(kind)
929+
self:WipeSearchCache(kind)
943930
local categoryTable = categories:GetSortedSearchCategories()
944931
for _, categoryFilter in ipairs(categoryTable) do
945932
if categoryFilter.enabled[kind] then
946-
-- Use search:Find() to check if this specific item matches the query
947-
-- This is much faster than search:Search() which evaluates all items
948-
local match = search:Find(categoryFilter.searchCategory.query, item)
933+
local results = search:Search(categoryFilter.searchCategory.query)
949934
local groupBy = categoryFilter.searchCategory.groupBy or const.SEARCH_CATEGORY_GROUP_BY.NONE
950935

951-
if match then
952-
local categoryName = categoryFilter.name
953-
954-
-- Apply groupBy logic to generate dynamic category name
955-
if groupBy ~= const.SEARCH_CATEGORY_GROUP_BY.NONE then
956-
local suffix = self:GetGroupBySuffix(item, groupBy)
957-
if suffix and suffix ~= "" then
958-
categoryName = categoryFilter.name .. " - " .. suffix
936+
for slotkey, match in pairs(results) do
937+
if match then
938+
local categoryName = categoryFilter.name
939+
940+
-- Apply groupBy logic to generate dynamic category name
941+
if groupBy ~= const.SEARCH_CATEGORY_GROUP_BY.NONE then
942+
local itemData = self:GetItemDataFromSlotKey(slotkey)
943+
if itemData and not itemData.isItemEmpty then
944+
local suffix = self:GetGroupBySuffix(itemData, groupBy)
945+
if suffix and suffix ~= "" then
946+
categoryName = categoryFilter.name .. " - " .. suffix
947+
end
948+
end
959949
end
960-
end
961950

962-
self.searchCache[kind][item.slotkey] = categoryName
963-
-- Once we find a matching category, use it and stop searching
964-
break
951+
self.searchCache[kind][slotkey] = categoryName
952+
end
965953
end
966954
end
967955
end
968956
end
969957

970-
---@param kind BagKind
971-
---@param ctx Context
972-
function items:RefreshSearchCache(kind, ctx)
973-
debug:StartProfile("RefreshSearchCache")
974-
975-
local slotInfo = self.slotInfo[kind]
976-
local added, removed, changed = slotInfo:GetChangeset()
977-
978-
-- For full refreshes (wipe/redraw), rebuild the entire cache
979-
if ctx:GetBool("wipe") or ctx:GetBool("redraw") then
980-
debug:Log("RefreshSearchCache", "Full refresh: rebuilding entire cache")
981-
self:WipeSearchCache(kind)
982-
for _, item in pairs(slotInfo:GetCurrentItems()) do
983-
self:UpdateSearchCacheForItem(kind, item)
984-
end
985-
else
986-
-- Performance optimization: Only update items that have actually changed.
987-
-- This reduces complexity from O(n × m) to O(k × m) where k is the number
988-
-- of changed items (typically 1-5 instead of 150+).
989-
debug:Log("RefreshSearchCache", "Incremental update:", #added, "added,", #removed, "removed,", #changed, "changed")
990-
991-
-- Remove cache entries for removed items
992-
for _, item in pairs(removed) do
993-
self:RemoveItemFromSearchCache(kind, item)
994-
end
995-
996-
-- Update cache entries for added items
997-
for _, item in pairs(added) do
998-
self:UpdateSearchCacheForItem(kind, item)
999-
end
1000-
1001-
-- Update cache entries for changed items (remove old, add new)
1002-
for _, item in pairs(changed) do
1003-
-- Note: changed items are still in the current items table with updated data
1004-
self:RemoveItemFromSearchCache(kind, item)
1005-
self:UpdateSearchCacheForItem(kind, item)
1006-
end
1007-
end
1008-
1009-
debug:EndProfile("RefreshSearchCache")
1010-
end
1011-
1012958
---@param data ItemData
1013959
---@param groupBy number
1014960
---@return string|nil

0 commit comments

Comments
 (0)