ZeroAlloc.Collections
2.0.1
dotnet add package ZeroAlloc.Collections --version 2.0.1
NuGet\Install-Package ZeroAlloc.Collections -Version 2.0.1
<PackageReference Include="ZeroAlloc.Collections" Version="2.0.1" />
<PackageVersion Include="ZeroAlloc.Collections" Version="2.0.1" />
<PackageReference Include="ZeroAlloc.Collections" />
paket add ZeroAlloc.Collections --version 2.0.1
#r "nuget: ZeroAlloc.Collections, 2.0.1"
#:package ZeroAlloc.Collections@2.0.1
#addin nuget:?package=ZeroAlloc.Collections&version=2.0.1
#tool nuget:?package=ZeroAlloc.Collections&version=2.0.1
ZeroAlloc.Collections
Pooled and stack-only counterparts to List<T>, Dictionary<TKey,TValue>, Stack<T> and Queue<T>
whose ref struct forms add, look up and enumerate with 0 B allocated, NativeAOT-ready.
Why ZeroAlloc.Collections
- 0 B on the hot path, asserted in CI. The
ref structcollections add, look up and enumerate with 0 B allocated per call;AllocationTestsfails CI if any of them allocates. - Faster than the BCL as well. Adding 1,000 items to a
PooledList<int>takes 0.63× the time ofList<int>, which allocates 8,424 B in the same run. Stacks, queues and dictionaries take 0.6–0.8×, and aRingBuffer<int>takes 0.47× a pre-sizedQueue<int>. - Works under NativeAOT and trimming. No reflection and no runtime code generation; every PR publishes and runs an AOT sample.
- Compile-time diagnostics instead of runtime surprises.
ZAC001warns when a pooled collection is never disposed, so a buffer that would never return to the pool is a build warning.
Install
dotnet add package ZeroAlloc.Collections
Quick start
using ZeroAlloc.Collections;
string[] order = ["apple", "pear", "apple", "kiwi"];
// Rents its entries from ArrayPool<T>.Shared and returns them on Dispose.
using var prices = new SpanDictionary<string, decimal>(capacity: 16);
prices.Add("apple", 0.50m);
prices.Add("pear", 0.75m);
// Lives on the stack: no heap allocation at all.
var basket = new FixedSizeList<decimal>(stackalloc decimal[8]);
foreach (var item in order)
if (prices.TryGetValue(item, out var price)) basket.Add(price);
decimal total = 0;
foreach (var p in basket) total += p;
Console.WriteLine($"{basket.Count} items, {total}"); // 3 items, 1.75
What you get
| 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 |
ConcurrentHeapSpanDictionary<TKey,TValue> |
No | — | Thread-safe, coarse-locked, pooled hash map |
- Two forms of each: a
ref structfor synchronous hot paths, and aHeap*class for fields,asynccode and APIs that takeIList<T>,IReadOnlyCollection<T>orIDictionary<TKey,TValue>. Span<T>access to the list and stack contents for zero-copy loops.- Source generators that emit type-specific collections from
[ZeroAllocList(typeof(T))]. See source generators. - Targets
net10.0andnet11.0.
Performance
BenchmarkDotNet, N = 1,000. Time is the same-run ratio against the BCL type; allocations are exact.
| Workload | BCL type | ZeroAlloc time | BCL alloc | ZeroAlloc alloc |
|---|---|---|---|---|
| Add N items | List<int> |
0.63× | 8,424 B | 0 B |
| Push and pop N items | Stack<int> |
0.62× | 8,424 B | 0 B |
| Enqueue and dequeue N items | Queue<int> |
0.79× | 8,432 B | 0 B |
| Write and read N items, capacity known | Queue<int> |
0.47× | 4,064 B | 0 B |
The Heap* classes enumerate with public struct enumerators, so their foreach allocates nothing either. Every workload, with the full numbers: docs/performance.md.
Learn more
- Documentation: collections.zeroalloc.net, including getting started, the cookbook and diagnostics.
- Related packages: ZeroAlloc.Inject and ZeroAlloc.Analyzers.
- Contributing: issues and pull requests are welcome on GitHub.
- CHANGELOG.
License
MIT. See LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- No dependencies.
-
net11.0
- No dependencies.
NuGet packages (16)
Showing the top 5 NuGet packages that depend on ZeroAlloc.Collections:
| Package | Downloads |
|---|---|
|
ZeroAlloc.Rest
Source-generated, AOT-compatible REST client for .NET |
|
|
ZeroAlloc.Scheduling.InMemory
In-memory job store for ZeroAlloc.Scheduling — use in tests. |
|
|
ZeroAlloc.Outbox.InMemory
In-memory store adapter for ZeroAlloc.Outbox (testing). |
|
|
ZeroAlloc.EventSourcing
Source-generated event-sourced aggregates for .NET. Zero-allocation event dispatch, AOT-safe. |
|
|
BlazorNative.Renderer
The BlazorNative renderer: drives Blazor's RenderTree diffing into a native widget tree and emits binary UI patch frames for platform shells. NativeAOT/trim-compatible. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.1 | 0 | 11/22/2026 |
| 2.0.0 | 4 | 11/22/2026 |
| 1.3.1 | 2,777 | 11/17/2026 |
| 1.3.0 | 9,387 | 11/11/2026 |
| 1.2.5 | 14,328 | 10/29/2026 |
| 1.2.4 | 12,376 | 10/18/2026 |
| 1.2.3 | 6,079 | 10/16/2026 |
| 1.2.2 | 2,069 | 10/15/2026 |
| 1.2.1 | 5,949 | 10/11/2026 |
| 1.2.0 | 5,237 | 10/10/2026 |
| 1.1.11 | 5,092 | 9/30/2026 |
| 1.1.10 | 4,751 | 9/28/2026 |
| 1.1.9 | 3,129 | 9/27/2026 |
| 1.1.8 | 4,295 | 9/25/2026 |
| 1.1.7 | 3,128 | 9/20/2026 |
| 1.1.5 | 20,246 | 9/19/2026 |
| 1.1.2 | 11,273 | 5/12/2026 |
| 1.1.1 | 304 | 5/3/2026 |
| 1.1.0 | 191 | 5/1/2026 |