[Feature Proposal] NativeArena for Scoped Native Memory Allocation
还没有人认领这个 Issue。
评估
调研方向
The proposal names the CommunityToolkit.HighPerformance.Buffers namespace and provides a prototype with unit tests, NativeBlockPool, and BenchmarkDotNet benchmarks. Start by reviewing that prototype and the existing buffer APIs in the repository. Done means the proposed NativeArena API is implemented with tests covering allocation, alignment, reset, and disposal behavior.
由索引模型根据 Issue 内容生成。
描述
[Feature Proposal] NativeArena for Scoped Native Memory Allocation
Overview
CommunityToolkit.HighPerformance provides specialized high-throughput memory and buffer primitives for scenarios where standard BCL abstractions are either too general or impose GC/allocation overhead.
In high-performance workloads—such as document parsing, serialization, game loops, native interop, and protocol decoding—applications frequently need to allocate many small unmanaged data structures or scratch buffers whose lifetimes are strictly bounded to a single operation (e.g., per-request or per-frame).
Currently, developers using NativeMemory.Alloc / Free incur significant OS heap allocation overhead for each individual buffer and must track the disposal of every pointer manually. ArrayPool<T> mitigates managed GC overhead, but is restricted to managed arrays, requires individual return calls, and does not provide native pointer semantics or memory alignment guarantees.
We propose introducing NativeArena into CommunityToolkit.HighPerformance.Buffers. It is a fast, thread-confined chunked bump allocator that allows multiple unmanaged allocations to share a single lifetime, released in bulk upon disposal or reused with zero allocations via Reset().
API breakdown
namespace CommunityToolkit.HighPerformance.Buffers;
/// <summary>
/// Configuration options for <see cref="NativeArena"/>.
/// </summary>
public sealed class NativeArenaOptions
{
public nuint ChunkSize { get; init; } = 64 * 1024;
public nuint DefaultAlignment { get; init; } = 16;
public bool ZeroInitialize { get; init; } = false;
public NativeBlockPool? BlockPool { get; init; }
public static NativeArenaOptions Default { get; }
public static NativeArenaOptions Pooled { get; }
}
/// <summary>
/// A fast, scoped bump allocator for native memory allocations sharing a single lifetime.
/// </summary>
public sealed unsafe class NativeArena : IDisposable
{
public NativeArena(NativeArenaOptions? options = null);
public static NativeArena Create(NativeArenaOptions? options = null);
// Raw pointer allocation
public void* Allocate(nuint byteCount, nuint alignment = 0);
// Type-safe allocation helpers
public ref T Allocate<T>() where T : unmanaged;
public Span<T> AllocateSpan<T>(int count) where T : unmanaged;
// Zero-allocation reuse
public void Reset();
// Bulk cleanup
public void Dispose();
}
Usage example
1. Basic Scoped Native Scratchpad
using var arena = NativeArena.Create();
// Fast bump allocation with guaranteed alignment
Span<int> indices = arena.AllocateSpan<int>(1024);
ref Matrix4x4 matrix = ref arena.Allocate<Matrix4x4>();
// All native memory is released at the end of the using scope
2. Per-Frame / Per-Request Reusable Arena
// Retain the arena across iterations without re-allocating chunks
using var arena = NativeArena.Create(NativeArenaOptions.Pooled);
while (server.TryReadPacket(out var packet))
{
arena.Reset(); // Rewinds offsets to 0 with 0 OS allocations
void* header = arena.Allocate(64);
Span<byte> payload = arena.AllocateSpan<byte>(packet.Length);
packet.CopyTo(payload);
ProcessNativePacket(header, payload);
}
Breaking change?
No, this is a purely additive API to CommunityToolkit.HighPerformance.
Alternatives
NativeMemory.Alloc/NativeMemory.Free: Each allocation requires an individual OS allocator roundtrip and explicit tracking/freeing.ArrayPool<T>: Only manages managed arrays (T[]), does not support unmanaged pointers without pinning, lacks custom alignment controls, and requires returning each rented buffer individually.stackalloc: Useful for tiny allocations, but can cause stack overflows for larger or dynamic payloads, and cannot outlive the immediate method frame.- Custom per-project allocators: Many game engines and native wrappers currently write custom bump allocators in C#.
Additional context
- Discussion History: This was previously discussed as a potential BCL candidate in dotnet/runtime#135986. The runtime team noted that because arena allocators depend heavily on workload-specific policies (single-threaded vs. concurrent, chunk sizing, and chunk pooling strategies), it is far better suited for a high-performance utility library like
CommunityToolkit.HighPerformancewhere developers opt into specific allocation characteristics. - Prototype Available: A working prototype featuring unit tests, configurable chunk sizing, block pooling (
NativeBlockPool), and BenchmarkDotNet benchmarks is available and ready for review.
Help us help you
Yes, I'd like to be assigned to work on this item
- 主要语言
- C#
- 星标
- 3.8k
- 派生
- 401
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
CommunityToolkit/dotnet 的其他 Issue
-
RelayCommand<T>.CanExecute(null) returns false for value types without a predicate可能已有人在做 @apachezy 于 86 天前认领。 未关闭bug :bug:
难度 2/5 1-3 小时 新手友好度 84/100
CommunityToolkit/dotnet#1206 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
CommunityToolkit/dotnet#1186 ·
-
bug :bug:
难度 1/5 1 小时以内 新手友好度 68/100
CommunityToolkit/dotnet#648 ·
-
bug :bug:
难度 3/5 1-2 天 新手友好度 68/100
CommunityToolkit/dotnet#1215 ·
-
Add segmented buffering and sequence reads to `ArrayPoolBufferWriter<T>`可能已有人在做 @paulomorgado 于 23 天前认领。 未关闭feature request :mailbox_with_mail:
难度 5/5 一周以上 新手友好度 45/100
CommunityToolkit/dotnet#1214 ·
查看 CommunityToolkit/dotnet 的全部 Issue
相似的 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 65/100
KnifeLemon/Filee#294 ·
维护者通常 1 天内回复
-
mtg-decide submission
难度 2/5 1-3 小时 新手友好度 66/100
Fialuxe/MouthOfGod_System#288 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
gavin-wolfe/wolfden#104 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
-
[BUG] Still libmicrohttpd dependency is not removed properly from External-Dependency-Info.txt file可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭type/bug
难度 1/5 1 小时以内 新手友好度 72/100
维护者通常 1 天内回复