-
Notifications
You must be signed in to change notification settings - Fork 10.7k
If not waiting for data, allocate memory before checking anything #22225
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
Changes from all commits
dd71863
ed40eac
9d98835
1fbde96
e864e2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,17 +187,24 @@ private async Task ProcessReceives() | |
| { | ||
| // Resolve `input` PipeWriter via the IDuplexPipe interface prior to loop start for performance. | ||
| var input = Input; | ||
|
|
||
| Memory<byte> buffer = default; | ||
| if (!_waitForData) | ||
| { | ||
| // Ensure we have some reasonable amount of buffer space to start. | ||
| buffer = input.GetMemory(MinAllocBufferSize); | ||
| } | ||
|
|
||
| while (true) | ||
| { | ||
| if (_waitForData) | ||
| { | ||
| // Wait for data before allocating a buffer. | ||
| await _receiver.WaitForDataAsync(); | ||
| // Data ready, allocate some memory to receive it. | ||
| buffer = input.GetMemory(MinAllocBufferSize); | ||
| } | ||
|
|
||
| // Ensure we have some reasonable amount of buffer space | ||
| var buffer = input.GetMemory(MinAllocBufferSize); | ||
|
|
||
| var bytesReceived = await _receiver.ReceiveAsync(buffer); | ||
|
|
||
| if (bytesReceived == 0) | ||
|
|
@@ -209,7 +216,18 @@ private async Task ProcessReceives() | |
|
|
||
| input.Advance(bytesReceived); | ||
|
|
||
| var flushTask = input.FlushAsync(); | ||
| var flushTask = input.FlushAsync(isMoreData: !_waitForData); | ||
| if (!_waitForData) | ||
| { | ||
| if (buffer.Length - bytesReceived >= MinAllocBufferSize) | ||
| { | ||
| buffer = buffer.Slice(bytesReceived); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The skipping of the call to GetMemory() doesn't even look like it should be allowed by the PipeWriter. Is this critical for perf?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its what the api would allow |
||
| } | ||
| else | ||
| { | ||
| buffer = input.GetMemory(MinAllocBufferSize); | ||
| } | ||
| } | ||
|
|
||
| var paused = !flushTask.IsCompleted; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benaadams @davidfowl Any chance we're getting the
isMoreData:API in .NET 5?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No; too soon