Performance optimization of SendPacketAsync (#1624)

* performance optimization of SendPacketAsync

* Update Source/MQTTnet.AspnetCore/MqttConnectionContext.cs

Co-authored-by: Günther Foidl <gue@korporal.at>

Co-authored-by: Günther Foidl <gue@korporal.at>
This commit is contained in:
老九
2022-12-16 17:13:11 +01:00
committed by GitHub
co-authored by Günther Foidl
parent 7d5e49b44c
commit 8a926a3db3
2 changed files with 79 additions and 2 deletions
@@ -211,8 +211,17 @@ namespace MQTTnet.AspNetCore
{
var buffer = PacketFormatterAdapter.Encode(packet);
WritePacketBuffer(_output, buffer);
await _output.FlushAsync(cancellationToken).ConfigureAwait(false);
if (buffer.Payload.Count == 0)
{
// zero copy
// https://github.com/dotnet/runtime/blob/e31ddfdc4f574b26231233dc10c9a9c402f40590/src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/StreamPipeWriter.cs#L279
await _output.WriteAsync(buffer.Packet, cancellationToken).ConfigureAwait(false);
}
else
{
WritePacketBuffer(_output, buffer);
await _output.FlushAsync(cancellationToken).ConfigureAwait(false);
}
BytesSent += buffer.Length;
}