Skip to content

Commit 3047068

Browse files
fix: Removed per-tick runtime allocations by reusing memory between ticks (#640)
PerformanceDataManager reuses PerformanceTickData by resetting per-frame Replaces deferred linq query in PerformanceTickData loop.
1 parent 56a02e6 commit 3047068

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,29 @@ namespace MLAPI.Profiling
55
{
66
internal static class PerformanceDataManager
77
{
8-
private static PerformanceTickData s_ProfilerData;
8+
private static PerformanceTickData s_ProfilerData = new PerformanceTickData();
99
private static int s_TickId;
1010

1111
internal static void BeginNewTick()
1212
{
1313
s_TickId = Math.Max(s_TickId, 0);
14-
s_ProfilerData = new PerformanceTickData
15-
{
16-
TickId = s_TickId++,
17-
};
14+
s_ProfilerData.Reset();
15+
s_ProfilerData.TickId = s_TickId++;
1816
}
1917

2018
internal static void Increment(string fieldName, int count = 1)
2119
{
22-
s_ProfilerData?.Increment(fieldName, count);
20+
s_ProfilerData.Increment(fieldName, count);
2321
}
2422

2523
internal static void AddTransportData(IReadOnlyDictionary<string, int> transportProfilerData)
2624
{
27-
s_ProfilerData?.AddNonDuplicateData(transportProfilerData);
25+
s_ProfilerData.AddNonDuplicateData(transportProfilerData);
2826
}
2927

3028
internal static PerformanceTickData GetData()
3129
{
3230
return s_ProfilerData;
3331
}
3432
}
35-
}
33+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ public void Increment(string fieldName, int count = 1)
1616

1717
public void AddNonDuplicateData(IReadOnlyDictionary<string, int> transportProfilerData)
1818
{
19-
var nonDuplicates = transportProfilerData.Where(entry => !m_TickData.HasData(entry.Key));
20-
foreach (var entry in nonDuplicates)
19+
foreach (var entry in transportProfilerData)
2120
{
21+
if (m_TickData.HasData(entry.Key))
22+
{
23+
continue;
24+
}
2225
m_TickData.Add(entry.Key, entry.Value);
2326
}
2427
}
@@ -32,5 +35,10 @@ public bool HasData(string fieldName)
3235
{
3336
return m_TickData.HasData(fieldName);
3437
}
38+
39+
public void Reset()
40+
{
41+
m_TickData.Clear();
42+
}
3543
}
3644
}

0 commit comments

Comments
 (0)