Skip to content

Commit ffbf84e

Browse files
jeffreyrainy0xFA11
andauthored
fix: more stable profiler stats. Averages over a full second. No memory allocations. (#692)
Co-authored-by: M. Fatih MAR <[email protected]>
1 parent 77790b7 commit ffbf84e

File tree

1 file changed

+12
-64
lines changed

1 file changed

+12
-64
lines changed

com.unity.multiplayer.mlapi/Runtime/Profiling/ProfilerStat.cs

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
using System.Collections.Generic;
21
using UnityEngine;
32

43
namespace MLAPI.Profiling
54
{
6-
public struct Sample
7-
{
8-
public int Count;
9-
public float TimeRecorded;
10-
}
11-
125
public class ProfilerStat
136
{
147
public ProfilerStat(string name)
@@ -18,67 +11,28 @@ public ProfilerStat(string name)
1811
}
1912

2013
public string PrettyPrintName;
21-
protected int m_MaxSamples = 10;
22-
23-
protected LinkedList<Sample> m_Data = new LinkedList<Sample>();
24-
25-
private bool m_IsDirty = true;
2614

27-
protected float m_LastCount;
28-
protected float m_LastTime;
15+
private int m_CurrentSecond = 0;
16+
private int m_TotalAmount = 0;
17+
private int m_Sample = 0;
2918

3019
public virtual void Record(int amt = 1)
3120
{
32-
m_IsDirty = true;
33-
var timeNow = Time.time;
34-
// 'Record' can get called many times in the same frame (for the same exact timestamp)
35-
// This not only blows out the samples but makes the rate computation break since we
36-
// have n samples with a time delta of zero.
37-
//
38-
// Instead, if we want to record a value at the same exact time as our last
39-
// sample, just adjust that sample
40-
if (m_Data.First != null && m_Data.First.Value.TimeRecorded == timeNow)
21+
int currentSecond = (int)Time.unscaledTime;
22+
if (currentSecond != m_CurrentSecond)
4123
{
42-
m_Data.First.Value = new Sample()
43-
{
44-
Count = m_Data.First.Value.Count + amt,
45-
TimeRecorded = m_Data.First.Value.TimeRecorded
46-
};
47-
}
48-
else
49-
{
50-
m_Data.AddFirst(new Sample() { Count = amt, TimeRecorded = Time.time });
51-
while (m_Data.Count > m_MaxSamples)
52-
{
53-
m_Data.RemoveLast();
54-
}
24+
m_Sample = m_TotalAmount;
25+
26+
m_TotalAmount = 0;
27+
m_CurrentSecond = currentSecond;
5528
}
29+
30+
m_TotalAmount += amt;
5631
}
5732

5833
public virtual float SampleRate()
5934
{
60-
if (m_IsDirty)
61-
{
62-
LinkedListNode<Sample> node = m_Data.First;
63-
m_LastCount = 0;
64-
m_LastTime = m_Data.Last?.Value.TimeRecorded ?? 0.0f;
65-
66-
while (node != null)
67-
{
68-
m_LastCount += node.Value.Count;
69-
node = node.Next;
70-
}
71-
72-
m_IsDirty = false;
73-
}
74-
75-
float delta = Time.time - m_LastTime;
76-
if (delta == 0.0f)
77-
{
78-
return 0.0f;
79-
}
80-
81-
return m_LastCount / delta;
35+
return m_Sample;
8236
}
8337
}
8438

@@ -90,12 +44,6 @@ public ProfilerIncStat(string name) : base(name) { }
9044

9145
public override void Record(int amt = 1)
9246
{
93-
m_Data.AddFirst(new Sample() { Count = amt, TimeRecorded = Time.time });
94-
while (m_Data.Count > m_MaxSamples)
95-
{
96-
m_Data.RemoveLast();
97-
}
98-
9947
m_InternalValue += amt;
10048
}
10149

0 commit comments

Comments
 (0)