Skip to content

Commit c8713bd

Browse files
committed
perf: zero-copy receive in ConvertToMessage
Reference envelope.Body directly instead of copying via ToArray(). Safe because the body is deserialized synchronously within the awaited subscriber dispatch before the handler returns, per the handler-scoped IMessage.Data validity contract.
1 parent fbad50a commit c8713bd

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/Foundatio.RabbitMQ/Messaging/RabbitMQMessageBus.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,12 @@ await _publisherReady.WaitAsync(cancellationToken)
472472

473473
protected virtual IMessage ConvertToMessage(BasicDeliverEventArgs envelope)
474474
{
475-
var message = new Message(envelope.Body.ToArray(), DeserializeMessageBody)
475+
// Zero-copy: envelope.Body is a pooled buffer that RabbitMQ.Client reclaims once OnMessageAsync
476+
// returns. Referencing it without copying is safe because the body is deserialized synchronously
477+
// within the awaited SendMessageToSubscribersAsync dispatch, before the handler returns. Per the
478+
// IMessage.Data contract, the buffer is only valid for the duration of handling; consumers that
479+
// retain the raw payload beyond the handler must copy it via ToArray().
480+
var message = new Message(envelope.Body, DeserializeMessageBody)
476481
{
477482
Type = envelope.BasicProperties.Type,
478483
ClrType = GetMappedMessageType(envelope.BasicProperties.Type),

0 commit comments

Comments
 (0)