[Feature Proposal] NativeArena for Scoped Native Memory Allocation
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
[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
- Ngôn ngữ chính
- C#
- Star
- 3.8k
- Fork
- 401
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của CommunityToolkit/dotnet
-
RelayCommand<T>.CanExecute(null) returns false for value types without a predicateCó thể đã có người làm @apachezy đã nhận 84 ngày trước. Đang mởbug :bug:
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
CommunityToolkit/dotnet#1206 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
CommunityToolkit/dotnet#1186 ·
-
bug :bug:
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
CommunityToolkit/dotnet#648 ·
-
bug :bug:
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
CommunityToolkit/dotnet#1215 ·
-
Add segmented buffering and sequence reads to `ArrayPoolBufferWriter<T>`Có thể đã có người làm @paulomorgado đã nhận 22 ngày trước. Đang mởfeature request :mailbox_with_mail:
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 45/100
CommunityToolkit/dotnet#1214 ·
Tất cả issue của CommunityToolkit/dotnet
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
qew21/Genshin-Subtitles#45 ·
-
bug priority: P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
space-wizards/RobustToolbox#7158 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Parameterless constructor is not a must-provided by user when a struct has a field initializerĐang mở:watch: Not Triaged dotnet-csharp/svc fundamentals/subsvc
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Wrong values of Kernel32.FileShare.Delete and Kernel32.FileAttributes.PosixSemanticsCó thể đã có người làm @ElectLive đã nhận hôm nay. Đang mởbug good first issue help wanted
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 83/100
ForNeVeR/TruePath#241 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày