不仅仅是直接使用socket来实现组件的这种改进,而且还通过更高级别的组件来间接使用socket,其他PR的结果是更高级别组件(如NetworkStream)的额外性能提升。例如,PR dotnet / corefx#16502在SocketAsyncEventArgs上重新实现了基于Socket的SendAsync和ReceiveAsync操作,并且允许它们在NetworkStream中使用。Read / WriteAsync和PR dotnet / corefx#12664添加了一个专门的CopyToAsync重写,以便更有效地从NetworkStream读取数据并将其复制到其他流中。这些变化对NetworkStream吞吐量和分配有非常大的影响。看看下面这个例子:
using System; using System.Diagnostics; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; class Test { static void Main() => MainAsync().GetAwaiter().GetResult(); static async Task MainAsync() { using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { listener.Bind(new IPEndPoint(IPAddress.Loopback, 0)); listener.Listen(1); Task connectTask = Task.Run(() => client.Connect(listener.LocalEndPoint)); using (Socket server = listener.Accept()) { await connectTask; using (var serverStream = new NetworkStream(server)) using (var clientStream = new NetworkStream(client)) { Task serverCopyAll = serverStream.CopyToAsync(Stream.Null); byte[] data = new byte[1024]; new Random().NextBytes(data); var sw = new Stopwatch(); int gen0 = GC.CollectionCount(0), gen1 = GC.CollectionCount(1), gen2 = GC.CollectionCount(2); sw.Start(); for (int i = 0; i < 1_000_000; i++) { await clientStream.WriteAsync(data, 0, data.Length); } client.Shutdown(SocketShutdown.Send); serverCopyAll.Wait(); sw.Stop(); Console.WriteLine($); } } } } }
Elapsed = 00:00:24.7827947 Gen0 = 220 Gen1 = 3 Gen2 = 0
而在.NET Core 2.0中,时间减少了5倍,垃圾回收有效地减少到零:
Elapsed=00:00:05.6456073 Gen0=74 Gen1=0 Gen2=0