Files
Nerfed/Nerfed.Runtime/Util/BoundedQueue.cs
T
max 2d4139fb2c Enhance Profiler with Label Metrics and Rolling Windows
- Introduced LabelMetrics and RollingLabelMetrics structs to capture detailed profiling data.
- Implemented a RollingWindow class to maintain a rolling average of metrics.
- Updated Frame class to collect and store label metrics, including memory allocation and garbage collection statistics.
- Enhanced ScopeNode class to support self-time calculations and child duration tracking.
- Improved the Profiler class to manage frame budgets and rolling metrics.
- Refactored the ProfilerVisualizer to support new timeline rendering features, including zoom and pan functionality.
- Added tooltip support for detailed node information in the flame graph.
- Enhanced BoundedQueue to be thread-safe with proper locking mechanisms for concurrent access.
2026-08-04 17:19:00 +02:00

130 lines
2.4 KiB
C#

using System.Collections;
namespace Nerfed.Runtime;
public class BoundedQueue<T> : IEnumerable<T>, ICollection, IReadOnlyCollection<T>
{
private readonly Queue<T> queue = null;
private readonly object syncLock = new object();
private readonly int maxSize = 10;
private T lastAddedElement;
public BoundedQueue(int maxSize)
{
this.maxSize = maxSize;
queue = new Queue<T>(maxSize);
}
public void Enqueue(T item)
{
Enqueue(item, out _);
}
public bool Enqueue(T item, out T evictedItem)
{
lock (syncLock)
{
queue.Enqueue(item);
if (queue.Count > maxSize)
{
evictedItem = queue.Dequeue();
lastAddedElement = item;
return true;
}
evictedItem = default;
lastAddedElement = item;
return false;
}
}
public T Dequeue()
{
lock (syncLock)
{
return queue.Dequeue();
}
}
public T Peek()
{
lock (syncLock)
{
return queue.Peek();
}
}
public T LastAddedElement()
{
lock (syncLock)
{
return lastAddedElement;
}
}
public void Clear()
{
lock (syncLock)
{
queue.Clear();
}
}
public bool Contains(T item)
{
lock (syncLock)
{
return queue.Contains(item);
}
}
public IEnumerator<T> GetEnumerator()
{
T[] snapshot;
lock (syncLock)
{
snapshot = queue.ToArray();
}
return ((IEnumerable<T>)snapshot).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void CopyTo(Array array, int index)
{
lock (syncLock)
{
((ICollection)queue).CopyTo(array, index);
}
}
public int Count
{
get
{
lock (syncLock)
{
return queue.Count;
}
}
}
public int Capacity => maxSize;
public bool IsSynchronized => true;
public object SyncRoot => syncLock;
int IReadOnlyCollection<T>.Count
{
get
{
lock (syncLock)
{
return queue.Count;
}
}
}
}