Skip to content

Commit 99a2b69

Browse files
authored
Merge pull request #1247 from bollhals/avoidRefInBasicPublish
change ref to in and use Unsafe.AsRef
2 parents 868dfc3 + b597225 commit 99a2b69

15 files changed

+91
-114
lines changed

projects/RabbitMQ.Client/client/api/IModel.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ string BasicConsume(
188188
/// Routing key must be shorter than 255 bytes.
189189
/// </para>
190190
/// </remarks>
191-
void BasicPublish<TProperties>(string exchange, string routingKey, ref TProperties basicProperties, ReadOnlyMemory<byte> body = default, bool mandatory = false)
191+
void BasicPublish<TProperties>(string exchange, string routingKey, in TProperties basicProperties, ReadOnlyMemory<byte> body = default, bool mandatory = false)
192192
where TProperties : IReadOnlyBasicProperties, IAmqpHeader;
193193
/// <summary>
194194
/// Publishes a message.
@@ -198,7 +198,7 @@ void BasicPublish<TProperties>(string exchange, string routingKey, ref TProperti
198198
/// Routing key must be shorter than 255 bytes.
199199
/// </para>
200200
/// </remarks>
201-
void BasicPublish<TProperties>(CachedString exchange, CachedString routingKey, ref TProperties basicProperties, ReadOnlyMemory<byte> body = default, bool mandatory = false)
201+
void BasicPublish<TProperties>(CachedString exchange, CachedString routingKey, in TProperties basicProperties, ReadOnlyMemory<byte> body = default, bool mandatory = false)
202202
where TProperties : IReadOnlyBasicProperties, IAmqpHeader;
203203
#nullable disable
204204

projects/RabbitMQ.Client/client/api/IModelExtensions.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ public static string BasicConsume(this IModel model, string queue,
8282
/// <remarks>
8383
/// The publication occurs with mandatory=false and immediate=false.
8484
/// </remarks>
85-
public static void BasicPublish<T>(this IModel model, PublicationAddress addr, ref T basicProperties, ReadOnlyMemory<byte> body)
85+
public static void BasicPublish<T>(this IModel model, PublicationAddress addr, in T basicProperties, ReadOnlyMemory<byte> body)
8686
where T : IReadOnlyBasicProperties, IAmqpHeader
8787
{
88-
model.BasicPublish(addr.ExchangeName, addr.RoutingKey, ref basicProperties, body);
88+
model.BasicPublish(addr.ExchangeName, addr.RoutingKey, in basicProperties, body);
8989
}
9090

9191
public static void BasicPublish(this IModel model, string exchange, string routingKey, ReadOnlyMemory<byte> body = default, bool mandatory = false)
92-
=> model.BasicPublish(exchange, routingKey, ref EmptyBasicProperty.Empty, body, mandatory);
92+
=> model.BasicPublish(exchange, routingKey, in EmptyBasicProperty.Empty, body, mandatory);
9393

9494
public static void BasicPublish(this IModel model, CachedString exchange, CachedString routingKey, ReadOnlyMemory<byte> body = default, bool mandatory = false)
95-
=> model.BasicPublish(exchange, routingKey, ref EmptyBasicProperty.Empty, body, mandatory);
95+
=> model.BasicPublish(exchange, routingKey, in EmptyBasicProperty.Empty, body, mandatory);
9696
#nullable disable
9797

9898
/// <summary>
@@ -113,7 +113,7 @@ public static void ExchangeBind(this IModel model, string destination, string so
113113
}
114114

115115
/// <summary>
116-
/// (Extension method) Like exchange bind but sets nowait to true.
116+
/// (Extension method) Like exchange bind but sets nowait to true.
117117
/// </summary>
118118
public static void ExchangeBindNoWait(this IModel model, string destination, string source, string routingKey, IDictionary<string, object> arguments = null)
119119
{
@@ -130,7 +130,7 @@ public static void ExchangeDeclare(this IModel model, string exchange, string ty
130130
}
131131

132132
/// <summary>
133-
/// (Extension method) Like ExchangeDeclare but sets nowait to true.
133+
/// (Extension method) Like ExchangeDeclare but sets nowait to true.
134134
/// </summary>
135135
public static void ExchangeDeclareNoWait(this IModel model, string exchange, string type, bool durable = false, bool autoDelete = false,
136136
IDictionary<string, object> arguments = null)

projects/RabbitMQ.Client/client/framing/Model.cs

+49-72
Original file line numberDiff line numberDiff line change
@@ -44,255 +44,232 @@ public Model(ConnectionConfig config, ISession session) : base(config, session)
4444

4545
public override void ConnectionTuneOk(ushort channelMax, uint frameMax, ushort heartbeat)
4646
{
47-
var cmd = new ConnectionTuneOk(channelMax, frameMax, heartbeat);
48-
ModelSend(ref cmd);
47+
ModelSend(new ConnectionTuneOk(channelMax, frameMax, heartbeat));
4948
}
5049

5150
public override void _Private_BasicCancel(string consumerTag, bool nowait)
5251
{
53-
var cmd = new BasicCancel(consumerTag, nowait);
54-
ModelSend(ref cmd);
52+
ModelSend(new BasicCancel(consumerTag, nowait));
5553
}
5654

5755
public override void _Private_BasicConsume(string queue, string consumerTag, bool noLocal, bool autoAck, bool exclusive, bool nowait, IDictionary<string, object> arguments)
5856
{
59-
var cmd = new BasicConsume(queue, consumerTag, noLocal, autoAck, exclusive, nowait, arguments);
60-
ModelSend(ref cmd);
57+
ModelSend(new BasicConsume(queue, consumerTag, noLocal, autoAck, exclusive, nowait, arguments));
6158
}
6259

6360
public override void _Private_BasicGet(string queue, bool autoAck)
6461
{
65-
var cmd = new BasicGet(queue, autoAck);
66-
ModelSend(ref cmd);
62+
ModelSend(new BasicGet(queue, autoAck));
6763
}
6864

6965
public override void _Private_BasicRecover(bool requeue)
7066
{
71-
var cmd = new BasicRecover(requeue);
72-
ModelSend(ref cmd);
67+
ModelSend(new BasicRecover(requeue));
7368
}
7469

7570
public override void _Private_ChannelClose(ushort replyCode, string replyText, ushort classId, ushort methodId)
7671
{
77-
var cmd = new ChannelClose(replyCode, replyText, classId, methodId);
78-
ModelSend(ref cmd);
72+
ModelSend(new ChannelClose(replyCode, replyText, classId, methodId));
7973
}
8074

8175
public override void _Private_ChannelCloseOk()
8276
{
83-
var cmd = new ChannelCloseOk();
84-
ModelSend(ref cmd);
77+
ModelSend(new ChannelCloseOk());
8578
}
8679

8780
public override void _Private_ChannelFlowOk(bool active)
8881
{
89-
var cmd = new ChannelFlowOk(active);
90-
ModelSend(ref cmd);
82+
ModelSend(new ChannelFlowOk(active));
9183
}
9284

9385
public override void _Private_ChannelOpen()
9486
{
95-
var cmd = new ChannelOpen();
96-
ModelRpc(ref cmd, ProtocolCommandId.ChannelOpenOk);
87+
ModelRpc(new ChannelOpen(), ProtocolCommandId.ChannelOpenOk);
9788
}
9889

9990
public override void _Private_ConfirmSelect(bool nowait)
10091
{
10192
var method = new ConfirmSelect(nowait);
10293
if (nowait)
10394
{
104-
ModelSend(ref method);
95+
ModelSend(method);
10596
}
10697
else
10798
{
108-
ModelRpc(ref method, ProtocolCommandId.ConfirmSelectOk);
99+
ModelRpc(method, ProtocolCommandId.ConfirmSelectOk);
109100
}
110101
}
111102

112103
public override void _Private_ConnectionCloseOk()
113104
{
114-
var cmd = new ConnectionCloseOk();
115-
ModelSend(ref cmd);
105+
ModelSend(new ConnectionCloseOk());
116106
}
117107

118108
public override void _Private_ConnectionOpen(string virtualHost)
119109
{
120-
var cmd = new ConnectionOpen(virtualHost);
121-
ModelSend(ref cmd);
110+
ModelSend(new ConnectionOpen(virtualHost));
122111
}
123112

124113
public override void _Private_ConnectionSecureOk(byte[] response)
125114
{
126-
var cmd = new ConnectionSecureOk(response);
127-
ModelSend(ref cmd);
115+
ModelSend(new ConnectionSecureOk(response));
128116
}
129117

130118
public override void _Private_ConnectionStartOk(IDictionary<string, object> clientProperties, string mechanism, byte[] response, string locale)
131119
{
132-
var cmd = new ConnectionStartOk(clientProperties, mechanism, response, locale);
133-
ModelSend(ref cmd);
120+
ModelSend(new ConnectionStartOk(clientProperties, mechanism, response, locale));
134121
}
135122

136123
public override void _Private_UpdateSecret(byte[] newSecret, string reason)
137124
{
138-
var cmd = new ConnectionUpdateSecret(newSecret, reason);
139-
ModelRpc(ref cmd, ProtocolCommandId.ConnectionUpdateSecretOk);
125+
ModelRpc(new ConnectionUpdateSecret(newSecret, reason), ProtocolCommandId.ConnectionUpdateSecretOk);
140126
}
141127

142128
public override void _Private_ExchangeBind(string destination, string source, string routingKey, bool nowait, IDictionary<string, object> arguments)
143129
{
144-
ExchangeBind method = new ExchangeBind(destination, source, routingKey, nowait, arguments);
130+
var method = new ExchangeBind(destination, source, routingKey, nowait, arguments);
145131
if (nowait)
146132
{
147-
ModelSend(ref method);
133+
ModelSend(method);
148134
}
149135
else
150136
{
151-
ModelRpc(ref method, ProtocolCommandId.ExchangeBindOk);
137+
ModelRpc(method, ProtocolCommandId.ExchangeBindOk);
152138
}
153139
}
154140

155141
public override void _Private_ExchangeDeclare(string exchange, string type, bool passive, bool durable, bool autoDelete, bool @internal, bool nowait, IDictionary<string, object> arguments)
156142
{
157-
ExchangeDeclare method = new ExchangeDeclare(exchange, type, passive, durable, autoDelete, @internal, nowait, arguments);
143+
var method = new ExchangeDeclare(exchange, type, passive, durable, autoDelete, @internal, nowait, arguments);
158144
if (nowait)
159145
{
160-
ModelSend(ref method);
146+
ModelSend(method);
161147
}
162148
else
163149
{
164-
ModelRpc(ref method, ProtocolCommandId.ExchangeDeclareOk);
150+
ModelRpc(method, ProtocolCommandId.ExchangeDeclareOk);
165151
}
166152
}
167153

168154
public override void _Private_ExchangeDelete(string exchange, bool ifUnused, bool nowait)
169155
{
170-
ExchangeDelete method = new ExchangeDelete(exchange, ifUnused, nowait);
156+
var method = new ExchangeDelete(exchange, ifUnused, nowait);
171157
if (nowait)
172158
{
173-
ModelSend(ref method);
159+
ModelSend(method);
174160
}
175161
else
176162
{
177-
ModelRpc(ref method, ProtocolCommandId.ExchangeDeleteOk);
163+
ModelRpc(method, ProtocolCommandId.ExchangeDeleteOk);
178164
}
179165
}
180166

181167
public override void _Private_ExchangeUnbind(string destination, string source, string routingKey, bool nowait, IDictionary<string, object> arguments)
182168
{
183-
ExchangeUnbind method = new ExchangeUnbind(destination, source, routingKey, nowait, arguments);
169+
var method = new ExchangeUnbind(destination, source, routingKey, nowait, arguments);
184170
if (nowait)
185171
{
186-
ModelSend(ref method);
172+
ModelSend(method);
187173
}
188174
else
189175
{
190-
ModelRpc(ref method, ProtocolCommandId.ExchangeUnbindOk);
176+
ModelRpc(method, ProtocolCommandId.ExchangeUnbindOk);
191177
}
192178
}
193179

194180
public override void _Private_QueueBind(string queue, string exchange, string routingKey, bool nowait, IDictionary<string, object> arguments)
195181
{
196-
QueueBind method = new QueueBind(queue, exchange, routingKey, nowait, arguments);
182+
var method = new QueueBind(queue, exchange, routingKey, nowait, arguments);
197183
if (nowait)
198184
{
199-
ModelSend(ref method);
185+
ModelSend(method);
200186
}
201187
else
202188
{
203-
ModelRpc(ref method, ProtocolCommandId.QueueBindOk);
189+
ModelRpc(method, ProtocolCommandId.QueueBindOk);
204190
}
205191
}
206192

207193
public override void _Private_QueueDeclare(string queue, bool passive, bool durable, bool exclusive, bool autoDelete, bool nowait, IDictionary<string, object> arguments)
208194
{
209-
QueueDeclare method = new QueueDeclare(queue, passive, durable, exclusive, autoDelete, nowait, arguments);
195+
var method = new QueueDeclare(queue, passive, durable, exclusive, autoDelete, nowait, arguments);
210196
if (nowait)
211197
{
212-
ModelSend(ref method);
198+
ModelSend(method);
213199
}
214200
else
215201
{
216-
ModelSend(ref method);
202+
ModelSend(method);
217203
}
218204
}
219205

220206
public override uint _Private_QueueDelete(string queue, bool ifUnused, bool ifEmpty, bool nowait)
221207
{
222-
QueueDelete method = new QueueDelete(queue, ifUnused, ifEmpty, nowait);
208+
var method = new QueueDelete(queue, ifUnused, ifEmpty, nowait);
223209
if (nowait)
224210
{
225-
ModelSend(ref method);
211+
ModelSend(method);
226212
return 0xFFFFFFFF;
227213
}
228214

229-
return ModelRpc(ref method, ProtocolCommandId.QueueDeleteOk, memory => new QueueDeleteOk(memory.Span)._messageCount);
215+
return ModelRpc(method, ProtocolCommandId.QueueDeleteOk, memory => new QueueDeleteOk(memory.Span)._messageCount);
230216
}
231217

232218
public override uint _Private_QueuePurge(string queue, bool nowait)
233219
{
234-
QueuePurge method = new QueuePurge(queue, nowait);
220+
var method = new QueuePurge(queue, nowait);
235221
if (nowait)
236222
{
237-
ModelSend(ref method);
223+
ModelSend(method);
238224
return 0xFFFFFFFF;
239225
}
240226

241-
return ModelRpc(ref method, ProtocolCommandId.QueuePurgeOk, memory => new QueuePurgeOk(memory.Span)._messageCount);
227+
return ModelRpc(method, ProtocolCommandId.QueuePurgeOk, memory => new QueuePurgeOk(memory.Span)._messageCount);
242228
}
243229

244230
public override void BasicAck(ulong deliveryTag, bool multiple)
245231
{
246-
var cmd = new BasicAck(deliveryTag, multiple);
247-
ModelSend(ref cmd);
232+
ModelSend(new BasicAck(deliveryTag, multiple));
248233
}
249234

250235
public override void BasicNack(ulong deliveryTag, bool multiple, bool requeue)
251236
{
252-
var cmd = new BasicNack(deliveryTag, multiple, requeue);
253-
ModelSend(ref cmd);
237+
ModelSend(new BasicNack(deliveryTag, multiple, requeue));
254238
}
255239

256240
public override void BasicQos(uint prefetchSize, ushort prefetchCount, bool global)
257241
{
258-
var cmd = new BasicQos(prefetchSize, prefetchCount, global);
259-
ModelRpc(ref cmd, ProtocolCommandId.BasicQosOk);
242+
ModelRpc(new BasicQos(prefetchSize, prefetchCount, global), ProtocolCommandId.BasicQosOk);
260243
}
261244

262245
public override void BasicRecoverAsync(bool requeue)
263246
{
264-
var cmd = new BasicRecoverAsync(requeue);
265-
ModelSend(ref cmd);
247+
ModelSend(new BasicRecoverAsync(requeue));
266248
}
267249

268250
public override void BasicReject(ulong deliveryTag, bool requeue)
269251
{
270-
var cmd = new BasicReject(deliveryTag, requeue);
271-
ModelSend(ref cmd);
252+
ModelSend(new BasicReject(deliveryTag, requeue));
272253
}
273254

274255
public override void QueueUnbind(string queue, string exchange, string routingKey, IDictionary<string, object> arguments)
275256
{
276-
var cmd = new QueueUnbind(queue, exchange, routingKey, arguments);
277-
ModelRpc(ref cmd, ProtocolCommandId.QueueUnbindOk);
257+
ModelRpc(new QueueUnbind(queue, exchange, routingKey, arguments), ProtocolCommandId.QueueUnbindOk);
278258
}
279259

280260
public override void TxCommit()
281261
{
282-
var cmd = new TxCommit();
283-
ModelRpc(ref cmd, ProtocolCommandId.TxCommitOk);
262+
ModelRpc(new TxCommit(), ProtocolCommandId.TxCommitOk);
284263
}
285264

286265
public override void TxRollback()
287266
{
288-
var cmd = new TxRollback();
289-
ModelRpc(ref cmd, ProtocolCommandId.TxRollbackOk);
267+
ModelRpc(new TxRollback(), ProtocolCommandId.TxRollbackOk);
290268
}
291269

292270
public override void TxSelect()
293271
{
294-
var cmd = new TxSelect();
295-
ModelRpc(ref cmd, ProtocolCommandId.TxSelectOk);
272+
ModelRpc(new TxSelect(), ProtocolCommandId.TxSelectOk);
296273
}
297274

298275
protected override bool DispatchAsynchronous(in IncomingCommand cmd)

projects/RabbitMQ.Client/client/impl/AutorecoveringModel.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ public BasicGetResult BasicGet(string queue, bool autoAck)
249249
public void BasicNack(ulong deliveryTag, bool multiple, bool requeue)
250250
=> InnerChannel.BasicNack(deliveryTag, multiple, requeue);
251251

252-
public void BasicPublish<TProperties>(string exchange, string routingKey, ref TProperties basicProperties, ReadOnlyMemory<byte> body, bool mandatory)
252+
public void BasicPublish<TProperties>(string exchange, string routingKey, in TProperties basicProperties, ReadOnlyMemory<byte> body, bool mandatory)
253253
where TProperties : IReadOnlyBasicProperties, IAmqpHeader
254-
=> InnerChannel.BasicPublish(exchange, routingKey, ref basicProperties, body, mandatory);
254+
=> InnerChannel.BasicPublish(exchange, routingKey, in basicProperties, body, mandatory);
255255

256-
public void BasicPublish<TProperties>(CachedString exchange, CachedString routingKey, ref TProperties basicProperties, ReadOnlyMemory<byte> body, bool mandatory)
256+
public void BasicPublish<TProperties>(CachedString exchange, CachedString routingKey, in TProperties basicProperties, ReadOnlyMemory<byte> body, bool mandatory)
257257
where TProperties : IReadOnlyBasicProperties, IAmqpHeader
258-
=> InnerChannel.BasicPublish(exchange, routingKey, ref basicProperties, body, mandatory);
258+
=> InnerChannel.BasicPublish(exchange, routingKey, in basicProperties, body, mandatory);
259259

260260
public void BasicQos(uint prefetchSize, ushort prefetchCount, bool global)
261261
{

projects/RabbitMQ.Client/client/impl/Connection.Receive.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private void HardProtocolExceptionHandler(HardProtocolException hpe)
148148
try
149149
{
150150
var cmd = new ConnectionClose(hpe.ShutdownReason.ReplyCode, hpe.ShutdownReason.ReplyText, 0, 0);
151-
_session0.Transmit(ref cmd);
151+
_session0.Transmit(in cmd);
152152
if (hpe.CanShutdownCleanly)
153153
{
154154
ClosingLoop();

projects/RabbitMQ.Client/client/impl/Connection.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ internal void Close(ShutdownEventArgs reason, bool abort, TimeSpan timeout)
284284
// Try to send connection.close wait for CloseOk in the MainLoop
285285
if (!_closed)
286286
{
287-
var cmd = new ConnectionClose(reason.ReplyCode, reason.ReplyText, 0, 0);
288-
_session0.Transmit(ref cmd);
287+
_session0.Transmit(new ConnectionClose(reason.ReplyCode, reason.ReplyText, 0, 0));
289288
}
290289
}
291290
catch (AlreadyClosedException)

0 commit comments

Comments
 (0)