Skip to content

Commit c9a4a81

Browse files
committed
Improved the performance some more
1 parent cbf9311 commit c9a4a81

File tree

1 file changed

+70
-41
lines changed

1 file changed

+70
-41
lines changed

AddonProfiler.lua

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ local s_trim = string.trim
44
local t_insert = table.insert
55
local t_removemulti = table.removemulti
66
local t_wipe = table.wipe
7+
local pairs = pairs
8+
local GetTime = GetTime
9+
10+
local C_AddOnProfiler_GetAddOnMetric = C_AddOnProfiler.GetAddOnMetric;
11+
local Enum_AddOnProfilerMetric_LastTime = Enum.AddOnProfilerMetric.LastTime;
12+
local Enum_AddOnProfilerMetric_EncounterAverageTime = Enum.AddOnProfilerMetric.EncounterAverageTime;
713

814
local NAP = {};
915
NAP.eventFrame = CreateFrame('Frame');
@@ -79,7 +85,7 @@ function NAP:Init()
7985
local addonName, title, notes = C_AddOns.GetAddOnInfo(i);
8086
self.initialMetrics[addonName] = {};
8187
for metric, ms in pairs(resettableMetrics) do
82-
self.initialMetrics[addonName][ms] = C_AddOnProfiler.GetAddOnMetric(addonName, metric);
88+
self.initialMetrics[addonName][ms] = C_AddOnProfiler_GetAddOnMetric(addonName, metric);
8389
end
8490
local isLoaded = C_AddOns.IsAddOnLoaded(addonName);
8591
if title == '' then
@@ -97,9 +103,8 @@ function NAP:Init()
97103
self:ADDON_LOADED(addonName);
98104
end
99105
end
100-
self.currentMetrics = CopyTable(self.initialMetrics);
101106

102-
self.eventFrame:SetScript('OnUpdate', function(_, ...) self:OnUpdate(...) end);
107+
self.eventFrame:SetScript('OnUpdate', function() self:OnUpdate() end);
103108
self.eventFrame:SetScript('OnEvent', function(_, event, ...)
104109
if self[event] then self[event](self, ...); end
105110
end);
@@ -173,48 +178,50 @@ function NAP:InitNewBucket()
173178
end
174179

175180
function NAP:OnUpdate()
176-
if not self.collectData then return end
177-
178181
self.tickNumber = self.tickNumber + 1;
179182

180-
local peakMs = self.peakMs;
181-
local totalMs = self.totalMs;
182-
local currentMetrics = self.currentMetrics;
183-
184183
local lastBucket = self.snapshots.lastBucket;
185184

186185
local curTickIndex = lastBucket.curTickIndex + 1;
187186
lastBucket.curTickIndex = curTickIndex;
188-
local tickTime = GetTime();
189-
lastBucket.tickMap[curTickIndex] = tickTime;
187+
lastBucket.tickMap[curTickIndex] = GetTime();
188+
189+
local lastTick = lastBucket.lastTick;
190+
local totalMs = self.totalMs;
191+
local peakMs = self.peakMs;
190192

191-
local getMetric = C_AddOnProfiler.GetAddOnMetric;
192193
for addonName in pairs(self.loadedAddons) do
193-
local lastTickMs = getMetric(addonName, Enum.AddOnProfilerMetric.LastTime);
194+
local lastTickMs = C_AddOnProfiler_GetAddOnMetric(addonName, Enum_AddOnProfilerMetric_LastTime);
194195
if lastTickMs > 0 then
195-
lastBucket.lastTick[addonName][curTickIndex] = lastTickMs;
196+
lastTick[addonName][curTickIndex] = lastTickMs;
196197
totalMs[addonName] = totalMs[addonName] + lastTickMs;
197198
if lastTickMs > peakMs[addonName] then
198199
peakMs[addonName] = lastTickMs;
199200
end
200-
for _, ms in ipairs(msOptions) do
201-
local count = getMetric(addonName, msMetricMap[ms]);
202-
local currentMetric = currentMetrics[addonName][ms];
203-
local increment = count - currentMetric;
204-
if increment > 0 then
205-
lastBucket[ms][addonName][curTickIndex] = increment;
206-
else
207-
-- larger ms counts can't increment when smaller ones didn't
208-
break;
209-
end
210-
currentMetrics[addonName][ms] = count;
201+
end
202+
end
203+
end
204+
205+
function NAP:GetCurrentMsSpikeMetrics(onlyForAddonName)
206+
local currentMetrics = {};
207+
if not onlyForAddonName then
208+
for addonName in pairs(self.loadedAddons) do
209+
currentMetrics[addonName] = {};
210+
for metric, ms in pairs(resettableMetrics) do
211+
currentMetrics[addonName][ms] = C_AddOnProfiler_GetAddOnMetric(addonName, metric);
211212
end
212213
end
214+
else
215+
for metric, ms in pairs(resettableMetrics) do
216+
currentMetrics[ms] = C_AddOnProfiler_GetAddOnMetric(onlyForAddonName, metric);
217+
end
213218
end
219+
220+
return currentMetrics;
214221
end
215222

216223
function NAP:ResetMetrics()
217-
self.resetBaselineMetrics = CopyTable(self.currentMetrics);
224+
self.resetBaselineMetrics = self:GetCurrentMsSpikeMetrics();
218225
self.tickNumber = 0;
219226
self.snapshots.buckets = {};
220227
self:InitNewBucket();
@@ -225,30 +232,35 @@ function NAP:ResetMetrics()
225232
end
226233
end
227234

228-
local BUCKET_CUTOFF = 3000; -- rather arbitrary number
235+
local BUCKET_CUTOFF = 2000; -- rather arbitrary number, but interestingly, the lower your fps, the less often actual work will be performed to purge old data ^^
229236
function NAP:PurgeOldData()
230237
if self.snapshots.lastBucket.curTickIndex > BUCKET_CUTOFF then
231238
self:InitNewBucket();
232239
end
233240

241+
local buckets = self.snapshots.buckets
242+
local firstBucket = buckets[1];
243+
if not buckets[2] or not firstBucket.tickMap[1] then
244+
return;
245+
end
246+
234247
local timestamp = GetTime();
235248
local cutoff = timestamp - HISTORY_RANGES[#HISTORY_RANGES];
236249

237-
local firstBucket = self.snapshots.buckets[1];
238-
if not self.snapshots.buckets[2] or not firstBucket.tickMap[1] or firstBucket.tickMap[1] > cutoff then
250+
if firstBucket.tickMap[1] > cutoff then
239251
return;
240252
end
241253

242254
local to;
243-
for i, bucket in ipairs(self.snapshots.buckets) do
255+
for i, bucket in ipairs(buckets) do
244256
if bucket.tickMap[1] and bucket.tickMap[1] > cutoff then
245257
to = i - 1;
246258
break;
247259
end
248260
end
249261

250262
if to and to > 1 then
251-
t_removemulti(self.snapshots.buckets, 1, to);
263+
t_removemulti(buckets, 1, to);
252264
end
253265
end
254266

@@ -311,17 +323,18 @@ function NAP:PrepareFilteredData()
311323
addonName = addonName,
312324
addonTitle = info.title,
313325
peakTime = 0,
314-
encounterAvg = C_AddOnProfiler.GetAddOnMetric(addonName, Enum.AddOnProfilerMetric.EncounterAverageTime),
326+
encounterAvg = C_AddOnProfiler_GetAddOnMetric(addonName, Enum_AddOnProfilerMetric_EncounterAverageTime),
315327
averageMs = 0,
316328
totalMs = 0,
317329
numberOfTicks = 0,
318330
};
319-
for _, ms in ipairs(msOptions) do
331+
for _, ms in pairs(msOptions) do
320332
data[msOptionFieldMap[ms]] = 0;
321333
end
322334
if 0 == self.curHistoryRange then
335+
local currentMetrics = self:GetCurrentMsSpikeMetrics(addonName);
323336
for ms in pairs(msMetricMap) do
324-
local currentMetric = self.currentMetrics[addonName][ms] or 0;
337+
local currentMetric = currentMetrics[ms] or 0;
325338
local baselineMetric = self.resetBaselineMetrics[addonName][ms] or 0;
326339
local adjustedValue = currentMetric - baselineMetric;
327340
data[msOptionFieldMap[ms]] = adjustedValue;
@@ -335,14 +348,30 @@ function NAP:PrepareFilteredData()
335348
for tickIndex = startingTickIndex, bucket.curTickIndex do
336349
local tickMs = bucket.lastTick[addonName][tickIndex];
337350
if tickMs and tickMs > 0 then
338-
data.peakTime = max(data.peakTime, tickMs);
351+
if tickMs > data.peakTime then
352+
data.peakTime = tickMs;
353+
end
339354
data.totalMs = data.totalMs + tickMs;
340-
for _, ms in ipairs(msOptions) do
341-
local count = bucket[ms][addonName][tickIndex];
342-
if count and count > 0 then
343-
data[msOptionFieldMap[ms]] = data[msOptionFieldMap[ms]] + count;
344-
else
345-
break;
355+
-- hardcoded for performance
356+
if tickMs > 1 then
357+
data.over1Ms = data.over1Ms + 1;
358+
if tickMs > 5 then
359+
data.over5Ms = data.over5Ms + 1;
360+
if tickMs > 10 then
361+
data.over10Ms = data.over10Ms + 1;
362+
if tickMs > 50 then
363+
data.over50Ms = data.over50Ms + 1;
364+
if tickMs > 100 then
365+
data.over100Ms = data.over100Ms + 1;
366+
if tickMs > 500 then
367+
data.over500Ms = data.over500Ms + 1;
368+
if tickMs > 1000 then
369+
data.over1000Ms = data.over1000Ms + 1;
370+
end
371+
end
372+
end
373+
end
374+
end
346375
end
347376
end
348377
end

0 commit comments

Comments
 (0)