Collections.Pooled 1.0.3.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Collections.Pooled --version 1.0.3.1
NuGet\Install-Package Collections.Pooled -Version 1.0.3.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Collections.Pooled" Version="1.0.3.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Collections.Pooled --version 1.0.3.1
#r "nuget: Collections.Pooled, 1.0.3.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Collections.Pooled as a Cake Addin
#addin nuget:?package=Collections.Pooled&version=1.0.3.1

// Install Collections.Pooled as a Cake Tool
#tool nuget:?package=Collections.Pooled&version=1.0.3.1

Collections.Pooled NuGet Version

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.

Installation

Available on NuGet:

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

Benchmarks

Collections

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:

  • Find and FindLast have become TryFind and TryFindLast, following the standard .NET "try" pattern.
  • The new PooledList<T>.Span property returns a Span<T> over the portion of the internal array store that is populated. This span can be further sliced, or passed into other methods that can read from or write to a span.
    • Enumerating the Span property can be more than twice as fast as enumerating the list itself, but as we are unable to do the normal "collection modified during enumeration" checks, please use with caution.
    • The list is unable to increment its version-checking flag when setting values via the Span. Therefore, "collection modified during enumeration" checks can be bypassed. Please use with caution.
  • GetRange now returns a Span<T>. For example, foo.GetRange(5, 100) is equivalent to foo.Span.Slice(5, 100).
  • CopyTo now takes a Span<T> and doesn't offer any start-index or count parameters. Use the Span property and slicing instead.
  • AddRange and InsertRange can now accept a ReadOnlySpan<T>.
  • The new AddSpan and InsertSpan methods ensure the internal storage has the capacity to add the requested number of items, and return a Span<T> that can be used to write directly to that section of the internal storage. Caveats about "collection modified during enumeration" checks apply here as well.
  • Delegate types such as Predicate<T> and Converter<T1, T2> have been replaced with standard Func<> equivalents.
  • PooledList implements IDisposable. Disposing the list returns the internal array to the ArrayPool. If you forget to dispose the list, nothing will break, but memory allocations and GC pauses will be closer to those of List<T> (you will still benefit from pooling of intermediate arrays as the PooledList is resized).
  • A selection of ToPooledList() extension methods are provided.
Performance

Please review the benchmark links above for complete details. Performance and memory allocations both range from "on par with List<T>" to "far better than List<T>" depending on the operation.

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:

  • New methods include: AddRange, GetOrAdd, AddOrUpdate
  • Both constructors and AddRange can take a sequence of KeyValuePair<TKey, TValue> objects, or a sequence of ValueTuple<TKey, TValue> objects.
  • Significantly reduced memory allocations when adding many items.
  • PooledDictionary implements IDisposable. Disposing the dictionary returns the internal arrays to the ArrayPool. If you forget to dispose the dictionary, nothing will break, but memory allocations and GC pauses will be closer to those of Dictionary<TKey, TValue> (you will still benefit from pooling of intermediate arrays as the PooledDictionary is resized).
  • A selection of ToPooledDictionary() extension methods are provided.

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.

  • Other than the ability to pass Spans into the constructor, there are no API changes compared to the original Stack.
  • Significantly reduced memory allocations when pushing many items.
  • PooledStack implements IDisposable. Disposing the stack returns the internal arrays to the ArrayPool. If you forget to dispose the stack, nothing will break, but memory allocations and GC pauses will be closer to those of Stack<T> (you will still benefit from pooling of intermediate arrays as the PooledStack is resized).
  • A selection of ToPooledStack() extension methods are provided.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (19)

Showing the top 5 NuGet packages that depend on Collections.Pooled:

Package Downloads
CosmosStack.Extensions.Collections

CosmosStack collections extension library

Arch

A high performance c# net.6 and net.7 archetype based ECS ( Entity component system ).

TextControlBox.JuliusKirsch

A fast and powerful textbox with syntaxhighlighting and much more

MoonTools.Core.Graph

A GC-friendly graph theory library for .NET Core intended for use with games.

Dispose.Scope

This library is a class library that allows you to easily use scopes to manage instances of objects that implement the `IDisposable` interface. Register the `IDisposable`s that need to be released into the scope, and then automatically release all registered objects at the end of the scope.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Collections.Pooled:

Repository Stars
genaray/Arch
A high-performance C# based Archetype & Chunks Entity Component System (ECS) with optional multithreading.
mirbeta/OpenMir2
Legend of Mir 2 Game server
Version Downloads Last updated
2.0.0-preview.27 196,402 11/28/2019
2.0.0-preview.26 290 11/13/2019
2.0.0-preview.25 277 11/13/2019
2.0.0-preview.24 462 9/24/2019
2.0.0-preview.23 290 9/5/2019
2.0.0-preview.22 295 9/5/2019
2.0.0-preview.21 286 9/5/2019
2.0.0-preview.20 304 8/14/2019
2.0.0-preview.19 322 8/10/2019
2.0.0-preview.18 320 8/8/2019
2.0.0-preview.16 305 6/23/2019
2.0.0-preview.15 295 6/23/2019
2.0.0-preview.14 300 6/23/2019
2.0.0-preview.13 339 5/19/2019
2.0.0-preview.12 327 5/18/2019
2.0.0-preview.11 311 5/18/2019
2.0.0-preview.10 309 5/18/2019
2.0.0-preview.9 326 5/7/2019
2.0.0-preview.8 330 4/19/2019
2.0.0-preview.7 314 4/8/2019
2.0.0-preview.6 320 4/7/2019
2.0.0-preview.5 304 4/7/2019
2.0.0-preview.4 302 4/7/2019
2.0.0-preview.3 310 4/7/2019
2.0.0-preview.2 1,230 4/7/2019
1.0.82 331,696 4/3/2019
1.0.77 13,403 2/12/2019
1.0.68 662 2/9/2019
1.0.61 669 2/6/2019
1.0.54 687 2/6/2019
1.0.47 656 1/31/2019
1.0.38 665 1/28/2019
1.0.21 643 1/24/2019
1.0.6 681 1/24/2019
1.0.5 677 1/24/2019
1.0.4 691 1/23/2019
1.0.3.1 689 1/22/2019
1.0.3 711 1/21/2019
1.0.2 710 1/19/2019
1.0.1 682 12/25/2018
1.0.0 815 12/24/2018

PooledDictionary bug fix: better releasing of reference types for early GC