Files
Kenji-NX/src/Ryujinx.Common/Pools/ObjectPool.cs
T
LotP 1bb1c7bba8 gpu allocation optimizations
ObjectPool now uses ConcurrentBag instead if ConcurrentStack, as it has a smaller memory footprint.

Fix compiler warnings related to Audio Command Pools.

Switch gpu command initialization to use pointers, that way skipping the allocation of the command which is unnecessary.

Skip byte array allocation in Ioctl2/3 if it isn't needed (if the source data is all continuous we don't need to copy it to make it continuous).
2025-10-27 09:16:23 -05:00

36 lines
752 B
C#

using System;
using System.Collections.Concurrent;
using System.Threading;
namespace Ryujinx.Common
{
public class ObjectPool<T>(Func<T> factory, int size = -1)
where T : class
{
private int _size = size;
private readonly ConcurrentBag<T> _items = new();
public T Allocate()
{
bool success = _items.TryTake(out T instance);
if (!success)
{
instance = factory();
}
return instance;
}
public void Release(T obj)
{
if (_size < 0 || _items.Count < _size)
{
_items.Add(obj);
}
}
public void Clear() => _items.Clear();
}
}