Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Add segmented buffering and sequence reads to `ArrayPoolBufferWriter<T>`

Đang mở
#1,214 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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
45/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
csharp
Lĩnh vực
backend, performance

Hướng nghiên cứu

Start by inspecting the existing ArrayPoolBufferWriter implementation, especially GetSpan(), GetMemory(), WrittenMemory, WrittenSpan, DangerousGetArray(), Clear(), and Dispose(). Compare the proposed segmented behavior with the feature/segmented-arraypoolbufferwriter branch. Done means adding GetReadOnlySequence(), retaining completed pooled arrays, consolidating contiguous views on demand, and returning all arrays during cleanup.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

feature request :mailbox_with_mail:
Overview

ArrayPoolBufferWriter<T> currently copies all written data into a new pooled array whenever it grows. With multiple writes and an unknown final size, those repeated copies consume memory bandwidth. Retaining completed arrays as segments would avoid copying earlier writes on each growth and let callers read the result as a ReadOnlySequence<T> without flattening it. Callers that need a contiguous buffer could still use the existing APIs, which would consolidate on demand.

API breakdown
namespace CommunityToolkit.HighPerformance.Buffers;

public sealed class ArrayPoolBufferWriter<T> : IBuffer<T>, IMemoryOwner<T>
{
    public ReadOnlySequence<T> GetReadOnlySequence();
}

Existing public signatures remain unchanged. GetSpan() and GetMemory() write to the active segment; WrittenMemory, WrittenSpan, and DangerousGetArray() provide contiguous data through on-demand consolidation. Clear() and Dispose() return retained arrays to the pool. A returned sequence is valid only until the writer is modified, consolidated, cleared, or disposed.

Usage example
using ArrayPoolBufferWriter<byte> writer = new();

foreach (ReadOnlyMemory<byte> chunk in chunks)
{
    chunk.Span.CopyTo(writer.GetSpan(chunk.Length));
    writer.Advance(chunk.Length);
}

// Consume the segments while the writer still owns their pooled arrays.
foreach (ReadOnlyMemory<byte> segment in writer.GetReadOnlySequence())
{
    destination.Write(segment.Span);
}
Breaking change?

I'm not sure

Alternatives

Keep the current contiguous writer and pay the copy cost on growth, or use Microsoft.IO.RecyclableMemoryStream where its MemoryStream-like API and pooling features are needed.

Additional context

This may be slower for some workloads. Segmentation adds buffer rents and metadata, while requesting a contiguous view still requires a copy. Small payloads and code that frequently accesses WrittenMemory or WrittenSpan should be benchmarked alongside append-heavy workloads.

Combined with other CommunityToolkit.HighPerformance memory and stream helpers, this might be a good alternative to Microsoft.IO.RecyclableMemoryStream for some append-and-read workloads. It would not be a drop-in replacement for that library’s seekable stream behavior, configurable pooling, or diagnostics. A proposed implementation is in feature/segmented-arraypoolbufferwriter.

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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của CommunityToolkit/dotnet

Tất cả issue của CommunityToolkit/dotnet

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.