From c6976cc79d406a31b88f179246f7bb2e4d013476 Mon Sep 17 00:00:00 2001 From: Kkthnx <40672673+Kkthnx@users.noreply.github.com> Date: Sun, 17 Nov 2024 16:30:51 -0500 Subject: [PATCH] Caching --- KkthnxUI/Developer/Core.lua | 19 +++++++ KkthnxUI/Modules/ActionBars/ButtonStyle.lua | 8 ++- KkthnxUI/Modules/ActionBars/Cooldown.lua | 7 +-- KkthnxUI/Modules/ActionBars/Elements/Bars.lua | 20 ++++--- .../Modules/ActionBars/Elements/Extrabar.lua | 10 +++- KkthnxUI/Modules/ActionBars/Fader.lua | 7 +++ KkthnxUI/Modules/ActionBars/Keybind.lua | 52 ++++++++++--------- 7 files changed, 86 insertions(+), 37 deletions(-) diff --git a/KkthnxUI/Developer/Core.lua b/KkthnxUI/Developer/Core.lua index 46550cd8..95759264 100644 --- a/KkthnxUI/Developer/Core.lua +++ b/KkthnxUI/Developer/Core.lua @@ -1,6 +1,25 @@ local K, C, L = KkthnxUI[1], KkthnxUI[2], KkthnxUI[3] local Module = K:NewModule("Developer") +-- Benchmarking global vs. cached access in WoW Lua +local _G = _G +_G.fake1 = 42 -- Set up a global variable + +-- Case 1: Direct global access +local start_time = debugprofilestop() +for i = 1, 1e7 do + local temp = _G.fake1 +end +print("Global access:", debugprofilestop() - start_time, "ms") + +-- Case 2: Cached access +local fake1 = _G.fake1 +start_time = debugprofilestop() +for i = 1, 1e7 do + local temp = fake1 +end +print("Cached access:", debugprofilestop() - start_time, "ms") + K.Devs = { ["Kkthnx-Area 52"] = true, ["Kkthnx-Valdrakken"] = true, diff --git a/KkthnxUI/Modules/ActionBars/ButtonStyle.lua b/KkthnxUI/Modules/ActionBars/ButtonStyle.lua index f45ca18f..acc7bcf2 100644 --- a/KkthnxUI/Modules/ActionBars/ButtonStyle.lua +++ b/KkthnxUI/Modules/ActionBars/ButtonStyle.lua @@ -1,8 +1,14 @@ local K = KkthnxUI[1] local Module = K:GetModule("ActionBar") --- Importing required functions and constants +-- WoW API +local CreateFrame = CreateFrame + +-- Lua functions local gsub = string.gsub +local ipairs = ipairs + +-- Constants local KEY_BUTTON4, KEY_NUMPAD1, RANGE_INDICATOR = KEY_BUTTON4, KEY_NUMPAD1, RANGE_INDICATOR -- Processing key strings diff --git a/KkthnxUI/Modules/ActionBars/Cooldown.lua b/KkthnxUI/Modules/ActionBars/Cooldown.lua index c1342f45..7d58d968 100644 --- a/KkthnxUI/Modules/ActionBars/Cooldown.lua +++ b/KkthnxUI/Modules/ActionBars/Cooldown.lua @@ -1,9 +1,10 @@ local K, C = KkthnxUI[1], KkthnxUI[2] local Module = K:NewModule("Cooldown") --- Importing required functions -local pairs, format, floor, strfind = pairs, format, floor, strfind -local GetTime, GetActionCooldown, tonumber = GetTime, GetActionCooldown, tonumber +-- Localizing global functions and constants +local _G = _G +local pairs, format, floor, strfind = pairs, string.format, math.floor, string.find +local GetTime, GetActionCooldown, tonumber = _G.GetTime, _G.GetActionCooldown, tonumber -- Constants for cooldown display local FONT_SIZE = 19 diff --git a/KkthnxUI/Modules/ActionBars/Elements/Bars.lua b/KkthnxUI/Modules/ActionBars/Elements/Bars.lua index 8b88b134..aaa25450 100644 --- a/KkthnxUI/Modules/ActionBars/Elements/Bars.lua +++ b/KkthnxUI/Modules/ActionBars/Elements/Bars.lua @@ -1,15 +1,19 @@ local K, C = KkthnxUI[1], KkthnxUI[2] local Module = K:NewModule("ActionBar") --- Global references for convenience and performance +-- Cache global references local _G = _G -local UIParent = UIParent -local GetVehicleBarIndex = GetVehicleBarIndex -local UnitExists = UnitExists -local VehicleExit = VehicleExit -local PetDismiss = PetDismiss -local tinsert = table.insert -local RegisterStateDriver = RegisterStateDriver +local UIParent = _G.UIParent +local GetVehicleBarIndex = _G.GetVehicleBarIndex +local UnitExists = _G.UnitExists +local VehicleExit = _G.VehicleExit +local PetDismiss = _G.PetDismiss +local tinsert = _G.table.insert +local RegisterStateDriver = _G.RegisterStateDriver +local ceil = _G.math.ceil +local min = _G.math.min +local select = _G.select +local GetCVarBool = _G.GetCVarBool -- Layout constants local margin, padding = 6, 0 diff --git a/KkthnxUI/Modules/ActionBars/Elements/Extrabar.lua b/KkthnxUI/Modules/ActionBars/Elements/Extrabar.lua index 69f26fc1..080e5078 100644 --- a/KkthnxUI/Modules/ActionBars/Elements/Extrabar.lua +++ b/KkthnxUI/Modules/ActionBars/Elements/Extrabar.lua @@ -1,7 +1,15 @@ local K = KkthnxUI[1] local Module = K:GetModule("ActionBar") -local tinsert = tinsert +-- Cache global references +local _G = _G +local CreateFrame = _G.CreateFrame +local UIParent = _G.UIParent +local tinsert = _G.table.insert +local RegisterStateDriver = _G.RegisterStateDriver +local hooksecurefunc = _G.hooksecurefunc +local IsUsableAction = _G.IsUsableAction + local padding = 0 function Module:CreateExtrabar() diff --git a/KkthnxUI/Modules/ActionBars/Fader.lua b/KkthnxUI/Modules/ActionBars/Fader.lua index 5e58954e..05b68ca8 100644 --- a/KkthnxUI/Modules/ActionBars/Fader.lua +++ b/KkthnxUI/Modules/ActionBars/Fader.lua @@ -3,6 +3,13 @@ local Module = K:GetModule("ActionBar") -- Credit: ElvUI +-- Localizing global functions and constants +local _G = _G +local pairs, ipairs, next = pairs, ipairs, next +local UnitAffectingCombat, UnitExists, UnitHealth, UnitHealthMax = _G.UnitAffectingCombat, _G.UnitExists, _G.UnitHealth, _G.UnitHealthMax +local UnitCastingInfo, UnitChannelInfo, UnitHasVehicleUI = _G.UnitCastingInfo, _G.UnitChannelInfo, _G.UnitHasVehicleUI +local CreateFrame, C_Timer = _G.CreateFrame, _G.C_Timer + local fadeParent Module.handledbuttons = {} diff --git a/KkthnxUI/Modules/ActionBars/Keybind.lua b/KkthnxUI/Modules/ActionBars/Keybind.lua index 2e1136e2..33b6b3ed 100644 --- a/KkthnxUI/Modules/ActionBars/Keybind.lua +++ b/KkthnxUI/Modules/ActionBars/Keybind.lua @@ -1,29 +1,33 @@ local K, L = KkthnxUI[1], KkthnxUI[3] local Module = K:GetModule("ActionBar") -local C_SpellBook_GetSpellBookItemName = C_SpellBook.GetSpellBookItemName -local CreateFrame = CreateFrame -local GameTooltip = GameTooltip -local GetBindingKey = GetBindingKey -local GetBindingName = GetBindingName -local GetMacroInfo = GetMacroInfo -local InCombatLockdown = InCombatLockdown -local IsAltKeyDown = IsAltKeyDown -local IsControlKeyDown = IsControlKeyDown -local IsShiftKeyDown = IsShiftKeyDown -local LoadBindings = LoadBindings -local MAX_ACCOUNT_MACROS = MAX_ACCOUNT_MACROS -local NOT_BOUND = NOT_BOUND -local PRESS_KEY_TO_BIND = PRESS_KEY_TO_BIND -local SaveBindings = SaveBindings -local SetBinding = SetBinding -local SpellBook_GetSpellBookSlot = SpellBook_GetSpellBookSlot -local UIErrorsFrame = UIErrorsFrame -local format = format -local hooksecurefunc = hooksecurefunc -local strfind = strfind -local strupper = strupper -local tonumber = tonumber +-- Cache global functions and constants +local _G = _G +local C_SpellBook_GetSpellBookItemName = _G.C_SpellBook.GetSpellBookItemName +local CreateFrame = _G.CreateFrame +local GameTooltip = _G.GameTooltip +local GetBindingKey = _G.GetBindingKey +local GetBindingName = _G.GetBindingName +local GetMacroInfo = _G.GetMacroInfo +local InCombatLockdown = _G.InCombatLockdown +local IsAltKeyDown = _G.IsAltKeyDown +local IsControlKeyDown = _G.IsControlKeyDown +local IsShiftKeyDown = _G.IsShiftKeyDown +local LoadBindings = _G.LoadBindings +local MAX_ACCOUNT_MACROS = _G.MAX_ACCOUNT_MACROS +local NOT_BOUND = _G.NOT_BOUND +local PRESS_KEY_TO_BIND = _G.PRESS_KEY_TO_BIND +local SaveBindings = _G.SaveBindings +local SetBinding = _G.SetBinding +local SpellBook_GetSpellBookSlot = _G.SpellBook_GetSpellBookSlot +local UIErrorsFrame = _G.UIErrorsFrame +local format = _G.format +local hooksecurefunc = _G.hooksecurefunc +local strfind = _G.strfind +local strupper = _G.strupper +local tonumber = _G.tonumber +local Enum = _G.Enum +local C_AddOns = _G.C_AddOns -- Button types local function hookActionButton(self) @@ -148,7 +152,7 @@ function Module:Bind_Update(button, spellmacro) if spellmacro == "SPELL" then frame.id = SpellBook_GetSpellBookSlot(button) - frame.name = C_SpellBook.GetSpellBookItemName(frame.id, Enum.SpellBookSpellBank.Player) + frame.name = C_SpellBook_GetSpellBookItemName(frame.id, Enum.SpellBookSpellBank.Player) frame.bindings = { GetBindingKey(spellmacro .. " " .. frame.name) } elseif spellmacro == "MACRO" then frame.id = button.selectionIndex or button:GetID()