Possible performance improvements
goatus2 created this issue 02/20/2017: https://wow.curseforge.com/projects/can-i-mog-it/issues/131
local function _GetAppearances()
...
-- Add new appearances learned.
for appearanceID, collected in <strong>pairsByKeys(appearancesTable)</strong> do
...
end
-- Remove appearances that are no longer learned.
for appearanceID, sources in <strong>pairsByKeys(removeAppearancesTable</strong>) do
...
end
...
end
From what I can tell after short look at the code (prompted by some performance complaints in the comments) I don't think you use pairsByKeys as you are supposed to. It returns iterator to the sorted table, but using it is completely pointless if you get new one every time you call _GetApperances (which is called in onUpdate until you are done) It's even worse because pairsByKeys copies and sorts table every time it is called so not only you use a lot of cpu every time (sorting) but also create garbage.
Instead you should cache this iterator between _GetApperance calls, this way each time you will continue traversing table from where you left (I assume that both apperancesTable and removeApperancesTable, don't get modified during initial scanning from outside)
So it would look like:
local apperancesIter, removeIter = nil,nil
local function _GetAppearances()
...
if apperancesIter == nil then apperancesIter = pairsByKeys(appearancesTable) end
-- Add new appearances learned.
for appearanceID, collected in apperancesIter do
...
end
if removeIter == nil then removeIter = pairsByKeys(removeAppearancesTable) end
-- Remove appearances that are no longer learned.
for appearanceID, sources in removeIter do
...
end
...
end
This modification cuts cpu time of _ GetAppearances measured with GetFunctionCPUUsage(_GetAppearances, true) on almost by half (tested multiple times with relog every time) , so for example on my lvl68 char from 70 to 32.
Additionally in the first loop you have if buffer >= CanIMogIt.bufferMax then return end
but you never increase buffer in that loop so first loop is not throttled at all which as I assume from this code is not intended