Skip to content

Commit 8bf9308

Browse files
stebetlukebakken
authored andcommitted
Recycling command buffers to reduce allocations when receiving messages.
1 parent f33dd6c commit 8bf9308

28 files changed

+109
-87
lines changed

projects/client/RabbitMQ.Client/src/client/api/AsyncDefaultBasicConsumer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public virtual Task HandleBasicDeliver(string consumerTag,
113113
string exchange,
114114
string routingKey,
115115
IBasicProperties properties,
116-
byte[] body)
116+
ReadOnlyMemory<byte> body)
117117
{
118118
// Nothing to do here.
119119
return TaskExtensions.CompletedTask;
@@ -167,7 +167,7 @@ void IBasicConsumer.HandleBasicConsumeOk(string consumerTag)
167167
throw new InvalidOperationException("Should never be called.");
168168
}
169169

170-
void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
170+
void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, ReadOnlyMemory<byte> body)
171171
{
172172
throw new InvalidOperationException("Should never be called.");
173173
}

projects/client/RabbitMQ.Client/src/client/api/BasicGetResult.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
3939
//---------------------------------------------------------------------------
4040

41+
using System;
42+
4143
namespace RabbitMQ.Client
4244
{
4345
/// <summary>Represents Basic.GetOk responses from the server.</summary>
@@ -57,7 +59,7 @@ public class BasicGetResult
5759
/// <param name="basicProperties">The Basic-class content header properties for the message.</param>
5860
/// <param name="body"></param>
5961
public BasicGetResult(ulong deliveryTag, bool redelivered, string exchange,
60-
string routingKey, uint messageCount, IBasicProperties basicProperties, byte[] body)
62+
string routingKey, uint messageCount, IBasicProperties basicProperties, ReadOnlyMemory<byte> body)
6163
{
6264
DeliveryTag = deliveryTag;
6365
Redelivered = redelivered;
@@ -76,7 +78,7 @@ public BasicGetResult(ulong deliveryTag, bool redelivered, string exchange,
7678
/// <summary>
7779
/// Retrieves the body of this message.
7880
/// </summary>
79-
public byte[] Body { get; private set; }
81+
public ReadOnlyMemory<byte> Body { get; private set; }
8082

8183
/// <summary>
8284
/// Retrieve the delivery tag for this message. See also <see cref="IModel.BasicAck"/>.

projects/client/RabbitMQ.Client/src/client/api/DefaultBasicConsumer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public virtual void HandleBasicDeliver(string consumerTag,
161161
string exchange,
162162
string routingKey,
163163
IBasicProperties properties,
164-
byte[] body)
164+
ReadOnlyMemory<byte> body)
165165
{
166166
// Nothing to do here.
167167
}

projects/client/RabbitMQ.Client/src/client/api/IAsyncBasicConsumer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Threading.Tasks;
23

34
using RabbitMQ.Client.Events;
@@ -51,7 +52,7 @@ Task HandleBasicDeliver(string consumerTag,
5152
string exchange,
5253
string routingKey,
5354
IBasicProperties properties,
54-
byte[] body);
55+
ReadOnlyMemory<byte> body);
5556

5657
/// <summary>
5758
/// Called when the model shuts down.
@@ -60,4 +61,4 @@ Task HandleBasicDeliver(string consumerTag,
6061
/// <param name="reason"> Information about the reason why a particular model, session, or connection was destroyed.</param>
6162
Task HandleModelShutdown(object model, ShutdownEventArgs reason);
6263
}
63-
}
64+
}

projects/client/RabbitMQ.Client/src/client/api/IBasicConsumer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void HandleBasicDeliver(string consumerTag,
104104
string exchange,
105105
string routingKey,
106106
IBasicProperties properties,
107-
byte[] body);
107+
ReadOnlyMemory<byte> body);
108108

109109
/// <summary>
110110
/// Called when the model shuts down.

projects/client/RabbitMQ.Client/src/client/events/AsyncEventingBasicConsumer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Threading.Tasks;
23

34
namespace RabbitMQ.Client.Events
@@ -37,7 +38,7 @@ public override async Task HandleBasicConsumeOk(string consumerTag)
3738
}
3839

3940
///<summary>Fires the Received event.</summary>
40-
public override async Task HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
41+
public override async Task HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, ReadOnlyMemory<byte> body)
4142
{
4243
await base.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body).ConfigureAwait(false);
4344
await (Received?.Invoke(

projects/client/RabbitMQ.Client/src/client/events/BasicDeliverEventArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public BasicDeliverEventArgs(string consumerTag,
5959
string exchange,
6060
string routingKey,
6161
IBasicProperties properties,
62-
byte[] body)
62+
ReadOnlyMemory<byte> body)
6363
{
6464
ConsumerTag = consumerTag;
6565
DeliveryTag = deliveryTag;
@@ -74,7 +74,7 @@ public BasicDeliverEventArgs(string consumerTag,
7474
public IBasicProperties BasicProperties { get; set; }
7575

7676
///<summary>The message body.</summary>
77-
public byte[] Body { get; set; }
77+
public ReadOnlyMemory<byte> Body { get; set; }
7878

7979
///<summary>The consumer tag of the consumer that the message
8080
///was delivered to.</summary>

projects/client/RabbitMQ.Client/src/client/events/BasicReturnEventArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class BasicReturnEventArgs : EventArgs
5050
public IBasicProperties BasicProperties { get; set; }
5151

5252
///<summary>The message body.</summary>
53-
public byte[] Body { get; set; }
53+
public ReadOnlyMemory<byte> Body { get; set; }
5454

5555
///<summary>The exchange the returned message was originally
5656
///published to.</summary>

projects/client/RabbitMQ.Client/src/client/events/EventingBasicConsumer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public override void HandleBasicConsumeOk(string consumerTag)
7979
}
8080

8181
///<summary>Fires the Received event.</summary>
82-
public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
82+
public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, ReadOnlyMemory<byte> body)
8383
{
8484
base.HandleBasicDeliver(consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body);
8585
Received?.Invoke(

projects/client/RabbitMQ.Client/src/client/impl/AsyncConsumerDispatcher.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace RabbitMQ.Client.Impl
1+
using System;
2+
3+
namespace RabbitMQ.Client.Impl
24
{
35
internal sealed class AsyncConsumerDispatcher : IConsumerDispatcher
46
{
@@ -46,7 +48,7 @@ public void HandleBasicDeliver(IBasicConsumer consumer,
4648
string exchange,
4749
string routingKey,
4850
IBasicProperties basicProperties,
49-
byte[] body)
51+
ReadOnlyMemory<byte> body)
5052
{
5153
ScheduleUnlessShuttingDown(new BasicDeliver(consumer, consumerTag, deliveryTag, redelivered, exchange, routingKey, basicProperties, body));
5254
}

0 commit comments

Comments
 (0)