Skip to content

Make FileBufferingReadStream lazily rent buffer - #10220

Merged
wtgodbe merged 1 commit into
mainfrom
wtgodbe/port-587ebd3-main
Oct 14, 2026
Merged

wtgodbe merged 1 commit into
mainfrom
wtgodbe/port-587ebd3-main

Conversation

@wtgodbe

@wtgodbe wtgodbe commented Oct 14, 2026

Copy link
Copy Markdown
Member

FileBufferingReadStream before was pre-allocating buffer in constructor. That buffer may be much bigger than incoming data, and its better to allocate only on the input received (with approximately same size of buffer). If going over the limit, changes to FileStream.

…ent buffer

FileBufferingReadStream before was pre-allocating buffer in constructor. That buffer may be much bigger than incoming data, and its better to allocate only on the input received (with approximately same size of buffer). If going over the limit, changes to `FileStream`.

----
#### AI description  (iteration 1)
#### PR Classification
Performance optimization to implement lazy buffer allocation in `FileBufferingReadStream`.

#### PR Summary
This PR optimizes memory usage by introducing a new `PooledMemoryStream` class that lazily rents buffers from `ArrayPool<byte>` only when capacity is needed, and integrates it into `FileBufferingReadStream` to defer memory allocation until data is actually written.

- `PooledMemoryStream.cs`: New sealed stream implementation that defers buffer rental from `ArrayPool<byte>` until first write operation, grows geometrically up to a maximum capacity, and exposes `WrittenMemory` property for efficient content access
- `FileBufferingReadStream.cs`: Simplified buffer-to-file migration logic by leveraging `PooledMemoryStream.WrittenMemory` to directly copy buffered content when spilling to disk
- `PooledMemoryStreamTests.cs`: Comprehensive test coverage for lazy rental behavior, buffer growth, read/write operations, disposal semantics, and boundary validation
- `FileBufferingReadStreamTests.cs`: Added tests verifying lazy buffer rental and proper handling of empty content without allocating buffers
<!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->

Co-authored-by: Copilot <[email protected]>
@wtgodbe
wtgodbe requested review from DeagleGross and a balanced review from Copilot October 14, 2026 16:10
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The implementation is coherent, preserves spillover behavior, and has comprehensive focused coverage.

0 open findings

What changed in this PR

Introduces lazy, pooled in-memory buffering for FileBufferingReadStream, reducing unnecessary upfront allocations while preserving file spillover behavior.

Changes:

  • Adds a geometrically growing PooledMemoryStream.
  • Integrates it for thresholds up to 1 MB.
  • Adds unit and integration coverage for allocation, streaming, disposal, and spill behavior.
File Description
src/​aspnetcore/​src/​Shared/​PooledMemoryStream.cs Implements the lazy pooled stream.
src/​aspnetcore/​src/​Shared/​test/​Shared.Tests/​PooledMemoryStreamTests.cs Tests stream behavior and pool ownership.
src/​aspnetcore/​src/​Shared/​test/​Shared.Tests/​Microsoft.AspNetCore.Shared.Tests.csproj Includes the shared stream in tests.
src/​aspnetcore/​src/​Http/​WebUtilities/​src/​FileBufferingReadStream.cs Uses lazy pooled buffering and simplifies spill copying.
src/​aspnetcore/​src/​Http/​WebUtilities/​src/​Microsoft.AspNetCore.WebUtilities.csproj Includes the new shared implementation.
src/​aspnetcore/​src/​Http/​WebUtilities/​test/​FileBufferingReadStreamTests.cs Verifies lazy allocation and growth.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@wtgodbe
wtgodbe merged commit ab1ca5c into main Oct 14, 2026
12 checks passed
@wtgodbe
wtgodbe deleted the wtgodbe/port-587ebd3-main branch October 14, 2026 21:52
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.

3 participants