Reduce allocation in NegotiateStream.Read/Write{Async}#37772
Merged
stephentoub merged 3 commits intodotnet:masterfrom Jun 12, 2020
Merged
Reduce allocation in NegotiateStream.Read/Write{Async}#37772stephentoub merged 3 commits intodotnet:masterfrom
stephentoub merged 3 commits intodotnet:masterfrom
Conversation
Member
stephentoub
commented
Jun 11, 2020
- Each time we call Write{Async}, we're allocating a new byte[] to store the encoded message to be written. Instead, just save the byte[] and reuse it, only allocating a new one if the existing one isn't big enough.
- In both Read/Write{Async}, we call into an EncryptDecryptHelper (on Windows) that allocates a byte[][]. It never has more than three buffers. We can just stack allocate it.
- In my previous change to switch to async/await, I used a default interface method. But when such a method is invoked on a struct implementing the interface, the struct gets boxed. Just move that instance off and avoid several allocations per read.
| Method | Toolchain | Mean | Ratio | Gen 0 | Allocated |
|---|---|---|---|---|---|
| WriteRead | netcore31 | 14.99 ms | 1.00 | 93.7500 | 616001 B |
| WriteRead | master | 13.24 ms | 0.88 | 31.2500 | 200001 B |
| WriteRead | pr | 12.82 ms | 0.85 | - | 1 B |
| ReadWrite | netcore31 | 34.49 ms | 1.00 | 71.4286 | 760230 B |
| ReadWrite | master | 33.30 ms | 0.97 | 62.5000 | 528230 B |
| ReadWrite | pr | 32.76 ms | 0.95 | - | 336240 B |
Hold onto and reuse our write buffer.
The default interface method on the interface results in boxing a struct that implements that interface on each call to the method.
|
Tagging subscribers to this area: @dotnet/ncl |
Member
Author
wfurt
approved these changes
Jun 12, 2020
Member
wfurt
left a comment
There was a problem hiding this comment.
LGTM.
This is essentially what we do in SslStream, right?
Member
Author
|
Yup |
davidsh
approved these changes
Jun 12, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.