Skip to content

Repository files navigation

ZeroAlloc.Collections

NuGet Build License: MIT AOT GitHub Sponsors

ZeroAlloc.Collections is a high-performance, zero-allocation collections library for .NET. It provides six collection types — each available as a ref struct for stack-only scenarios and as a heap-allocated class for use in async code, fields, and DI containers. All collections rent their backing storage from ArrayPool<T>.Shared, return it on Dispose(), and expose Span<T> accessors for tight inner loops. Source generators are included for emitting specialized, type-specific collection implementations at compile time.

Install

dotnet add package ZeroAlloc.Collections

Example

PooledList

using ZeroAlloc.Collections;

using var list = new PooledList<int>(capacity: 64);

list.Add(1);
list.Add(2);
list.Add(3);

foreach (var item in list)
{
    Console.WriteLine(item);
}

The list rents from ArrayPool<T>.Shared on construction and returns the buffer when Dispose() runs — zero heap allocation for the backing array.

RingBuffer

using ZeroAlloc.Collections;

using var ring = new RingBuffer<string>(capacity: 4);

ring.TryWrite("alpha");
ring.TryWrite("beta");
ring.TryWrite("gamma");

while (ring.TryRead(out var item))
{
    Console.WriteLine(item); // alpha, beta, gamma
}

A fixed-capacity circular buffer suitable for producer/consumer queues, telemetry windows, and bounded logging.

Features

  • Zero Allocation — all pooled collections rent from ArrayPool<T>.Shared and return buffers on disposal
  • Ref Struct Variants — stack-only types with compile-time lifetime enforcement and ref T indexers
  • Heap Variants — class-based counterparts that implement IDisposable, IList<T>, and IReadOnlyList<T> for use in async methods and DI
  • Span AccessorsAsSpan() and AsReadOnlySpan() on every collection for zero-copy interop
  • Source Generators — emit type-specific collections, pooled wrappers, and ref struct enumerators at compile time
  • Analyzer Diagnostics — build-time warnings for undisposed collections and accidental ref struct copies
  • Multi-TFM — targets netstandard2.1, net8.0, and net9.0 (allows ref struct on .NET 9)
  • Native AOT Compatible — no reflection, no dynamic code generation

Collections

Type Ref Struct Heap Variant Description
PooledList<T> Yes HeapPooledList<T> Pooled-backed growable list
RingBuffer<T> Yes HeapRingBuffer<T> Fixed-capacity circular buffer
SpanDictionary<TKey,TValue> Yes HeapSpanDictionary<TKey,TValue> Open-addressing hash map
PooledStack<T> Yes HeapPooledStack<T> Pooled-backed LIFO stack
PooledQueue<T> Yes HeapPooledQueue<T> Pooled-backed FIFO queue
FixedSizeList<T> Yes HeapFixedSizeList<T> Stack-allocated fixed-capacity list

Documentation

Page Description
Getting Started Install and use your first collection in five minutes
PooledList Growable list backed by ArrayPool<T>
RingBuffer Fixed-capacity circular buffer
SpanDictionary Open-addressing hash map with Span accessors
PooledStack & PooledQueue LIFO stack and FIFO queue with pooled storage
FixedSizeList Stack-allocated fixed-capacity list
Source Generators Emit type-specific collections at compile time
Diagnostics Analyzer warnings and error reference
Performance Benchmark results and zero-alloc design internals
Testing Unit-test collections with xUnit

License

MIT

About

Zero-allocation, high-performance collection types for .NET — pooled lists, ring buffers, span dictionaries, stacks, queues, and fixed-size lists with ref struct and heap variants.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages