audio effects fix and audio object pooling (ryubing/ryujinx!192)

See merge request ryubing/ryujinx!192
This commit is contained in:
LotP 2025-10-25 21:07:10 -05:00
parent c6bc77e4bf
commit fd07453887
44 changed files with 764 additions and 595 deletions

View file

@ -1,67 +1,35 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
namespace Ryujinx.Common
{
public class ObjectPool<T>(Func<T> factory, int size)
public class ObjectPool<T>(Func<T> factory, int size = -1)
where T : class
{
private T _firstItem;
private readonly T[] _items = new T[size - 1];
private int _size = size;
private readonly ConcurrentStack<T> _items = new();
public T Allocate()
{
T instance = _firstItem;
bool success = _items.TryPop(out T instance);
if (instance == null || instance != Interlocked.CompareExchange(ref _firstItem, null, instance))
if (!success)
{
instance = AllocateInternal();
instance = factory();
}
return instance;
}
private T AllocateInternal()
{
T[] items = _items;
for (int i = 0; i < items.Length; i++)
{
T instance = items[i];
if (instance != null && instance == Interlocked.CompareExchange(ref items[i], null, instance))
{
return instance;
}
}
return factory();
}
public void Release(T obj)
{
if (_firstItem == null)
if (_size < 0 || _items.Count < _size)
{
_firstItem = obj;
}
else
{
ReleaseInternal(obj);
}
}
private void ReleaseInternal(T obj)
{
T[] items = _items;
for (int i = 0; i < items.Length; i++)
{
if (items[i] == null)
{
items[i] = obj;
break;
}
_items.Push(obj);
}
}
public void Clear() => _items.Clear();
}
}