unity-collection-pool

Unity Collection Pool expert for GC-free collection management using ListPool, DictionaryPool, HashSetPool, and ObjectPool. Masters memory optimization, pool sizing, and allocation-free patterns. Use PROACTIVELY for collection allocations, GC pressure reduction, temporary list/dictionary usage, or performance-critical code paths.

8 stars

Best use case

unity-collection-pool is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Unity Collection Pool expert for GC-free collection management using ListPool, DictionaryPool, HashSetPool, and ObjectPool. Masters memory optimization, pool sizing, and allocation-free patterns. Use PROACTIVELY for collection allocations, GC pressure reduction, temporary list/dictionary usage, or performance-critical code paths.

Teams using unity-collection-pool should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/unity-collection-pool/SKILL.md --create-dirs "https://raw.githubusercontent.com/creator-hian/claude-code-plugins/main/unity-plugin/skills/unity-collection-pool/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/unity-collection-pool/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How unity-collection-pool Compares

Feature / Agentunity-collection-poolStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Unity Collection Pool expert for GC-free collection management using ListPool, DictionaryPool, HashSetPool, and ObjectPool. Masters memory optimization, pool sizing, and allocation-free patterns. Use PROACTIVELY for collection allocations, GC pressure reduction, temporary list/dictionary usage, or performance-critical code paths.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Unity Collection Pool - GC-Free Collection Management

## Overview

Unity's `UnityEngine.Pool` namespace (2021.1+) provides built-in collection pooling to eliminate GC allocations from temporary collection usage.

**Foundation Required**: `unity-csharp-fundamentals` (TryGetComponent, FindAnyObjectByType), C# generics, IDisposable pattern

**Core Topics**:
- ListPool, HashSetPool, DictionaryPool usage
- CollectionPool for custom collections
- ObjectPool for arbitrary objects
- Pool lifecycle and disposal patterns
- Memory optimization strategies

## Quick Start

### ListPool Basic Usage

```csharp
using UnityEngine.Pool;
using System.Collections.Generic;

public class PoolExample : MonoBehaviour
{
    void ProcessItems()
    {
        // Get pooled list (zero allocation after warmup)
        List<int> tempList = ListPool<int>.Get();

        try
        {
            // Use the list
            tempList.Add(1);
            tempList.Add(2);
            tempList.Add(3);
            ProcessList(tempList);
        }
        finally
        {
            // Always return to pool
            ListPool<int>.Release(tempList);
        }
    }
}
```

### Using Statement Pattern (Recommended)

```csharp
using UnityEngine.Pool;

void ProcessWithUsing()
{
    // Auto-release via PooledObject<T>
    List<int> tempList;
    using (ListPool<int>.Get(out tempList))
    {
        tempList.Add(1);
        tempList.Add(2);
        DoSomething(tempList);
    } // Automatically returned to pool
}

// HashSet example
void CheckDuplicates(IEnumerable<string> items)
{
    HashSet<string> seen;
    using (HashSetPool<string>.Get(out seen))
    {
        foreach (string item in items)
        {
            if (!seen.Add(item))
                Debug.Log($"Duplicate: {item}");
        }
    }
}

// Dictionary example
void BuildLookup(Item[] items)
{
    Dictionary<int, Item> lookup;
    using (DictionaryPool<int, Item>.Get(out lookup))
    {
        foreach (Item item in items)
            lookup[item.Id] = item;

        ProcessLookup(lookup);
    }
}
```

## Available Pools

| Pool Type | Usage | Get/Release |
|-----------|-------|-------------|
| `ListPool<T>` | Temporary lists | `Get()` / `Release(list)` |
| `HashSetPool<T>` | Duplicate checking, set operations | `Get()` / `Release(set)` |
| `DictionaryPool<K,V>` | Temporary lookups | `Get()` / `Release(dict)` |
| `CollectionPool<C,T>` | Custom ICollection types | `Get()` / `Release(coll)` |
| `ObjectPool<T>` | Arbitrary object pooling | `Get()` / `Release(obj)` |
| `LinkedPool<T>` | Linked list-based pool | `Get()` / `Release(obj)` |
| `GenericPool<T>` | Static shared pool | `Get()` / `Release(obj)` |

## Common Patterns

### Raycast Results Pooling

```csharp
void DetectCollisions(Vector3 origin, Vector3 direction)
{
    List<RaycastHit> hits;
    using (ListPool<RaycastHit>.Get(out hits))
    {
        int count = Physics.RaycastNonAlloc(origin, direction, hitsArray);
        for (int i = 0; i < count; i++)
            hits.Add(hitsArray[i]);

        ProcessHits(hits);
    }
}
```

### Component Query Pooling

```csharp
void FindAllEnemies()
{
    List<Enemy> enemies;
    using (ListPool<Enemy>.Get(out enemies))
    {
        GetComponentsInChildren(enemies); // Overload that takes list
        foreach (Enemy enemy in enemies)
            enemy.Alert();
    }
}
```

### LINQ Alternative (GC-Free)

```csharp
// AVOID: LINQ allocates
List<Item> filtered = items.Where(x => x.IsActive).ToList();

// PREFER: Pooled collection
List<Item> pooledFiltered;
using (ListPool<Item>.Get(out pooledFiltered))
{
    foreach (Item item in items)
    {
        if (item.IsActive)
            pooledFiltered.Add(item);
    }
    Process(pooledFiltered);
}
```

## Performance Guidelines

### When to Use Pools

```yaml
Use Pools:
  - Temporary collections in Update/FixedUpdate
  - Collections created and discarded within single method
  - High-frequency operations (per-frame, per-physics-step)
  - Known short-lived collection usage

Avoid Pools:
  - Long-lived collections (store as fields instead)
  - Collections passed across async boundaries
  - Collections with unclear ownership
  - Very small operations (< 3 items, consider stackalloc)
```

### Pool vs Stackalloc

```csharp
// For very small, fixed-size arrays, prefer stackalloc
Span<int> small = stackalloc int[4];

// For variable size or larger collections, use pool
List<int> larger;
using (ListPool<int>.Get(out larger))
{
    // Variable size operations
}
```

## Key Principles

1. **Always Release**: Use `using` pattern or try/finally to guarantee release
2. **Clear on Get**: Pools automatically clear collections on Get
3. **Don't Store References**: Never cache pooled collection references
4. **Match Types**: Release to same pool type that provided the collection
5. **Prefer `using` Pattern**: Auto-disposal prevents leaks

## Anti-Patterns

```csharp
// WRONG: Storing pooled reference
private List<int> mCachedList;
void Bad()
{
    mCachedList = ListPool<int>.Get(); // Memory leak!
}

// WRONG: Missing release
void AlsoBAD()
{
    List<int> list = ListPool<int>.Get();
    Process(list);
    // Forgot to release - leak!
}

// WRONG: Releasing wrong pool
void VeryBad()
{
    List<int> list = ListPool<int>.Get();
    ListPool<float>.Release(list); // Type mismatch!
}

// WRONG: Using after release
void TerriblyBad()
{
    List<int> list;
    using (ListPool<int>.Get(out list)) { }
    list.Add(1); // Using disposed collection!
}
```

## Reference Documentation

### [Pool Fundamentals](references/pool-fundamentals.md)
Core pool concepts:
- Pool lifecycle and memory management
- Built-in pool types detailed API
- Collection clearing behavior
- Capacity management
- Thread safety considerations

### [Advanced Patterns](references/advanced-patterns.md)
Advanced usage patterns:
- Custom ObjectPool implementation
- Pool configuration and sizing
- Nested pool usage
- Integration with ECS/DOTS
- Profiling pool efficiency

## Integration with Other Skills

- **unity-performance**: Collection pooling is key optimization technique
- **unity-async**: Careful pool usage across async boundaries
- **unity-unitask**: Combine with UniTask for async pooling patterns

Related Skills

unity-vcontainer

8
from creator-hian/claude-code-plugins

VContainer dependency injection expert specializing in IoC container configuration, lifecycle management, and Unity-optimized DI patterns. Masters dependency resolution, scoped containers, and testable architecture design. Use PROACTIVELY for VContainer setup, service registration, or SOLID principle implementation.

unity-unitask

8
from creator-hian/claude-code-plugins

UniTask library expert specializing in allocation-free async/await patterns, coroutine migration, and Unity-optimized asynchronous programming. Masters UniTask performance optimizations, cancellation handling, and memory-efficient async operations. Use PROACTIVELY for UniTask implementation, async optimization, or coroutine replacement.

unity-unirx

8
from creator-hian/claude-code-plugins

UniRx (Reactive Extensions) library expert for legacy Unity projects. Specializes in UniRx-specific patterns, Observable streams, and ReactiveProperty. Use for maintaining existing UniRx codebases. For new projects, use unity-r3 skill instead.

unity-ui

8
from creator-hian/claude-code-plugins

Build and optimize Unity UI with UI Toolkit and UGUI. Masters responsive layouts, event systems, and performance optimization. Use for UI implementation, Canvas optimization, or cross-platform UI challenges.

unity-textmeshpro

8
from creator-hian/claude-code-plugins

TextMeshPro (TMPro) expert for Unity text rendering with advanced typography, performance optimization, and professional text effects. Masters font asset creation, dynamic fonts, rich text formatting, material presets, and text mesh optimization. Use PROACTIVELY for text rendering, font management, localization text, UI text performance, or text effects implementation.

unity-testrunner

8
from creator-hian/claude-code-plugins

Unity Test Framework CLI automation and test writing patterns. Masters batchmode execution, NUnit assertions, EditMode/PlayMode testing, and TDD workflows. Use PROACTIVELY for test automation, CI/CD pipelines, or test-driven development in Unity.

unity-r3

8
from creator-hian/claude-code-plugins

R3 (Reactive Extensions) library expert specializing in modern reactive programming patterns, event-driven architectures, and Observable streams. Masters R3-specific features, async enumerable integration, and Unity-optimized reactive patterns. Use PROACTIVELY for R3 implementation, reactive programming, or MVVM/MVP architecture.

unity-performance

8
from creator-hian/claude-code-plugins

Optimize Unity game performance through profiling, draw call reduction, and resource management. Masters batching, LOD, occlusion culling, and mobile optimization. Use for performance bottlenecks, frame rate issues, or optimization strategies.

unity-networking

8
from creator-hian/claude-code-plugins

Implement multiplayer games with Unity Netcode, Mirror, or Photon. Masters client-server architecture, state synchronization, and lag compensation. Use for multiplayer features, networking issues, or real-time synchronization.

unity-mobile

8
from creator-hian/claude-code-plugins

Optimize Unity games for mobile platforms with IL2CPP, platform-specific code, and memory management. Masters iOS/Android deployment, app size reduction, and battery optimization. Use for mobile builds, platform issues, or device-specific optimization.

unity-csharp-fundamentals

8
from creator-hian/claude-code-plugins

Unity C# fundamental patterns including TryGetComponent, SerializeField, RequireComponent, and safe coding practices. Essential patterns for robust Unity development. Use PROACTIVELY for any Unity C# code to ensure best practices.

unity-async

8
from creator-hian/claude-code-plugins

Handle Unity's asynchronous programming patterns including coroutines, async/await, and Job System. Masters Unity's main thread restrictions and threading models. Use when guidance needed on Unity async operations, coroutine optimization, or parallel processing in Unity context.