Array Pool Scope allows you to use ArrayPool in a scope-like manner.
$ dotnet add package Hertzole.ArrayPoolScopeArray Pool Scope allows you to use ArrayPool in a scope-like manner.
ArrayPool in a scope-like manner using readonly structs with no allocations!¹ 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:
package.openupm.comhttps://package.openupm.comse.hertzole.array-pool-scopeSave (or Apply)+Add package by name... or Add package from git URL...se.hertzole.array-pool-scope into nameAddYou can install the package through the Unity Package Manager.
Window/Package Manager+ in the top left cornerAdd package from git URL...https://github.com/Hertzole/array-pool-scope.git#upmFor 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.