Quantcast

Cached weight normalization

Kevin Lyles [03-20-11 - 13:00]
Cached weight normalization
Filename
WeightsWatcher.lua
weights.lua
diff --git a/WeightsWatcher.lua b/WeightsWatcher.lua
index 47b489f..0a20fdf 100644
--- a/WeightsWatcher.lua
+++ b/WeightsWatcher.lua
@@ -140,6 +140,27 @@ ww_itemCache = setmetatable({}, ww_itemCacheMetatable)
 ww_weightCache = setmetatable({}, ww_weightCacheMetatable)
 ww_weightIdealCache = setmetatable({}, ww_weightIdealCacheMetatable)

+local ww_weightNormalizationCacheMetatable = {
+	__index = function(tbl, key)
+		local total = 0
+		for _, value in pairs(key) do
+			if type(value) == "number" then
+				if value ~= 0 then
+					total = total + math.abs(value)
+				end
+			end
+		end
+		if total == 0 then
+			-- Avoids a divide by zero
+			total = 1
+		end
+		tbl[key] = total
+		return total
+	end
+}
+
+ww_weightNormalizationCache = setmetatable({}, ww_weightNormalizationCacheMetatable)
+
 local function loadGeneralInfo()
 	local _, class = UnitClass("player")
 	WeightsWatcher.playerClass = class
@@ -944,18 +965,7 @@ function WeightsWatcher.calculateWeight(bareItemStats, itemStats, weightsScale)
 		end
 	end
 	if ww_vars.options.tooltip.normalizeWeights == true then
-		local total = 0
-
-		for _, value in pairs(weightsScale) do
-			if type(value) == "number" then
-				total = total + math.abs(value)
-			end
-		end
-		if total == 0 then
-			-- Avoids a divide by zero
-			return 0
-		end
-		weight = weight / total
+		weight = weight / ww_weightNormalizationCache[weightsScale]
 	end
 	return weight
 end
diff --git a/weights.lua b/weights.lua
index fb2b3d8..c87c4cd 100644
--- a/weights.lua
+++ b/weights.lua
@@ -210,10 +210,6 @@ function ww_configSaveWeight()
 	local number
 	local weightFrame = ww_weights.rightPanel.weightFrame

-	-- The weight is changing, clear any cached info
-	ww_weightCache[weightFrame.category.class][weightFrame.name] = nil
-	ww_weightIdealCache[weightFrame.category.class][weightFrame.name] = nil
-
 	if ww_weights.rightPanel.changedStats then
 		for statValue, statName in pairs(ww_weights.rightPanel.changedStats) do
 			number = tonumber(statValue:GetText())
@@ -233,6 +229,11 @@ function ww_configSaveWeight()
 		end
 	end

+	-- The weight is changing, clear any cached info
+	ww_weightCache[weightFrame.category.class][weightFrame.name] = nil
+	ww_weightIdealCache[weightFrame.category.class][weightFrame.name] = nil
+	ww_weightNormalizationCache[ww_weights.rightPanel.statList] = nil
+
 	ww_weights.rightPanel.changedStats = {}
 	ww_weights.rightPanel.changedTriggers = {}
 	ww_weights.rightPanel.saveButton:Disable()