Skip to content

QueuePool

Provides a shared pool of Queue<T> instances to reduce allocations.

C# Signature
internal static class QueuePool<T>

The following example uses a pooled queue to process values in order.

C#
using System.Collections.Generic;
using Hertzole.SourceGen;
public static class QueueProcessor
{
public static void Process(IEnumerable<int> values)
{
// The scope returns the queue to the pool when disposed.
using (QueuePool<int>.Get(out Queue<int> queue))
{
foreach (int value in values)
{
queue.Enqueue(value);
}
while (queue.Count > 0)
{
System.Console.WriteLine(queue.Dequeue());
}
}
}
}

Retrieves a Queue<T> from the pool.

C# Signature
public static Queue<T> Get();

A Queue<T> from the pool.

Retrieves a Queue<T> from the pool and wraps it in a disposable scope that returns it automatically.

C# Signature
public static PoolScope<Queue<T>> Get(out Queue<T> item);

When this method returns, contains the Queue<T> retrieved from the pool.

A disposable scope that returns the Queue<T> to the pool when disposed.

Returns a Queue<T> to the pool.

C# Signature
public static void Return(Queue<T> item);

The Queue<T> to return to the pool.