Add Memory<byte> and ReadOnlyMemory<byte> constructors to MemoryStream#124990
Draft
Add Memory<byte> and ReadOnlyMemory<byte> constructors to MemoryStream#124990
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-memory |
…m with delegation pattern Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com>
…tructors Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com>
…ternalRead method Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add ReadOnlyMemory and Memory constructors to MemoryStream
Add Memory<byte> and ReadOnlyMemory<byte> constructors to MemoryStream
Feb 27, 2026
jkotas
reviewed
Feb 28, 2026
|
|
||
| private sealed class MemoryMemoryStream | ||
| { | ||
| private readonly ReadOnlyMemory<byte> _memory; |
Member
There was a problem hiding this comment.
Store only ReadOnlyMemory/Memory here and use the rest of the state from the main Stream?
It would reduce the overall instance size and it would make it unnecessary to the switch in some of the properties, e.g. Length or Position.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds
ReadOnlyMemory<byte>andMemory<byte>constructors toMemoryStreamusing a delegation pattern that avoids any performance penalty for existing byte-array code paths.A private inner class
MemoryMemoryStreamholds all Memory-backed state and operations. Areadonlyfield_memoryMemoryStreamis only set by the new constructors — in all existing constructors it remainsnull. Every public/override method checks this field first:New constructors
MemoryStream(ReadOnlyMemory<byte>)— read-only, non-expandableMemoryStream(Memory<byte>)— writable, non-expandableMemoryStream(Memory<byte>, bool writable)— writability controlKey design decisions
BinaryReader.ReadCharsCommonusedInternalGetBuffer()/InternalGetPosition()which returnbyte[]— not viable for arbitraryMemory<byte>. AddedInternalRead(int count)that returnsReadOnlySpan<byte>and works for both paths.MemoryStream(byte[], writable)— fixed capacity,SetLengthwithin capacity is allowed, expansion throwsNotSupportedException.GetBuffer()throwsUnauthorizedAccessException,TryGetBuffer()returnsfalse(no underlyingbyte[]to expose).Files changed
MemoryStream.csInternalReadBinaryReader.csInternalReadinstead ofInternalGetBuffer/InternalGetPositionSystem.Runtime.csMemoryStreamTests.cs💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.