jtmueller / Collections.Pooled

Fast, low-allocation ports of List, Dictionary, HashSet, Stack, and Queue using ArrayPool and Span.
MIT License
538 stars 47 forks source link
arraypool dictionary hashset list queue span stack

Collections.Pooled

Master Latest
Build status Build status

This library is based on classes from System.Collections.Generic that have been altered to take advantage of the new System.Span<T> and System.Buffers.ArrayPool<T> libraries to minimize memory allocations, improve performance, and/or allow greater interoperablity with modern API's.

Collections.Pooled supports both .NET Standard 2.0 (.NET Framework 4.6.1+) as well as an optimized build for .NET Core 2.1+. An extensive set of unit tests and benchmarks have been ported from corefx.

Total tests: 27501. Passed: 27501. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 9.9019 Seconds

Installation

NuGet Version

Install-Package Collections.Pooled
dotnet add package Collections.Pooled
paket add Collections.Pooled

Benchmarks

PooledList<T>

PooledList<T> is based on the corefx source code for System.Collections.Generic.List<T>, modified to use ArrayPool for internal array-storage allocation, and to support Span<T>.

There are some API changes worth noting:

Performance

Adding items to a list is one area where ArrayPool helps us quite a bit: List Add Benchmarks

PooledDictionary<TKey, TValue>

PooledDictionary<TKey, TValue> is based on the corefx source code for System.Collections.Generic.Dictionary<TKey, TValue>, modified to use ArrayPool for internal storage allocation, and to support Span<T>.

There are some API changes worth noting:

Performance

Adding to dictionaries is where using ArrayPool really has an impact: Dictionary Add Benchmarks

PooledSet<T>

PooledSet<T> is based on the corefx source code for System.Generic.Collections.HashSet<T>, modified to use ArrayPool for internal storage allocation, and to support ReadOnlySpan<T> for all set functions.

Performance

Here's what pooling does for us when adding to a PooledSet.

Set Add Benchmarks

PooledStack<T>

PooledStack<T> is based on the corefx source code for System.Generic.Collections.Stack<T>, modified to use ArrayPool for internal storage allocation.

Performance

Once again, pushing to a stack shows off some of the advantages of using ArrayPool: Stack Push Benchmarks

PooledQueue<T>

PooledQueue<T> is based on the corefx source code for System.Generic.Collections.Queue<T>, modified to use ArrayPool for internal storage allocation.

Performance

Queue Enqueue Benchmarks