Ryujinx/ARMeilleure/Common/ThreadStaticPool.cs

105 lines
2.9 KiB
C#
Raw Normal View History

CodeGen Optimisations (LSRA and Translator) (#978) * Start of JIT garbage collection improvements - thread static pool for Operand, MemoryOperand, Operation - Operands and Operations are always to be constructed via their static helper classes, so they can be pooled. - removing LinkedList from Node for sources/destinations (replaced with List<>s for now, but probably could do arrays since size is bounded) - removing params constructors from Node - LinkedList<> to List<> with Clear() for Operand assignments/uses - ThreadStaticPool is very simple and basically just exists for the purpose of our specific translation allocation problem. Right now it will stay at the worst case allocation count for that thread (so far) - the pool can never shrink. - Still some cases of Operand[] that haven't been removed yet. Will need to evaluate them (eg. is there a reasonable max number of params for Calls?) * ConcurrentStack instead of ConcurrentQueue for Rejit * Optimize some parts of LSRA - BitMap now operates on 64-bit int rather than 32-bit - BitMap is now pooled in a ThreadStatic pool (within lrsa) - BitMap now is now its own iterator. Marginally speeds up iterating through the bits. - A few cases where enumerators were generated have been converted to forms that generate less garbage. - New data structure for sorting _usePositions in LiveIntervals. Much faster split, NextUseAfter, initial insertion. Random insertion is slightly slower. - That last one is WIP since you need to insert the values backwards. It would be ideal if it just flipped it for you, uncomplicating things on the caller side. * Use a static pool of thread static pools. (yes.) Prevents each execution thread creating its own lowCq pool and making me cry. * Move constant value to top, change naming convention. * Fix iteration of memory operands. * Increase max thread count. * Address Feedback
2020-03-18 12:44:32 +01:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
namespace ARMeilleure.Common
{
internal class ThreadStaticPool<T> where T : class, new()
{
private const int PoolSizeIncrement = 200;
[ThreadStatic]
private static ThreadStaticPool<T> _instance;
public static ThreadStaticPool<T> Instance
{
get
{
if (_instance == null)
{
PreparePool(0); // So that we can still use a pool when blindly initializing one.
}
return _instance;
}
}
private static ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>> _pools = new ConcurrentDictionary<int, Stack<ThreadStaticPool<T>>>();
private static Stack<ThreadStaticPool<T>> GetPools(int groupId)
{
return _pools.GetOrAdd(groupId, x => new Stack<ThreadStaticPool<T>>());
}
public static void PreparePool(int groupId)
{
// Prepare the pool for this thread, ideally using an existing one from the specified group.
if (_instance == null)
{
Stack<ThreadStaticPool<T>> pools = GetPools(groupId);
lock (pools)
{
_instance = (pools.Count != 0) ? pools.Pop() : new ThreadStaticPool<T>(PoolSizeIncrement * 2);
}
}
}
public static void ReturnPool(int groupId)
{
// Reset and return the pool for this thread to the specified group.
Stack<ThreadStaticPool<T>> pools = GetPools(groupId);
lock (pools)
{
_instance.Clear();
pools.Push(_instance);
_instance = null;
}
}
private T[] _pool;
private int _poolUsed = -1;
private int _poolSize;
public ThreadStaticPool(int initialSize)
{
_pool = new T[initialSize];
for (int i = 0; i < initialSize; i++)
{
_pool[i] = new T();
}
_poolSize = initialSize;
}
public T Allocate()
{
int index = Interlocked.Increment(ref _poolUsed);
if (index >= _poolSize)
{
IncreaseSize();
}
return _pool[index];
}
private void IncreaseSize()
{
_poolSize += PoolSizeIncrement;
T[] newArray = new T[_poolSize];
Array.Copy(_pool, 0, newArray, 0, _pool.Length);
for (int i = _pool.Length; i < _poolSize; i++)
{
newArray[i] = new T();
}
Interlocked.Exchange(ref _pool, newArray);
}
public void Clear()
{
_poolUsed = -1;
}
}
}