perf: avoid repeated charge tooltip scans

This commit is contained in:
github-actions[bot]
2026-09-06 07:00:01 +00:00
parent cb3019568c
commit e9c3a460fc
2 changed files with 66 additions and 139 deletions
@@ -1,85 +0,0 @@
name: Optimize charge cache
on:
push:
branches:
- refactor/consolidate-patch-layers
permissions:
contents: write
jobs:
optimize:
if: github.actor != 'github-actions[bot]'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: refactor/consolidate-patch-layers
fetch-depth: 0
- name: Optimize explicit charge detection cache
shell: python
run: |
from pathlib import Path
import re
path = Path('Core/ItemDetectionClassicAPI.lua')
text = path.read_text(encoding='utf-8')
old = '''local detectionCache = {}\nlocal cacheHits = 0\nlocal cacheMisses = 0\n'''
new = '''local detectionCache = {}\nlocal cacheHits = 0\nlocal cacheMisses = 0\n\n-- Explicit charge overlays are cached per live slot. Negative results are kept\n-- across ordinary BAG_UPDATE events so normal stacks do not trigger a tooltip\n-- scan every time their count changes.\nlocal chargesCache = {}\nlocal chargeCapableLinks = {}\n'''
if old not in text:
raise SystemExit('Top cache declaration block not found')
text = text.replace(old, new, 1)
old = ''' local numLines = tooltip:NumLines() or 0\n local complete = numLines >= 2\n\n for i = 1, numLines do\n'''
new = ''' local numLines = tooltip:NumLines() or 0\n local complete = numLines >= 2\n local explicitCharges = nil\n\n for i = 1, numLines do\n'''
if old not in text:
raise SystemExit('ScanProperties numLines block not found')
text = text.replace(old, new, 1)
old = ''' local leftLower = leftText ~= "" and string.lower(leftText) or ""\n\n local lr, lg, lb = 1, 1, 1\n'''
new = ''' local leftLower = leftText ~= "" and string.lower(leftText) or ""\n\n -- Reuse this same tooltip pass for charge detection. This avoids a\n -- second synchronous tooltip scan later when ItemButton asks for xN.\n local _, _, chargeCount = string.find(leftLower, "^(%d+) charges?$")\n if chargeCount then\n explicitCharges = tonumber(chargeCount)\n end\n\n local lr, lg, lb = 1, 1, 1\n'''
if old not in text:
raise SystemExit('ScanProperties line text block not found')
text = text.replace(old, new, 1)
old = ''' if result.isPermanentEnchant then\n result.isQuestItem = false\n result.isQuestStarter = false\n result.isQuestUsable = false\n end\n\n return result, complete\nend\n'''
new = ''' if result.isPermanentEnchant then\n result.isQuestItem = false\n result.isQuestStarter = false\n result.isQuestUsable = false\n end\n\n -- Seed the per-slot charge cache from the property scan we already paid\n -- for. Store the item link with the result so slot swaps cannot reuse stale\n -- charge data.\n if bagID and slotID and complete then\n local slotKey = bagID .. ":" .. slotID\n chargesCache[slotKey] = {\n link = itemData.link,\n charges = explicitCharges or false,\n }\n if explicitCharges and itemData.link then\n chargeCapableLinks[itemData.link] = true\n end\n end\n\n return result, complete\nend\n'''
if old not in text:
raise SystemExit('ScanProperties return block not found')
text = text.replace(old, new, 1)
# Remove the obsolete early ClassicAPI charge override; the explicit
# tooltip-compatible implementation below is authoritative and avoids
# the historical normal-stack x20 regression.
pattern = re.compile(
r'\n-- ClassicAPI can read charges directly from the live item descriptor, making\n'
r'.*?'
r'\naddon:Debug\("ClassicAPI single-pass ItemDetection enabled"\)\n',
re.S,
)
replacement = '\naddon:Debug("ClassicAPI single-pass ItemDetection enabled")\n'
text, count = pattern.subn(replacement, text, count=1)
if count != 1:
raise SystemExit(f'Expected one obsolete ClassicAPI charge block, found {count}')
start = text.find('--=====================================================\n-- Consolidated from Core/ItemChargesClassicAPI.lua')
end = text.find('--=====================================================\n-- Consolidated from Core/ItemDetectionCacheSafety.lua')
if start < 0 or end < 0 or end <= start:
raise SystemExit('Consolidated charge block markers not found')
charge_block = '''--=====================================================\n-- Explicit charge overlay cache\n--=====================================================\n-- ClassicAPI charge values can mirror ordinary stack counts on this client,\n-- so a yellow xN overlay is still shown only after the tooltip explicitly\n-- confirms a Charges line. The cache below keeps negative results across bag\n-- updates and invalidates real charge items conservatively.\n\nlocal function ChargeSafeSetHyperlink(tooltip, link)\n if not link then return false end\n local _, _, bare = string.find(link, "|H(item:[^|]+)|h")\n if not bare and string.find(link, "^item:") then bare = link end\n if not bare then return false end\n return pcall(tooltip.SetHyperlink, tooltip, bare)\nend\n\nlocal function GetExplicitTooltipCharges(itemData, bagID, slotID)\n local tooltip, tooltipName = addon.Modules.Utils:GetScanTooltip()\n tooltip:SetOwner(WorldFrame, "ANCHOR_NONE")\n tooltip:ClearLines()\n\n local ok = false\n if bagID and slotID and bagID ~= -1 then\n ok = pcall(tooltip.SetBagItem, tooltip, bagID, slotID)\n end\n if not ok then\n ok = ChargeSafeSetHyperlink(tooltip, itemData and itemData.link)\n end\n if not ok then return nil, false end\n\n local numLines = tooltip:NumLines() or 0\n for i = 1, numLines do\n local line = getglobal(tooltipName .. "TextLeft" .. i)\n local text = line and line:GetText()\n if text then\n local _, _, num = string.find(string.lower(text), "^(%d+) charges?$")\n if num then return tonumber(num), numLines >= 2 end\n end\n end\n return nil, numLines >= 2\nend\n\nfunction ItemDetection:GetCharges(itemData, bagID, slotID)\n if not bagID or not slotID then return nil end\n\n local slotKey = bagID .. ":" .. slotID\n local itemLink = itemData and itemData.link or nil\n local cached = chargesCache[slotKey]\n\n -- A slot cache is valid only for the exact item link currently occupying\n -- it. Normal stacks usually hit the cached `false` path here with no\n -- tooltip work at all.\n if cached and cached.link == itemLink then\n if cached.charges == false then return nil end\n return cached.charges\n end\n\n local charges, complete = GetExplicitTooltipCharges(itemData, bagID, slotID)\n if complete then\n chargesCache[slotKey] = {\n link = itemLink,\n charges = charges or false,\n }\n if charges and itemLink then\n chargeCapableLinks[itemLink] = true\n end\n end\n return charges\nend\n\nfunction ItemDetection:InvalidateCharges(bagID)\n if not bagID then\n chargesCache = {}\n chargeCapableLinks = {}\n return\n end\n\n local prefix = bagID .. ":"\n for key, cached in pairs(chargesCache) do\n if string.find(key, "^" .. prefix) then\n -- Keep proven negative results for ordinary items. A changed slot\n -- is still safe because GetCharges validates cached.link against\n -- the current item link. Real/known charge items are discarded so\n -- their remaining charge count is refreshed exactly from tooltip.\n if not cached or cached.charges ~= false\n or (cached.link and chargeCapableLinks[cached.link]) then\n chargesCache[key] = nil\n end\n end\n end\nend\n\naddon:Debug("Explicit charge cache optimization enabled")\n\n'''
text = text[:start] + charge_block + text[end:]
path.write_text(text, encoding='utf-8')
Path('.github/workflows/optimize-charge-cache.yml').unlink()
- name: Commit optimized charge cache
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "perf: avoid repeated charge tooltip scans"
git push origin HEAD:refactor/consolidate-patch-layers