Array Pool Scope allows you to use ArrayPool in a scope-like manner.
- ๐ Fast and Safe - Use
ArrayPool
in a scope-like manner using readonly structs with no allocations! - โ Fully trimmable and AOT compatible - No reflection or dynamic code is used, making it fully compatible with AOT platforms like NativeAOT and IL2CPP!
- ๐ Wide range of .NET support - Supports .NET Standard 1.1 and up, including .NET Framework 4.6.1, .NET 5+ and even Unity!
- โ 100% test coverage - All code is tested to make sure it works as expected!
- ๐ Almost zero allocations - The library is designed to be as allocation-free as possible!ยน
- ๐ Fully documented - Every public member is documented with XML docs to make it easy to understand how to use the library!
ยน Sort(Comparison<T>)
will allocate a small amount of memory in anything below .NET 5 due to the lack of efficient sorting methods.
using Hertzole.Buffers;
int length = 10;
// Use a using statement to automatically return the array to the pool.
using (ArrayPoolScope<int> pool = new ArrayPoolScope<int>(length))
{
// For loop
for (int i = 0; i < pool.Length; i++)
{
pool[i] = i;
Console.WriteLine(pool[i]);
}
// Foreach loop
foreach (int item in pool)
{
Console.WriteLine(item);
}
}
// Manully return the array to the pool.
ArrayPoolScope<int> pool = new ArrayPoolScope<int>(length);
pool.Dispose();
// Provide your own pool.
ArrayPool<int> customPool = ArrayPool<int>.Create();
using ArrayPoolScope<int> pool = new ArrayPoolScope<int>(length, customPool);
// Get directly from pool.
using ArrayPoolScope<int> pool = ArrayPool<int>.Shared.RentScope(length);
// As span and memory.
using ArrayPoolScope<int> pool = ArrayPool<int>.Shared.RentScope(length);
Span<int> span = pool.AsSpan();
Memory<int> memory = pool.AsMemory();
// Control how the array is cleared when disposed.
ArrayClearMode clearMode = ArrayClearMode.Auto; // Auto, Always, Never
using ArrayPoolScope<int> pool = new ArrayPoolScope<int>(length, clearMode);
// Get the array directly if needed. (you should avoid this, unless you really need the array)
using ArrayPoolScope<int> pool = ArrayPool<int>.Shared.RentScope(length);
int[] array = UnsafeArrayPool.GetArray(pool);
You can install the package via NuGet. The package supports .NET Standard 2.0 and up.
dotnet add package Hertzole.ArrayPoolScope
The minimum Unity version for Array Pool Scope is 2021.3.
You can install the package through OpenUPM by using the OpenUPM CLI.
openupm add se.hertzole.array-pool-scope
If you don't have the CLI installed, you can follow these steps:
- Open Edit/Project Settings/Package Manager
- Add a new Scoped Registry (or edit the existing OpenUPM entry)
Name:package.openupm.com
URL:https://package.openupm.com
Scope:se.hertzole.array-pool-scope
- Click
Save
(orApply
) - Open Window/Package Manager
- Click
+
- Select
Add package by name...
orAdd package from git URL...
- Paste
se.hertzole.array-pool-scope
into name - Click
Add
You can install the package through the Unity Package Manager.
- Open
Window/Package Manager
- Click the
+
in the top left corner - Select
Add package from git URL...
- Paste
https://github.com/Hertzole/array-pool-scope.git#upm
For standard .NET development, you need the following:
For Unity development, you need the following:
To build the project, you can use the dotnet
CLI.
dotnet build
To run the tests, you can use the dotnet
CLI.
dotnet test
The main SDK should be the "single source of truth". This means that all code should be written in the main project and then copied over to the Unity project.
To open the project in Unity, you need to open the Unity
folder as a project.
Contributions, issues and feature requests are welcome!
Please make sure your pull requests are made to the develop
branch and that you have tested your changes. If you're adding a new feature, please also add tests for it.
Your code should follow the C# Coding Conventions. Your commits should follow the Conventional Commits standard.