Skip to content

Commit

Permalink
move history updates to background task
Browse files Browse the repository at this point in the history
should improve performance when updating auras somewhat
  • Loading branch information
emptyrivers committed Oct 27, 2024
1 parent 92facb1 commit 0d174b1
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions WeakAurasOptions/OptionsFrames/Update.lua
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ local function BuildUidMap(data, children, type)
for _, childUid in ipairs(children) do
self:EnsureUniqueIdOfUnmatched(childUid, IncProgress)
end
coroutine.yield(0.1, "ensure unique uids")
end

uidMap.InsertUnmatchedPhase1 = function(self, otherUidMap, otherUid, IncProgress)
Expand Down Expand Up @@ -795,10 +796,10 @@ local function BuildUidMap(data, children, type)
end
else
IncProgress()
coroutine.yield()
coroutine.yield(0.1)
end
end
coroutine.yield()
coroutine.yield(0.1)
end

for uid, otherList in pairs(matchToInsert) do
Expand All @@ -818,8 +819,9 @@ local function BuildUidMap(data, children, type)
otherUidMap:SetUIDMatch(otherUid, otherUid) -- Uids are the same!
self:SetUIDMatch(otherUid, otherUid)
IncProgress()
coroutine.yield()
coroutine.yield(0.1)
end
coroutine.yield(0.1)
end

if otherList.after then
Expand All @@ -833,11 +835,12 @@ local function BuildUidMap(data, children, type)
otherUidMap:SetUIDMatch(otherUid, otherUid) -- Uids are the same!
self:SetUIDMatch(otherUid, otherUid)
IncProgress()
coroutine.yield()
coroutine.yield(0.1)
end
coroutine.yield(0.1)
end
end
coroutine.yield()
coroutine.yield(0.1)
end

for _, otherUid in ipairs(waitingForMatch) do
Expand All @@ -857,7 +860,7 @@ local function BuildUidMap(data, children, type)
otherUidMap:SetUIDMatch(otherUid, otherUid) -- Uids are the same!
self:SetUIDMatch(otherUid, otherUid)
IncProgress()
coroutine.yield()
coroutine.yield(0.1)
end

return #waitingForMatch > 0
Expand Down Expand Up @@ -1632,37 +1635,46 @@ local methods = {
self.closeButton:SetEnabled(false)
self.viewCodeButton:SetEnabled(false)
OptionsPrivate.Private.SetImporting(true)

coroutine.yield(10, "init")
-- Adjust UI
self:ReleaseChildren()
self:AddBasicInformationWidgets(pendingData.data, pendingData.sender)
self:AddProgressWidgets()

---@type {uid: uid, data: auraData, source: string}[]
local copies = {}
local pendingPickData

if userChoices.mode == "import" then
coroutine.yield(0.1, "start import")
self:InitializeProgress(2 * (#pendingData.children + 1))

EnsureUniqueUid(pendingData.data)
coroutine.yield(0.1, "ensure unique uids")
for i, child in ipairs(pendingData.children) do
EnsureUniqueUid(child)
coroutine.yield(0.1, "ensure unique uids")
end

coroutine.yield(1, "build uid map")
local uidMap = BuildUidMap(pendingData.data, pendingData.children, "new")

local phase2Order = {}
coroutine.yield(1, "start phase 1")
self:ImportPhase1(uidMap, uidMap:GetRootUID(), phase2Order)
self:ImportPhase2(uidMap, phase2Order)
coroutine.yield(1, "start phase 2")
self:ImportPhase2(uidMap, phase2Order, copies)

pendingPickData = {
id = uidMap:GetIdFor(uidMap:GetRootUID())
}
if #pendingData.children > 0 then
pendingPickData.tabToShow = "group"
end

coroutine.yield(1, "update ui")
OptionsPrivate.SortDisplayButtons()
elseif userChoices.mode == "update" then
coroutine.yield(0.1, "start update")
local onePhaseProgress = matchInfo.oldUidMap:GetTotalCount() + matchInfo.newUidMap:GetTotalCount()
local IncProgress = function() self:IncProgress() end

Expand All @@ -1675,9 +1687,9 @@ local methods = {
-- On update, we won't match A_new to A_old, because A_old is outside the matched parent group
-- Thus on import A_new needs to get its own uid
-- On next import, the auras uids won't match either, there's not much we can do about that.
coroutine.yield(0.1, "ensure unique uids")
matchInfo.newUidMap:EnsureUniqueIdOfUnmatched(nil, IncProgress)
self:SetMinimumProgress(1 * onePhaseProgress)
coroutine.yield()

local removeOldGroups = matchInfo.activeCategories.arrangement and userChoices.activeCategories.arrangement
if userChoices.activeCategories.oldchildren or removeOldGroups then
Expand Down Expand Up @@ -1707,6 +1719,7 @@ local methods = {
if not userChoices.activeCategories.oldchildren then
-- Keep old children
matchInfo.newUidMap:InsertUnmatchedFrom(matchInfo.oldUidMap, IncProgress)
coroutine.yield(0.1, "keep old children done")
end

self:SetMinimumProgress(4 * onePhaseProgress)
Expand Down Expand Up @@ -1773,17 +1786,26 @@ local methods = {
end
end

coroutine.yield(10, "prep done")
local phase2Order = {}
self:UpdatePhase1(structureUidMap, structureUidMap:GetRootUID(), GetPhase1Data, phase2Order)
self:SetMinimumProgress(16 * onePhaseProgress)
coroutine.yield(10, " phase 1 done")
self:UpdatePhase2(structureUidMap, GetPhase2Data, phase2Order, copies)

self:UpdatePhase2(structureUidMap, GetPhase2Data, phase2Order)
self:SetMinimumProgress(26 * onePhaseProgress)
coroutine.yield(10, " phase 2 done")

local renameTries = 0
while(self:RenameAuras(targetNames)) do
-- Try renaming again and again...
renameTries = renameTries + 1
if renameTries % 10 == 0 then
coroutine.yield(0.1, "renaming auras")
end
end
self:SetMaxProgress()
coroutine.yield()
coroutine.yield(0.1, "renaming auras done")

pendingPickData = {
id = OptionsPrivate.Private.GetDataByUID(matchInfo.oldUidMap:GetRootUID()).id
Expand All @@ -1794,7 +1816,7 @@ local methods = {

OptionsPrivate.SortDisplayButtons()
end

coroutine.yield(0.1, "winding down")
OptionsPrivate.Private.SetImporting(false)
self.viewCodeButton:SetEnabled(true)
self.importButton:SetEnabled(true)
Expand All @@ -1807,6 +1829,12 @@ local methods = {
OptionsPrivate.ClearPicks()
WeakAuras.PickDisplay(pendingPickData.id, pendingPickData.tabToShow)
end
OptionsPrivate.Private.Threads:Add("history_update", coroutine.create(function()
for _, copy in ipairs(copies) do
OptionsPrivate.Private.SetHistory(copy.uid, copy.data, copy.source)
coroutine.yield()
end
end), "background")
end,
-- This ensures that the id that we are adding is either
-- same for existing uids
Expand Down Expand Up @@ -1930,7 +1958,7 @@ local methods = {
end
end
self:IncProgress()
coroutine.yield()
coroutine.yield(0.1, "remove unmatched old")
return false
end,
RemoveUnmatchedNew = function(self, uidMap, uid, otherMap, removeAuras, removeGroups)
Expand Down Expand Up @@ -1968,7 +1996,7 @@ local methods = {
end
end
self:IncProgress()
coroutine.yield()
coroutine.yield(0.1, "remove unmatched new")
return false
end,
UpdatePhase1 = function(self, structureUidMap, uid, GetPhase1Data, phase2Order)
Expand All @@ -1982,7 +2010,7 @@ local methods = {
WeakAuras.Add(data)
WeakAuras.NewDisplayButton(data, true)
self:IncProgress10()
coroutine.yield()
coroutine.yield(1, "adding phase 1 data")

local children = structureUidMap:GetChildren(uid)
local parentIsDynamicGroup = data.regionType == "dynamicgroup"
Expand All @@ -1992,14 +2020,14 @@ local methods = {
structureUidMap:SetParentIsDynamicGroup(childUid, parentIsDynamicGroup)
end
end,
UpdatePhase2 = function(self, structureUidMap, GetPhase2Data, phase2Order)
UpdatePhase2 = function(self, structureUidMap, GetPhase2Data, phase2Order, copies)
for i = #phase2Order, 1, -1 do
local uid = phase2Order[i]
local data = GetPhase2Data(uid)
data.preferToUpdate = true
data.authorMode = nil
WeakAuras.Add(data)
OptionsPrivate.Private.SetHistory(data.uid, data, "import")
table.insert(copies, {uid = uid, data = CopyTable(data), source = "update"})
local button = OptionsPrivate.GetDisplayButton(data.id)
button:SetData(data)
if (data.parent) then
Expand Down Expand Up @@ -2053,14 +2081,14 @@ local methods = {
uidMap:SetParentIsDynamicGroup(childUid, parentIsDynamicGroup)
end
end,
ImportPhase2 = function(self, uidMap, phase2Order)
ImportPhase2 = function(self, uidMap, phase2Order, copies)
for i = #phase2Order, 1, -1 do
local uid = phase2Order[i]
local data = uidMap:GetPhase2Data(uid)
data.preferToUpdate = false
data.authorMode = nil
WeakAuras.Add(data)
OptionsPrivate.Private.SetHistory(data.uid, data, "import")
table.insert(copies, {uid = uid, data = CopyTable(data), source = "import"})

local button = OptionsPrivate.GetDisplayButton(data.id)
button:SetData(data)
Expand Down

0 comments on commit 0d174b1

Please sign in to comment.