@@ -385,8 +385,9 @@ private async Task RepublishMessageWithIncrementedDeliveryCountAsync(BasicDelive
385385 try
386386 {
387387 await EnsureTopicCreatedAsync ( envelope . CancellationToken ) . AnyContext ( ) ;
388- // PERF: ToArray() allocates; PublishMessageAsync takes byte[] until Foundatio base supports ReadOnlyMemory<byte>
389- await PublishMessageAsync ( envelope . Exchange , envelope . RoutingKey , envelope . Body . ToArray ( ) , properties , envelope . CancellationToken ) . AnyContext ( ) ;
388+ // Zero-copy: the publish is awaited within this consumer callback, so envelope.Body stays valid;
389+ // pass the ReadOnlyMemory<byte> straight through to BasicPublishAsync without an intermediate copy.
390+ await PublishMessageAsync ( envelope . Exchange , envelope . RoutingKey , envelope . Body , properties , envelope . CancellationToken ) . AnyContext ( ) ;
390391 await subscriberChannel . BasicAckAsync ( envelope . DeliveryTag , false ) . AnyContext ( ) ;
391392
392393 _logger . LogDebug ( "Republished classic queue message ({MessageId}) (OriginalMessageId={OriginalMessageId}) with delivery count {DeliveryCount}" ,
@@ -431,7 +432,7 @@ private static long GetRetryCountFromHeader(BasicDeliverEventArgs envelope)
431432 return envelope . BasicProperties . MessageId ;
432433 }
433434
434- private async Task PublishMessageAsync ( string exchange , string routingKey , byte [ ] body , BasicProperties properties , CancellationToken cancellationToken )
435+ private async Task PublishMessageAsync ( string exchange , string routingKey , ReadOnlyMemory < byte > body , BasicProperties properties , CancellationToken cancellationToken )
435436 {
436437 await _resiliencePolicy . ExecuteAsync ( async _ =>
437438 {
@@ -473,8 +474,7 @@ await _publisherReady.WaitAsync(cancellationToken)
473474
474475 protected virtual IMessage ConvertToMessage ( BasicDeliverEventArgs envelope )
475476 {
476- // PERF: ToArray() allocates a copy; Message ctor requires byte[] until Foundatio supports ReadOnlyMemory<byte>
477- var message = new Message ( envelope . Body . ToArray ( ) , DeserializeMessageBody )
477+ var message = new Message ( envelope . Body , DeserializeMessageBody )
478478 {
479479 Type = envelope . BasicProperties . Type ,
480480 ClrType = GetMappedMessageType ( envelope . BasicProperties . Type ) ,
0 commit comments