This repository was archived by the owner on Nov 20, 2018. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 191
Fast-path async in HttpResponseStreamWriter #1033
Closed
benaadams wants to merge 4 commits into aspnet:release/2.2 from benaadams:HttpResponseStreamWriter-async-fastpath
Closed
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -3,7 +3,9 @@ | |
| | ||
| using System; | ||
| using System.Buffers; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| | ||
| | @@ -150,34 +152,66 @@ public override void Write(string value) | |
| } | ||
| } | ||
| | ||
| public override async Task WriteAsync(char value) | ||
| public override Task WriteAsync(char value) | ||
| { | ||
| if (_disposed) | ||
| { | ||
| throw new ObjectDisposedException(nameof(HttpResponseStreamWriter)); | ||
| return GetObjectDisposedTask(); | ||
| } | ||
| | ||
| if (_charBufferCount == _charBufferSize) | ||
| { | ||
| await FlushInternalAsync(flushEncoder: false); | ||
| return WriteAsyncAwaited(value); | ||
| } | ||
| else | ||
| { | ||
| // Enough room in buffer, no need to go async | ||
| _charBuffer[_charBufferCount] = value; | ||
| _charBufferCount++; | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| | ||
| private async Task WriteAsyncAwaited(char value) | ||
| { | ||
| Debug.Assert(_charBufferCount == _charBufferSize); | ||
| | ||
| await FlushInternalAsync(flushEncoder: false); | ||
| | ||
| _charBuffer[_charBufferCount] = value; | ||
| _charBufferCount++; | ||
| } | ||
| | ||
| public override async Task WriteAsync(char[] values, int index, int count) | ||
| public override Task WriteAsync(char[] values, int index, int count) | ||
| { | ||
| if (_disposed) | ||
| { | ||
| throw new ObjectDisposedException(nameof(HttpResponseStreamWriter)); | ||
| return GetObjectDisposedTask(); | ||
| } | ||
| | ||
| if (values == null) | ||
| if (values == null || count == 0) | ||
| { | ||
| return; | ||
| return Task.CompletedTask; | ||
| } | ||
| | ||
| var remaining = _charBufferSize - _charBufferCount; | ||
| if (remaining >= count) | ||
| { | ||
| // Enough room in buffer, no need to go async | ||
| CopyToCharBuffer(values, ref index, ref count); | ||
| return Task.CompletedTask; | ||
| } | ||
| else | ||
| { | ||
| return WriteAsyncAwaited(values, index, count); | ||
| } | ||
| } | ||
| | ||
| private async Task WriteAsyncAwaited(char[] values, int index, int count) | ||
| { | ||
| Debug.Assert(count > 0); | ||
| Debug.Assert(_charBufferSize - _charBufferCount > count); | ||
| | ||
| while (count > 0) | ||
| { | ||
| if (_charBufferCount == _charBufferSize) | ||
| | @@ -186,22 +220,48 @@ public override async Task WriteAsync(char[] values, int index, int count) | |
| } | ||
| | ||
| CopyToCharBuffer(values, ref index, ref count); | ||
| Debug.Assert(count == 0); | ||
| } | ||
| } | ||
| | ||
| public override async Task WriteAsync(string value) | ||
| public override Task WriteAsync(string value) | ||
| { | ||
| if (_disposed) | ||
| { | ||
| throw new ObjectDisposedException(nameof(HttpResponseStreamWriter)); | ||
| return GetObjectDisposedTask(); | ||
| } | ||
| | ||
| if (value == null) | ||
| { | ||
| return; | ||
| return Task.CompletedTask; | ||
| } | ||
| | ||
| var count = value.Length; | ||
| if (count == 0) | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
| 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. Is it worth folding this together with L234? Contributor 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. Done | ||
| | ||
| var remaining = _charBufferSize - _charBufferCount; | ||
| if (remaining >= count) | ||
| { | ||
| // Enough room in buffer, no need to go async | ||
| CopyToCharBuffer(value); | ||
| return Task.CompletedTask; | ||
| } | ||
| else | ||
| { | ||
| return WriteAsyncAwaited(value); | ||
| } | ||
| } | ||
| | ||
| private async Task WriteAsyncAwaited(string value) | ||
| { | ||
| var count = value.Length; | ||
| | ||
| Debug.Assert(count > 0); | ||
| Debug.Assert(_charBufferSize - _charBufferCount < count); | ||
| | ||
| var index = 0; | ||
| while (count > 0) | ||
| { | ||
| | @@ -231,7 +291,7 @@ public override Task FlushAsync() | |
| { | ||
| if (_disposed) | ||
| { | ||
| throw new ObjectDisposedException(nameof(HttpResponseStreamWriter)); | ||
| return GetObjectDisposedTask(); | ||
| } | ||
| | ||
| return FlushInternalAsync(flushEncoder: true); | ||
| | @@ -306,6 +366,19 @@ private async Task FlushInternalAsync(bool flushEncoder) | |
| } | ||
| } | ||
| | ||
| private void CopyToCharBuffer(string value) | ||
| { | ||
| Debug.Assert(_charBufferSize - _charBufferCount >= value.Length); | ||
| | ||
| value.CopyTo( | ||
| sourceIndex: 0, | ||
| destination: _charBuffer, | ||
| destinationIndex: _charBufferCount, | ||
| count: value.Length); | ||
| | ||
| _charBufferCount += value.Length; | ||
| } | ||
| | ||
| private void CopyToCharBuffer(string value, ref int index, ref int count) | ||
| { | ||
| var remaining = Math.Min(_charBufferSize - _charBufferCount, count); | ||
| | @@ -336,5 +409,11 @@ private void CopyToCharBuffer(char[] values, ref int index, ref int count) | |
| index += remaining; | ||
| count -= remaining; | ||
| } | ||
| | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static Task GetObjectDisposedTask() | ||
| { | ||
| return Task.FromException(new ObjectDisposedException(nameof(HttpResponseStreamWriter))); | ||
| } | ||
| } | ||
| } | ||
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.
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.
This would now throw if
valueisnull.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.
fixed