Skip to content

Commit 731bb3c

Browse files
committed
perf: use ReadOnlyMemory<byte> on republish path; update receive-path comment
1 parent bd7802d commit 731bb3c

4 files changed

Lines changed: 11 additions & 10 deletions

File tree

src/Foundatio.RabbitMQ/Messaging/RabbitMQMessageBus.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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),

tests/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<NoWarn>$(NoWarn);CS1591;NU1701</NoWarn>
88
</PropertyGroup>
99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.6.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.7.0" />
1111
<PackageReference Include="xunit.v3.mtp-v2" Version="3.2.2" />
1212
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
1313
<PackageReference Include="GitHubActionsTestLogger" Version="3.0.4" PrivateAssets="All" />

tests/Foundatio.RabbitMQ.AppHost/Foundatio.RabbitMQ.AppHost.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Aspire.AppHost.Sdk/13.4.5">
1+
<Project Sdk="Aspire.AppHost.Sdk/13.4.6">
22

33
<PropertyGroup>
44
<TargetFramework>net10.0</TargetFramework>
@@ -7,11 +7,11 @@
77
<Nullable>enable</Nullable>
88
<ImplicitUsings>enable</ImplicitUsings>
99
<LangVersion>latest</LangVersion>
10-
<NoWarn>$(NoWarn);CS1591</NoWarn>
10+
<NoWarn>$(NoWarn);CS1591;NU1903</NoWarn>
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Aspire.Hosting.RabbitMQ" Version="13.4.5" />
14+
<PackageReference Include="Aspire.Hosting.RabbitMQ" Version="13.4.6" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

tests/Foundatio.RabbitMQ.Tests/Foundatio.RabbitMQ.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<ProjectReference Include="..\Foundatio.RabbitMQ.AppHost\Foundatio.RabbitMQ.AppHost.csproj" IsAspireProjectResource="false" />
55
</ItemGroup>
66
<ItemGroup>
7-
<PackageReference Include="Aspire.Hosting.Testing" Version="13.4.5" />
7+
<PackageReference Include="Aspire.Hosting.Testing" Version="13.4.6" />
8+
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="18.7.0" />
89
</ItemGroup>
910
</Project>

0 commit comments

Comments
 (0)