Skip to content

Align stream wrappers with the dotnet 11 implementation (follow up to #562)#563

Merged
SimonCropp merged 2 commits into
mainfrom
Align-stream-wrappers-with-the-dotnet-11-implementation-(follow-up-to-#562)
Jun 27, 2026
Merged

Align stream wrappers with the dotnet 11 implementation (follow up to #562)#563
SimonCropp merged 2 commits into
mainfrom
Align-stream-wrappers-with-the-dotnet-11-implementation-(follow-up-to-#562)

Conversation

@SimonCropp

Copy link
Copy Markdown
Owner

No description provided.

…562)

StringStream and ReadOnlySequenceStream diverged from dotnet/runtime#126669
in ways that hurt performance on the cases those types exist for. Bring them
in line and port the PR's tests.

StringStream
- Encode incrementally via a stateful Encoder (fast-path single-shot, spillover
  buffer for sub-scalar reads, final flush) instead of encoding the whole text
  into one buffer on first read. Peak memory is now bounded by the caller's read
  buffer rather than the full encoded length, matching the BCL.
- Span-based Encoder.Convert/Encoding.GetBytes on net core 2.1 / netstandard 2.1+,
  array-based equivalents on older TFMs.

ReadOnlySequenceStream
- Track a SequencePosition cursor alongside the absolute position so Read/ReadByte
  advance from the current segment instead of re-slicing from the start every call
  (was O(segments^2) for multi-segment sequences). Seek/Position reposition the
  cursor forward from the current spot, walking from the start only for back jumps.
- Override CopyTo/CopyToAsync to write segments directly to the destination.
  CopyTo is gated to net core 2.1 / netstandard 2.1+ (Stream.CopyTo(Stream, int)
  is not virtual on older TFMs); CopyToAsync applies everywhere.

ReadOnlyMemoryStream / WritableMemoryStream are unchanged: they are already at
parity for array-backed memory, and matching the BCL for native (non-array)
memory would require MemoryStream internals unavailable outside CoreLib.

Tests
- Port the explicit unit tests from the PR (StringStream encodings, surrogate
  pairs, chunk boundaries, GetMaxByteCount overflow guard, memory-slice/char-array
  ctors; WritableMemoryStream capacity/seek/overwrite cases) into the TUnit suite,
  plus ReadOnlySequenceStream CopyTo/CopyToAsync coverage.

Regenerated Split + assemblySize; public API surface unchanged. Verified across
all 22 TFMs (Consume) and passing on net10.0 and net48.
@SimonCropp SimonCropp added this to the 10.11.1 milestone Jun 27, 2026
@SimonCropp SimonCropp merged commit e479ffc into main Jun 27, 2026
4 of 6 checks passed
@SimonCropp SimonCropp deleted the Align-stream-wrappers-with-the-dotnet-11-implementation-(follow-up-to-#562) branch June 27, 2026 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant