diff --git a/Benchmarking/Benchmark/Benchmark.csproj b/Benchmarking/Benchmark/Benchmark.csproj index 43c2aab..f9f1474 100644 --- a/Benchmarking/Benchmark/Benchmark.csproj +++ b/Benchmarking/Benchmark/Benchmark.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/Benchmarking/EchoServiceCapnp/EchoServiceCapnp.csproj b/Benchmarking/EchoServiceCapnp/EchoServiceCapnp.csproj index 3b41a29..5954408 100644 --- a/Benchmarking/EchoServiceCapnp/EchoServiceCapnp.csproj +++ b/Benchmarking/EchoServiceCapnp/EchoServiceCapnp.csproj @@ -6,7 +6,7 @@ - + diff --git a/Capnp.Net.Runtime/Util/AsyncNetworkStreamAdapter.cs b/Capnp.Net.Runtime/Util/AsyncNetworkStreamAdapter.cs index cfd255a..9c9481e 100644 --- a/Capnp.Net.Runtime/Util/AsyncNetworkStreamAdapter.cs +++ b/Capnp.Net.Runtime/Util/AsyncNetworkStreamAdapter.cs @@ -7,15 +7,24 @@ namespace Capnp.Util { internal class AsyncNetworkStreamAdapter : Stream { - readonly NetworkStream _stream; - readonly object _reentrancyBlocker = new object(); - //Exception? _bufferedException; + // Async I/O pays off for large payloads. Perf. profiling gave a threshold around 200kB + const int DefaultAsyncThreshold = 200000; - public AsyncNetworkStreamAdapter(Stream stream) + readonly NetworkStream _stream; + readonly int _asyncThreshold; + readonly object _reentrancyBlocker = new object(); + Exception? _bufferedException; + + public AsyncNetworkStreamAdapter(Stream stream, int asyncThreshold) { + _asyncThreshold = asyncThreshold; _stream = stream as NetworkStream ?? throw new ArgumentException("stream argument must be a NetworkStream"); } + public AsyncNetworkStreamAdapter(Stream stream): this(stream, DefaultAsyncThreshold) + { + } + public override bool CanRead => true; public override bool CanSeek => false; @@ -32,7 +41,8 @@ namespace Capnp.Util public override void Flush() { - _stream.Flush(); + _stream.FlushAsync(); + //_stream.Flush(); } public override int Read(byte[] buffer, int offset, int count) @@ -73,7 +83,10 @@ namespace Capnp.Util // throw exception; //} - //_stream.BeginWrite(buffer, offset, count, WriteCallback, null); + //if (count >= _asyncThreshold) + // _stream.BeginWrite(buffer, offset, count, WriteCallback, null); + //else + // _stream.Write(buffer, offset, count); } protected override void Dispose(bool disposing)