Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary MemoryStream allocation from ByteArrayContent #35005

Merged
merged 1 commit into from Apr 15, 2020

Conversation

stephentoub
Copy link
Member

Constructing a ByteArrayContent (or anything that derives from it, like StringContent) or a ReadOnlyMemoryContent proactively allocates a MemoryStream that it stores as the _bufferedContent on the base HttpContent. But the 99.99% use case for ByteArrayContent is as a request payload, and this _bufferedContent isn't used for that. The only time this would be used is if a developer created a ByteArrayContent and then explicitly got the stream from it, e.g. via GetStreamAsync, in which case we're no worse off by doing the creation lazily. This PR just deletes the unnecessary code / allocation.

Fixes #34642
cc: @scalablecory

Constructing a ByteArrayContent (or anything that derives from it, like StringContent) or a ReadOnlyMemoryContent  proactively allocates a MemoryStream that it stores as the _bufferedContent on the base HttpContent.  But the 99.99% use case for ByteArrayContent is as a request payload, and this _bufferedContent isn't used for that.  The only time this would be used is if a developer created a ByteArrayContent and then explicitly got the stream from it, e.g. via GetStreamAsync, in which case we're no worse off by doing the creation lazily.  This PR just deletes the unnecessary code / allocation.
@ghost
Copy link

ghost commented Apr 15, 2020

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@stephentoub stephentoub added the tenet-performance Performance related issue label Apr 15, 2020
@davidsh davidsh added this to the 5.0 milestone Apr 15, 2020
@stephentoub stephentoub merged commit 5395b14 into dotnet:master Apr 15, 2020
@stephentoub stephentoub deleted the deletememstream branch April 15, 2020 20:52
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Eliminate spurious MemoryStream allocation in ByteArrayContent
4 participants