perf(serialization): grow memory stream buffers geometrically

Merged
RE
2mo ago
+191-17

Problem

MemoryStreamBufferWriter expanded capacity by only the current size hint and set the stream length to the allocated capacity. Incremental serialization could therefore repeatedly reallocate and copy the entire backing buffer, producing potentially quadratic work for large payloads while also including unwritten capacity in the logical stream length.

Solution

Grow expandable memory streams geometrically while preserving the requested contiguous capacity. Commit only written bytes to the logical stream length, preserve overwrite and gap semantics, account for non-zero array origins, and retain usable capacity on fixed exposed streams.

Focused regression coverage exercises geometric growth, overwrite and append behavior, gaps, fixed and read-only exposed streams, non-zero origins, invalid advances, and size-hint validation.

Microsoft Reviewers: Open in CodeFlow