Skip to content

HashSetPool

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

C# Signature
internal static class HashSetPool<T>

The following example uses a pooled hash set to remove duplicate values.

C#
using System.Collections.Generic;
using Hertzole.SourceGen;
public static class DuplicateRemover
{
public static List<string> RemoveDuplicates(IEnumerable<string> values)
{
// The scope returns the set to the pool when disposed.
using (HashSetPool<string>.Get(out HashSet<string> set))
{
set.UnionWith(values);
return new List<string>(set);
}
}
}

Retrieves a HashSet<T> from the pool.

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

A HashSet<T> from the pool.

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

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

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

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

Returns a HashSet<T> to the pool.

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

The HashSet<T> to return to the pool.