Skip to content

Commit

Permalink
Write the entire packet in SendData() synchronously.
Browse files Browse the repository at this point in the history
This doesn't seem to regress performance and feels safer since we're now reusing the underlying byte array.

We're also saving on not allocating the Overlapped and NativeOverlapped data structures and various other async overhead.
  • Loading branch information
KirillOsenkov committed Jan 18, 2021
1 parent 6eb445d commit dc5f1fb
Showing 1 changed file with 0 additions and 28 deletions.
Expand Up @@ -766,13 +766,7 @@ public void SendData(INodePacket packet)
int lengthToWrite = Math.Min(writeStreamLength - i, MaxPacketWriteSize);
if (writeStreamLength - i <= MaxPacketWriteSize)
{
// We are done, write the last bit asynchronously. This is actually the general case for
// most packets in the build, and the asynchronous behavior here is desirable.
#if FEATURE_APM
_serverToClientStream.BeginWrite(writeStreamBuffer, i, lengthToWrite, PacketWriteComplete, null);
#else
_serverToClientStream.Write(writeStreamBuffer, i, lengthToWrite);
#endif
return;
}
else
Expand All @@ -781,12 +775,7 @@ public void SendData(INodePacket packet)
// return out of this function and let the rest of the system continue because another operation
// might want to send data immediately afterward, and that could result in overlapping writes
// to the pipe on different threads.
#if FEATURE_APM
IAsyncResult result = _serverToClientStream.BeginWrite(writeStreamBuffer, i, lengthToWrite, null, null);
_serverToClientStream.EndWrite(result);
#else
_serverToClientStream.Write(writeStreamBuffer, i, lengthToWrite);
#endif
}
}
}
Expand Down Expand Up @@ -814,23 +803,6 @@ public void Close()
_terminateDelegate(_nodeId);
}

#if FEATURE_APM
/// <summary>
/// Completes the asynchronous packet write to the node.
/// </summary>
private void PacketWriteComplete(IAsyncResult result)
{
try
{
_serverToClientStream.EndWrite(result);
}
catch (IOException)
{
// Do nothing here because any exception will be caught by the async read handler
}
}
#endif

private bool ProcessHeaderBytesRead(int bytesRead)
{
if (bytesRead != _headerByte.Length)
Expand Down

0 comments on commit dc5f1fb

Please sign in to comment.