Remove a field from the CopyToAsync async state machine (dotnet/coreclr#7157)
authorStephen Toub <stoub@microsoft.com>
Tue, 13 Sep 2016 16:36:44 +0000 (12:36 -0400)
committerJan Kotas <jkotas@microsoft.com>
Tue, 13 Sep 2016 16:36:44 +0000 (09:36 -0700)
The compiler is lifting the local ```bytesRead``` into a field on the async state machine, even though its value need not be preserved across any await.

Commit migrated from https://github.com/dotnet/coreclr/commit/6f399b187eb286b2b682fe9efaad531495770638

src/coreclr/src/mscorlib/src/System/IO/Stream.cs

index a1f2936..7b7b0d3 100644 (file)
@@ -176,9 +176,10 @@ namespace System.IO {
             Contract.Requires(destination.CanWrite);
 
             byte[] buffer = new byte[bufferSize];
-            int bytesRead;
-            while ((bytesRead = await ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
+            while (true)
             {
+                int bytesRead = await ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
+                if (bytesRead == 0) break;
                 await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
             }
         }