Skip to content

Reduce allocation in DownloadTimeoutStream.ReadAsync caused by avoida…#7035

Merged
martinrrm merged 1 commit intoNuGet:devfrom
nareshjo:downloadTimeoutStream-read-alloc
Jan 9, 2026
Merged

Reduce allocation in DownloadTimeoutStream.ReadAsync caused by avoida…#7035
martinrrm merged 1 commit intoNuGet:devfrom
nareshjo:downloadTimeoutStream-read-alloc

Conversation

@nareshjo
Copy link
Copy Markdown
Contributor

@nareshjo nareshjo commented Jan 8, 2026

This pull request was generated by the VS Perf Rel AI Agent. Please review this AI-generated PR with extra care! For more information, visit our wiki. Please share feedback with TIP Insights

  • Issue: The ReadAsync method in DownloadTimeoutStream.cs allocates a timeout error message string via string.Format() on every single call (line with var timeoutMessage = string.Format(...)), even though this message is only used when a TimeoutException is caught. This can lead to unnecessary allocations. Evidence: The stack trace shows String.FormatHelper being called from DownloadTimeoutStream+<ReadAsync>d__.MoveNext, and the source code confirms string.Format is called unconditionally at the start of every ReadAsync invocation.
    Allocation path: DownloadTimeoutStream.ReadAsyncString.FormatHelperStringBuilder.ToStringTypeAllocated!System.String

  • Issue type: Reduce string allocations

  • Proposed fix: Move the string.Format call from the beginning of the ReadAsync method into the catch (TimeoutException) block. This defers the string allocation to only occur when a timeout exception is actually caught, eliminating allocations on the hot path (normal successful reads).

Best practices wiki
See related failure in PRISM
ADO work item

@nareshjo nareshjo requested a review from a team as a code owner January 8, 2026 21:19
@nareshjo nareshjo requested review from donnie-msft and zivkan January 8, 2026 21:19
@dotnet-policy-service dotnet-policy-service bot added the Community PRs created by someone not in the NuGet team label Jan 8, 2026
@nareshjo
Copy link
Copy Markdown
Contributor Author

nareshjo commented Jan 9, 2026

@zivkan, @martinrrm thanks for reviewing. Would you be able to merge it as I don't seem to have permissions.

@martinrrm martinrrm enabled auto-merge (squash) January 9, 2026 21:38
@martinrrm
Copy link
Copy Markdown
Contributor

@nareshjo Thanks for the contribution, I queued the CI, should be automatically merged if it succeeds.

@martinrrm martinrrm merged commit a0bfa97 into NuGet:dev Jan 9, 2026
17 checks passed
@nareshjo nareshjo deleted the downloadTimeoutStream-read-alloc branch January 10, 2026 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants