@@ -3874,12 +3874,33 @@ local function cloneSpecForJewelComparison(spec)
38743874 return specCopy
38753875end
38763876
3877- local function buildSpecForJewelComparison (itemsTab , compareSlot , replacementItem )
3877+ local function buildSpecForJewelComparison (itemsTab , compareSlot , replacementItem , useCache )
38783878 local tempItemId
3879+ local replacementItemId = replacementItem and replacementItem .id
3880+ local replacementItemIsStored = replacementItemId and itemsTab .items [replacementItemId ] == replacementItem
3881+ local canCache = useCache and (not replacementItem or replacementItemIsStored )
3882+ local cacheKey
3883+ local cache
3884+ if canCache then
3885+ local outputRevision = itemsTab .build and itemsTab .build .outputRevision or 0
3886+ cache = itemsTab .targetedJewelComparisonSpecCache
3887+ if not cache or cache .outputRevision ~= outputRevision then
3888+ cache = {
3889+ outputRevision = outputRevision ,
3890+ specs = { },
3891+ }
3892+ itemsTab .targetedJewelComparisonSpecCache = cache
3893+ end
3894+ cacheKey = tostring (compareSlot .nodeId ) .. " :" .. tostring (replacementItemId or " " )
3895+ if cache .specs [cacheKey ] then
3896+ return cache .specs [cacheKey ]
3897+ end
3898+ end
3899+
38793900 local spec = cloneSpecForJewelComparison (itemsTab .build .spec )
38803901 if replacementItem then
3881- if replacementItem . id and itemsTab . items [ replacementItem . id ] == replacementItem then
3882- spec .jewels [compareSlot .nodeId ] = replacementItem . id
3902+ if replacementItemIsStored then
3903+ spec .jewels [compareSlot .nodeId ] = replacementItemId
38833904 else
38843905 tempItemId = - 1
38853906 while itemsTab .items [tempItemId ] do
@@ -3901,6 +3922,9 @@ local function buildSpecForJewelComparison(itemsTab, compareSlot, replacementIte
39013922 if not ok then
39023923 error (err , 0 )
39033924 end
3925+ if cacheKey then
3926+ cache .specs [cacheKey ] = spec
3927+ end
39043928 return spec
39053929end
39063930
@@ -4537,18 +4561,18 @@ function ItemsTabClass:AddItemTooltip(tooltip, item, slot, dbMode, maxWidth)
45374561
45384562 tooltip :AddLine (14 , colorCodes .TIP .. " Tip: Press Ctrl+D to disable the display of stat differences." )
45394563
4540- local function getReplacedItemAndOutput (compareSlot )
4541- local selItem = self .items [compareSlot .selItemId ]
4564+ local function getReplacedItemAndOutput (compareSlot , selItem , useJewelComparisonSpecCache )
4565+ selItem = selItem or self .items [compareSlot .selItemId ]
45424566 local override = { repSlotName = compareSlot .slotName , repItem = item ~= selItem and item or nil }
45434567 if compareSlot .nodeId and (itemChangesPassiveTreeRadius (selItem ) or itemChangesPassiveTreeRadius (item )) then
4544- override .spec = buildSpecForJewelComparison (self , compareSlot , override .repItem )
4568+ override .spec = buildSpecForJewelComparison (self , compareSlot , override .repItem , useJewelComparisonSpecCache )
45454569 end
45464570 local output = calcFunc (override )
45474571 return selItem , output
45484572 end
4549- local function addCompareForSlot (compareSlot , selItem , output )
4573+ local function addCompareForSlot (compareSlot , selItem , output , useJewelComparisonSpecCache )
45504574 if not selItem or not output then
4551- selItem , output = getReplacedItemAndOutput (compareSlot )
4575+ selItem , output = getReplacedItemAndOutput (compareSlot , nil , useJewelComparisonSpecCache )
45524576 end
45534577 local header
45544578 if item == selItem then
@@ -4561,29 +4585,38 @@ function ItemsTabClass:AddItemTooltip(tooltip, item, slot, dbMode, maxWidth)
45614585
45624586 -- if we have a specific slot to compare to, and the user has "Show
45634587 -- tooltips only for affected slots" checked, we can just compare that
4564- -- one slot
4588+ -- one slot.
4589+ local compareOnlySlot = type (slot ) ~= " string" and slot or self .slots [slot ]
45654590 if main .slotOnlyTooltips and slot then
4566- slot = type (slot ) ~= " string" and slot or self .slots [slot ]
4567- if slot then addCompareForSlot (slot ) end
4591+ if compareOnlySlot then addCompareForSlot (compareOnlySlot , nil , nil , true ) end
45684592 return
45694593 end
45704594
4571-
4572- local slots = {}
45734595 local isUnique = item .rarity == " UNIQUE" or item .rarity == " RELIC"
45744596 local currentSameUniqueCount = 0
4597+ local slotCandidates = {}
45754598 for _ , compareSlot in ipairs (compareSlots ) do
4576- local selItem , output = getReplacedItemAndOutput ( compareSlot )
4599+ local selItem = self . items [ compareSlot . selItemId ]
45774600 local isSameUnique = isUnique and selItem and item .name == selItem .name
45784601 if isUnique and isSameUnique and item .limit then
45794602 currentSameUniqueCount = currentSameUniqueCount + 1
45804603 end
4581- table.insert (slots ,
4582- { selItem = selItem , output = output , compareSlot = compareSlot , isSameUnique = isSameUnique })
4604+ table.insert (slotCandidates ,
4605+ { selItem = selItem , compareSlot = compareSlot , isSameUnique = isSameUnique })
4606+ end
4607+ local isLimitedUniqueAtLimit = (isUnique and item .limit and currentSameUniqueCount == item .limit ) or false
4608+
4609+ local slots = {}
4610+ for _ , slotEntry in ipairs (slotCandidates ) do
4611+ if not isLimitedUniqueAtLimit or slotEntry .isSameUnique then
4612+ local _ , output = getReplacedItemAndOutput (slotEntry .compareSlot , slotEntry .selItem )
4613+ slotEntry .output = output
4614+ table.insert (slots , slotEntry )
4615+ end
45834616 end
45844617
45854618 -- limited uniques: only compare to slots with the same item if more don't fit
4586- if currentSameUniqueCount == item . limit then
4619+ if isLimitedUniqueAtLimit then
45874620 for _ , slotEntry in ipairs (slots ) do
45884621 if slotEntry .isSameUnique then
45894622 addCompareForSlot (slotEntry .compareSlot , slotEntry .selItem , slotEntry .output )
0 commit comments