Skip to content

Commit

Permalink
Use reusable compound key for StatModCache entries
Browse files Browse the repository at this point in the history
  • Loading branch information
raethkcj committed Nov 28, 2024
1 parent d2f1f62 commit ed3a678
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
22 changes: 11 additions & 11 deletions RatingBuster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ RatingBuster.date = ("$Date: 2008-07-22 15:35:19 +0800 (星期二, 22 七月 200
-- Cache --
-----------
local cache = setmetatable({}, {
__index = function(t, profileSpec)
t[profileSpec] = setmetatable({}, { __mode = "kv" })
return t[profileSpec]
__index = function(t, k)
t[k] = setmetatable({}, { __mode = "kv" })
return t[k]
end,
})

Expand Down Expand Up @@ -2114,17 +2114,17 @@ end

function RatingBuster:ProcessLine(text, link, color, statModContext)
-- Get data from cache if available
local profileSpec = statModContext.profile .. statModContext.spec
local cacheKey = statModContext:CacheKey()
local cacheID = text .. statModContext.level
local cacheText = cache[profileSpec][cacheID]
local cacheText = cache[cacheKey][cacheID]
if cacheText then
if cacheText ~= text then
return cacheText
end
elseif EmptySocketLookup[text] and db.profile[EmptySocketLookup[text]].gemText then -- Replace empty sockets with gem text
local gemText = db.profile[EmptySocketLookup[text]].gemText
text = RatingBuster:ProcessLine(gemText, link, color, statModContext)
cache[profileSpec][cacheID] = text
cache[cacheKey][cacheID] = text
return text
elseif text:find("%d") then -- do nothing if we don't find a number
-- Temporarily replace exclusions
Expand Down Expand Up @@ -2152,11 +2152,11 @@ function RatingBuster:ProcessLine(text, link, color, statModContext)
text = text:gsub(replacement, exclusion)
end
end
cache[profileSpec][cacheID] = text
cache[cacheKey][cacheID] = text
-- SetText
return text
else
cache[profileSpec][cacheID] = text
cache[cacheKey][cacheID] = text
return text
end
end
Expand Down Expand Up @@ -3831,8 +3831,8 @@ function RatingBuster:StatSummary(tooltip, link, statModContext)
local numLines = StatLogic:GetItemTooltipNumLines(link)

-- Check Cache
local profileSpec = statModContext.profile .. statModContext.spec
local cached = cache[profileSpec][id]
local cacheKey = statModContext:CacheKey()
local cached = cache[cacheKey][id]
if cached and cached.numLines == numLines then
if table.maxn(cached) == 0 then return end
WriteSummary(tooltip, cached)
Expand Down Expand Up @@ -4014,7 +4014,7 @@ function RatingBuster:StatSummary(tooltip, link, statModContext)
tsort(output, sumSortAlphaComp)
end
-- Write cache
cache[profileSpec][id] = output
cache[cacheKey][id] = output
if table.maxn(output) == 0 then return end
WriteSummary(tooltip, output)
end
Expand Down
17 changes: 13 additions & 4 deletions libs/StatLogic/StatLogic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1346,13 +1346,22 @@ do
-- Helper object for repeatedly calling GetStatMod for varying StatMods but consistent other parameters
---@class StatModContext : StatModContextArgs
local StatModContext = {}
StatModContext.__index = StatModContext

---@param statMod string|Stat
---@return number
function StatModContext:__call(statMod)
return StatLogic:GetStatMod(statMod, self)
end

---@return string
function StatModContext:CacheKey()
return table.concat({
self.profile,
self.spec,
self.level,
})
end

---@param context? StatModContextArgs
---@return StatModContext
Expand Down Expand Up @@ -1385,17 +1394,17 @@ do
if not context then
context = self:NewStatModContext()
end
local profileSpec = context.profile .. context.spec
local cacheKey = context:CacheKey()

if context.level == UnitLevel("player") and not next(context.overrideStats) then
value = StatModCache[statMod][profileSpec]
value = StatModCache[statMod][cacheKey]
end

if not value then
wipe(ExclusiveGroupCache)
local statModInfo = StatLogic.StatModInfo[statMod]
if not statModInfo then
StatModCache[statMod][profileSpec] = 0
StatModCache[statMod][cacheKey] = 0
return 0
end
value = statModInfo.initialValue
Expand All @@ -1409,7 +1418,7 @@ do

value = value + statModInfo.finalAdjust
if context.level == UnitLevel("player") then
StatModCache[statMod][profileSpec] = value
StatModCache[statMod][cacheKey] = value
end
end

Expand Down

0 comments on commit ed3a678

Please sign in to comment.