Remove a few unnecessary state machines from SocketsHttpHandler (#56109)
authorStephen Toub <stoub@microsoft.com>
Thu, 22 Jul 2021 15:26:11 +0000 (11:26 -0400)
committerGitHub <noreply@github.com>
Thu, 22 Jul 2021 15:26:11 +0000 (11:26 -0400)
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ChunkedEncodingWriteStream.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ContentLengthWriteStream.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnection.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpContentWriteStream.cs

index 1411e67..9daea15 100644 (file)
@@ -70,12 +70,12 @@ namespace System.Net.Http
                 }
             }
 
-            public override async ValueTask FinishAsync(bool async)
+            public override Task FinishAsync(bool async)
             {
                 // Send 0 byte chunk to indicate end, then final CrLf
                 HttpConnection connection = GetConnectionOrThrow();
                 _connection = null;
-                await connection.WriteBytesAsync(s_finalChunkBytes, async).ConfigureAwait(false);
+                return connection.WriteBytesAsync(s_finalChunkBytes, async);
             }
         }
     }
index 7ea5975..5b7b655 100644 (file)
@@ -39,10 +39,10 @@ namespace System.Net.Http
                 return connection.WriteAsync(buffer, async: true);
             }
 
-            public override ValueTask FinishAsync(bool async)
+            public override Task FinishAsync(bool async)
             {
                 _connection = null;
-                return default;
+                return Task.CompletedTask;
             }
         }
     }
index 7ef8ca0..c781446 100644 (file)
@@ -1788,20 +1788,24 @@ namespace System.Net.Http
             return bytesToCopy;
         }
 
-        private async ValueTask CopyFromBufferAsync(Stream destination, bool async, int count, CancellationToken cancellationToken)
+        private ValueTask CopyFromBufferAsync(Stream destination, bool async, int count, CancellationToken cancellationToken)
         {
             Debug.Assert(count <= _readLength - _readOffset);
 
             if (NetEventSource.Log.IsEnabled()) Trace($"Copying {count} bytes to stream.");
+
+            int offset = _readOffset;
+            _readOffset += count;
+
             if (async)
             {
-                await destination.WriteAsync(new ReadOnlyMemory<byte>(_readBuffer, _readOffset, count), cancellationToken).ConfigureAwait(false);
+                return destination.WriteAsync(new ReadOnlyMemory<byte>(_readBuffer, offset, count), cancellationToken);
             }
             else
             {
-                destination.Write(_readBuffer, _readOffset, count);
+                destination.Write(_readBuffer, offset, count);
+                return default;
             }
-            _readOffset += count;
         }
 
         private Task CopyToUntilEofAsync(Stream destination, bool async, int bufferSize, CancellationToken cancellationToken)
index 5777ac8..825e1f6 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Net.Http
 
             public sealed override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) => throw new NotSupportedException();
 
-            public abstract ValueTask FinishAsync(bool async);
+            public abstract Task FinishAsync(bool async);
         }
     }
 }