using System.Collections.Concurrent;
namespace Sandbox;
///
/// A thread-safe pool for reusing instances.
/// Returning to the pool clears the instance but does not free its memory, allowing for efficient reuse.
///
internal class RenderAttributePool
{
private readonly ConcurrentBag Pool = [];
///
/// Get a pooled instance.
/// Make sure you return it to the pool after use with .
///
public RenderAttributes Get()
{
if ( Pool.TryTake( out var ra ) )
return ra;
return new RenderAttributes();
}
///
/// Returns a instance to the pool for reuse.
/// Make sure you're done with it because it will be cleared.
///
public void Return( RenderAttributes renderAttributes )
{
ArgumentNullException.ThrowIfNull( renderAttributes );
renderAttributes.Clear( false ); // don't free our memory, we want to reuse it
Pool.Add( renderAttributes );
}
}