Skip to content

Commit b91ac7d

Browse files
committed
feat: implement profiling utilities
1 parent 991956a commit b91ac7d

14 files changed

Lines changed: 1097 additions & 1 deletion

Runtime/Collections.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
3+
namespace Utils.Collections
4+
{
5+
// source: https://gist.github.com/adammyhre/51aed0c1168ed7fcedf3d6ca6f5c036b (git-amend)
6+
// A fixed-size ring buffer that overwrites the oldest entries when full.
7+
public class CircularBuffer<T>
8+
{
9+
readonly T[] buffer;
10+
int head, tail;
11+
public int Count { get; private set; }
12+
public int Capacity => buffer.Length;
13+
14+
public CircularBuffer(int size)
15+
{
16+
if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size));
17+
buffer = new T[size];
18+
}
19+
20+
public void Enqueue(T item)
21+
{
22+
buffer[head] = item;
23+
head = (head + 1) % Capacity;
24+
if (Count == Capacity) tail = (tail + 1) % Capacity;
25+
else Count++;
26+
}
27+
28+
public T Dequeue()
29+
{
30+
if (Count == 0) throw new InvalidOperationException("Buffer is empty");
31+
var item = buffer[tail];
32+
tail = (tail + 1) % Capacity;
33+
Count--;
34+
return item;
35+
}
36+
37+
// Access elements by logical index (0 = oldest, Count-1 = newest)
38+
public T this[int index]
39+
{
40+
get
41+
{
42+
if ((uint)index >= (uint)Count) throw new ArgumentOutOfRangeException(nameof(index));
43+
if (Capacity == 0 || buffer == null) throw new InvalidOperationException();
44+
return buffer[(tail + index) % Capacity];
45+
}
46+
}
47+
}
48+
}

Runtime/Collections/CircularBuffer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Profiling.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Profiling/AllocCounter.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using UnityEngine.Profiling;
3+
4+
namespace Utils.Profiling
5+
{
6+
// I took it from git-amend's YouTube channel: https://www.youtube.com/watch?v=ham_w48aRJ4
7+
// See UnityEngine.TestTools.Constraints.AllocatingGCMemoryConstraint
8+
// and https://maingauche.games/devlog/20230504-counting-allocations-in-unity/
9+
public class AllocCounter
10+
{
11+
private Recorder _rec;
12+
13+
public AllocCounter()
14+
{
15+
_rec = Recorder.Get("GC.Alloc");
16+
_rec.enabled = false;
17+
18+
#if !UNITY_WEBGL
19+
_rec.FilterToCurrentThread();
20+
#endif
21+
22+
_rec.enabled = true;
23+
24+
}
25+
26+
public int Stop()
27+
{
28+
if (_rec == null) throw new InvalidOperationException("AllocCounter was not started.");
29+
30+
_rec.enabled = false;
31+
32+
#if !UNITY_WEBGL
33+
_rec.CollectFromAllThreads();
34+
#endif
35+
36+
var result = _rec.sampleBlockCount;
37+
_rec = null;
38+
return result;
39+
}
40+
}
41+
}

Runtime/Profiling/AllocCounter.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)