Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stream overrides for reading/writing spans to MemoryStream<TOwner> #1052

Open
bill-poole opened this issue Feb 23, 2025 · 0 comments
Open
Labels
feature request 📬 A request for new changes to improve functionality

Comments

@bill-poole
Copy link

bill-poole commented Feb 23, 2025

Overview

The Stream class has default virtual methods for reading/writing Span<T>/ReadOnlySpan<T> and Memory<T>/ReadOnlyMemory<T> that are very inefficient because they were added when Span<T>/ReadOnlySpan<T> and Memory<T>/ReadOnlyMemory<T> were added in .NET Core 2.1. These default methods allocate an array from the array pool, copy data into the array and then invoke the byte[] variant of the read/write method.

The MemoryStream<TOwner> class should be updated to override the Span<T>/ReadOnlySpan<T> and Memory<T>/ReadOnlyMemory<T> variants of the read/write methods to avoid this unnecessary overhead.

API breakdown

namespace CommunityToolkit.HighPerformance.Streams;

internal partial class MemoryStream<TSource> : Stream
    where TSource : struct, ISpanOwner
{
    public override int Read(Span<byte> buffer);

    public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default);

    public override void Write(ReadOnlySpan<byte> buffer);

    public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default);
}

Usage example

These are standard Stream methods that should be overridden. Standard usage for these methods.

Breaking change?

No

Alternatives

No alternatives.

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

@bill-poole bill-poole added the feature request 📬 A request for new changes to improve functionality label Feb 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request 📬 A request for new changes to improve functionality
Projects
None yet
Development

No branches or pull requests

1 participant