Skip to content

Commit 5d60790

Browse files
Merge pull request #1098 from bollhals/feature/refInsteadOfIn
use ref instead of in for generic T + interface
2 parents 7640036 + 1b1aed7 commit 5d60790

File tree

10 files changed

+103
-76
lines changed

10 files changed

+103
-76
lines changed

projects/Benchmarks/WireFormatting/MethodFraming.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ namespace RabbitMQ.Benchmarks
1414
[BenchmarkCategory("Framing")]
1515
public class MethodFramingBasicAck
1616
{
17-
private readonly BasicAck _basicAck = new BasicAck(ulong.MaxValue, true);
17+
private BasicAck _basicAck = new BasicAck(ulong.MaxValue, true);
1818

1919
[Params(0)]
2020
public ushort Channel { get; set; }
2121

2222
[Benchmark]
23-
public ReadOnlyMemory<byte> BasicAckWrite() => Framing.SerializeToFrames(_basicAck, Channel);
23+
public ReadOnlyMemory<byte> BasicAckWrite() => Framing.SerializeToFrames(ref _basicAck, Channel);
2424
}
2525

2626
[Config(typeof(Config))]
2727
[BenchmarkCategory("Framing")]
2828
public class MethodFramingBasicPublish
2929
{
3030
private const string StringValue = "Exchange_OR_RoutingKey";
31-
private readonly BasicPublish _basicPublish = new BasicPublish(StringValue, StringValue, false, false);
32-
private readonly BasicPublishMemory _basicPublishMemory = new BasicPublishMemory(Encoding.UTF8.GetBytes(StringValue), Encoding.UTF8.GetBytes(StringValue), false, false);
31+
private BasicPublish _basicPublish = new BasicPublish(StringValue, StringValue, false, false);
32+
private BasicPublishMemory _basicPublishMemory = new BasicPublishMemory(Encoding.UTF8.GetBytes(StringValue), Encoding.UTF8.GetBytes(StringValue), false, false);
3333
private readonly BasicProperties _propertiesEmpty = new BasicProperties();
3434
private readonly BasicProperties _properties = new BasicProperties { AppId = "Application id", MessageId = "Random message id" };
3535
private readonly ReadOnlyMemory<byte> _bodyEmpty = ReadOnlyMemory<byte>.Empty;
@@ -42,25 +42,25 @@ public class MethodFramingBasicPublish
4242
public int FrameMax { get; set; }
4343

4444
[Benchmark]
45-
public ReadOnlyMemory<byte> BasicPublishWriteNonEmpty() => Framing.SerializeToFrames(_basicPublish, _properties, _body, Channel, FrameMax);
45+
public ReadOnlyMemory<byte> BasicPublishWriteNonEmpty() => Framing.SerializeToFrames(ref _basicPublish, _properties, _body, Channel, FrameMax);
4646

4747
[Benchmark]
48-
public ReadOnlyMemory<byte> BasicPublishWrite() => Framing.SerializeToFrames(_basicPublish, _propertiesEmpty, _bodyEmpty, Channel, FrameMax);
48+
public ReadOnlyMemory<byte> BasicPublishWrite() => Framing.SerializeToFrames(ref _basicPublish, _propertiesEmpty, _bodyEmpty, Channel, FrameMax);
4949

5050
[Benchmark]
51-
public ReadOnlyMemory<byte> BasicPublishMemoryWrite() => Framing.SerializeToFrames(_basicPublishMemory, _propertiesEmpty, _bodyEmpty, Channel, FrameMax);
51+
public ReadOnlyMemory<byte> BasicPublishMemoryWrite() => Framing.SerializeToFrames(ref _basicPublishMemory, _propertiesEmpty, _bodyEmpty, Channel, FrameMax);
5252
}
5353

5454
[Config(typeof(Config))]
5555
[BenchmarkCategory("Framing")]
5656
public class MethodFramingChannelClose
5757
{
58-
private readonly ChannelClose _channelClose = new ChannelClose(333, string.Empty, 0099, 2999);
58+
private ChannelClose _channelClose = new ChannelClose(333, string.Empty, 0099, 2999);
5959

6060
[Params(0)]
6161
public ushort Channel { get; set; }
6262

6363
[Benchmark]
64-
public ReadOnlyMemory<byte> ChannelCloseWrite() => Framing.SerializeToFrames(_channelClose, Channel);
64+
public ReadOnlyMemory<byte> ChannelCloseWrite() => Framing.SerializeToFrames(ref _channelClose, Channel);
6565
}
6666
}

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

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -44,107 +44,123 @@ public Model(bool dispatchAsync, int concurrency, ISession session) : base(dispa
4444

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

5051
public override void _Private_BasicCancel(string consumerTag, bool nowait)
5152
{
52-
ModelSend(new BasicCancel(consumerTag, nowait));
53+
var cmd = new BasicCancel(consumerTag, nowait);
54+
ModelSend(ref cmd);
5355
}
5456

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

6063
public override void _Private_BasicGet(string queue, bool autoAck)
6164
{
62-
ModelSend(new BasicGet(queue, autoAck));
65+
var cmd = new BasicGet(queue, autoAck);
66+
ModelSend(ref cmd);
6367
}
6468

6569
public override void _Private_BasicPublish(string exchange, string routingKey, bool mandatory, IBasicProperties basicProperties, ReadOnlyMemory<byte> body)
6670
{
67-
ModelSend(new BasicPublish(exchange, routingKey, mandatory, default), (BasicProperties) basicProperties, body);
71+
var cmd = new BasicPublish(exchange, routingKey, mandatory, default);
72+
ModelSend(ref cmd, (BasicProperties) basicProperties, body);
6873
}
6974

7075
public override void _Private_BasicPublishMemory(ReadOnlyMemory<byte> exchange, ReadOnlyMemory<byte> routingKey, bool mandatory, IBasicProperties basicProperties, ReadOnlyMemory<byte> body)
7176
{
72-
ModelSend(new BasicPublishMemory(exchange, routingKey, mandatory, default), (BasicProperties) basicProperties, body);
77+
var cmd = new BasicPublishMemory(exchange, routingKey, mandatory, default);
78+
ModelSend(ref cmd, (BasicProperties) basicProperties, body);
7379
}
7480

7581
public override void _Private_BasicRecover(bool requeue)
7682
{
77-
ModelSend(new BasicRecover(requeue));
83+
var cmd = new BasicRecover(requeue);
84+
ModelSend(ref cmd);
7885
}
7986

8087
public override void _Private_ChannelClose(ushort replyCode, string replyText, ushort classId, ushort methodId)
8188
{
82-
ModelSend(new ChannelClose(replyCode, replyText, classId, methodId));
89+
var cmd = new ChannelClose(replyCode, replyText, classId, methodId);
90+
ModelSend(ref cmd);
8391
}
8492

8593
public override void _Private_ChannelCloseOk()
8694
{
87-
ModelSend(new ChannelCloseOk());
95+
var cmd = new ChannelCloseOk();
96+
ModelSend(ref cmd);
8897
}
8998

9099
public override void _Private_ChannelFlowOk(bool active)
91100
{
92-
ModelSend(new ChannelFlowOk(active));
101+
var cmd = new ChannelFlowOk(active);
102+
ModelSend(ref cmd);
93103
}
94104

95105
public override void _Private_ChannelOpen()
96106
{
97-
ModelRpc(new ChannelOpen(), ProtocolCommandId.ChannelOpenOk);
107+
var cmd = new ChannelOpen();
108+
ModelRpc(ref cmd, ProtocolCommandId.ChannelOpenOk);
98109
}
99110

100111
public override void _Private_ConfirmSelect(bool nowait)
101112
{
102113
var method = new ConfirmSelect(nowait);
103114
if (nowait)
104115
{
105-
ModelSend(method);
116+
ModelSend(ref method);
106117
}
107118
else
108119
{
109-
ModelRpc(method, ProtocolCommandId.ConfirmSelectOk);
120+
ModelRpc(ref method, ProtocolCommandId.ConfirmSelectOk);
110121
}
111122
}
112123

113124
public override void _Private_ConnectionCloseOk()
114125
{
115-
ModelSend(new ConnectionCloseOk());
126+
var cmd = new ConnectionCloseOk();
127+
ModelSend(ref cmd);
116128
}
117129

118130
public override void _Private_ConnectionOpen(string virtualHost)
119131
{
120-
ModelSend(new ConnectionOpen(virtualHost));
132+
var cmd = new ConnectionOpen(virtualHost);
133+
ModelSend(ref cmd);
121134
}
122135

123136
public override void _Private_ConnectionSecureOk(byte[] response)
124137
{
125-
ModelSend(new ConnectionSecureOk(response));
138+
var cmd = new ConnectionSecureOk(response);
139+
ModelSend(ref cmd);
126140
}
127141

128142
public override void _Private_ConnectionStartOk(IDictionary<string, object> clientProperties, string mechanism, byte[] response, string locale)
129143
{
130-
ModelSend(new ConnectionStartOk(clientProperties, mechanism, response, locale));
144+
var cmd = new ConnectionStartOk(clientProperties, mechanism, response, locale);
145+
ModelSend(ref cmd);
131146
}
132147

133148
public override void _Private_UpdateSecret(byte[] newSecret, string reason)
134149
{
135-
ModelRpc(new ConnectionUpdateSecret(newSecret, reason), ProtocolCommandId.ConnectionUpdateSecretOk);
150+
var cmd = new ConnectionUpdateSecret(newSecret, reason);
151+
ModelRpc(ref cmd, ProtocolCommandId.ConnectionUpdateSecretOk);
136152
}
137153

138154
public override void _Private_ExchangeBind(string destination, string source, string routingKey, bool nowait, IDictionary<string, object> arguments)
139155
{
140156
ExchangeBind method = new ExchangeBind(destination, source, routingKey, nowait, arguments);
141157
if (nowait)
142158
{
143-
ModelSend(method);
159+
ModelSend(ref method);
144160
}
145161
else
146162
{
147-
ModelRpc(method, ProtocolCommandId.ExchangeBindOk);
163+
ModelRpc(ref method, ProtocolCommandId.ExchangeBindOk);
148164
}
149165
}
150166

@@ -153,11 +169,11 @@ public override void _Private_ExchangeDeclare(string exchange, string type, bool
153169
ExchangeDeclare method = new ExchangeDeclare(exchange, type, passive, durable, autoDelete, @internal, nowait, arguments);
154170
if (nowait)
155171
{
156-
ModelSend(method);
172+
ModelSend(ref method);
157173
}
158174
else
159175
{
160-
ModelRpc(method, ProtocolCommandId.ExchangeDeclareOk);
176+
ModelRpc(ref method, ProtocolCommandId.ExchangeDeclareOk);
161177
}
162178
}
163179

@@ -166,11 +182,11 @@ public override void _Private_ExchangeDelete(string exchange, bool ifUnused, boo
166182
ExchangeDelete method = new ExchangeDelete(exchange, ifUnused, nowait);
167183
if (nowait)
168184
{
169-
ModelSend(method);
185+
ModelSend(ref method);
170186
}
171187
else
172188
{
173-
ModelRpc(method, ProtocolCommandId.ExchangeDeleteOk);
189+
ModelRpc(ref method, ProtocolCommandId.ExchangeDeleteOk);
174190
}
175191
}
176192

@@ -179,11 +195,11 @@ public override void _Private_ExchangeUnbind(string destination, string source,
179195
ExchangeUnbind method = new ExchangeUnbind(destination, source, routingKey, nowait, arguments);
180196
if (nowait)
181197
{
182-
ModelSend(method);
198+
ModelSend(ref method);
183199
}
184200
else
185201
{
186-
ModelRpc(method, ProtocolCommandId.ExchangeUnbindOk);
202+
ModelRpc(ref method, ProtocolCommandId.ExchangeUnbindOk);
187203
}
188204
}
189205

@@ -192,11 +208,11 @@ public override void _Private_QueueBind(string queue, string exchange, string ro
192208
QueueBind method = new QueueBind(queue, exchange, routingKey, nowait, arguments);
193209
if (nowait)
194210
{
195-
ModelSend(method);
211+
ModelSend(ref method);
196212
}
197213
else
198214
{
199-
ModelRpc(method, ProtocolCommandId.QueueBindOk);
215+
ModelRpc(ref method, ProtocolCommandId.QueueBindOk);
200216
}
201217
}
202218

@@ -205,11 +221,11 @@ public override void _Private_QueueDeclare(string queue, bool passive, bool dura
205221
QueueDeclare method = new QueueDeclare(queue, passive, durable, exclusive, autoDelete, nowait, arguments);
206222
if (nowait)
207223
{
208-
ModelSend(method);
224+
ModelSend(ref method);
209225
}
210226
else
211227
{
212-
ModelSend(method);
228+
ModelSend(ref method);
213229
}
214230
}
215231

@@ -218,48 +234,53 @@ public override uint _Private_QueueDelete(string queue, bool ifUnused, bool ifEm
218234
QueueDelete method = new QueueDelete(queue, ifUnused, ifEmpty, nowait);
219235
if (nowait)
220236
{
221-
ModelSend(method);
237+
ModelSend(ref method);
222238
return 0xFFFFFFFF;
223239
}
224240

225-
return ModelRpc(method, ProtocolCommandId.QueueDeleteOk, memory => new QueueDeleteOk(memory.Span)._messageCount);
241+
return ModelRpc(ref method, ProtocolCommandId.QueueDeleteOk, memory => new QueueDeleteOk(memory.Span)._messageCount);
226242
}
227243

228244
public override uint _Private_QueuePurge(string queue, bool nowait)
229245
{
230246
QueuePurge method = new QueuePurge(queue, nowait);
231247
if (nowait)
232248
{
233-
ModelSend(method);
249+
ModelSend(ref method);
234250
return 0xFFFFFFFF;
235251
}
236252

237-
return ModelRpc(method, ProtocolCommandId.QueuePurgeOk, memory => new QueuePurgeOk(memory.Span)._messageCount);
253+
return ModelRpc(ref method, ProtocolCommandId.QueuePurgeOk, memory => new QueuePurgeOk(memory.Span)._messageCount);
238254
}
239255

240256
public override void BasicAck(ulong deliveryTag, bool multiple)
241257
{
242-
ModelSend(new BasicAck(deliveryTag, multiple));
258+
var cmd = new BasicAck(deliveryTag, multiple);
259+
ModelSend(ref cmd);
243260
}
244261

245262
public override void BasicNack(ulong deliveryTag, bool multiple, bool requeue)
246263
{
247-
ModelSend(new BasicNack(deliveryTag, multiple, requeue));
264+
var cmd = new BasicNack(deliveryTag, multiple, requeue);
265+
ModelSend(ref cmd);
248266
}
249267

250268
public override void BasicQos(uint prefetchSize, ushort prefetchCount, bool global)
251269
{
252-
ModelRpc(new BasicQos(prefetchSize, prefetchCount, global), ProtocolCommandId.BasicQosOk);
270+
var cmd = new BasicQos(prefetchSize, prefetchCount, global);
271+
ModelRpc(ref cmd, ProtocolCommandId.BasicQosOk);
253272
}
254273

255274
public override void BasicRecoverAsync(bool requeue)
256275
{
257-
ModelSend(new BasicRecoverAsync(requeue));
276+
var cmd = new BasicRecoverAsync(requeue);
277+
ModelSend(ref cmd);
258278
}
259279

260280
public override void BasicReject(ulong deliveryTag, bool requeue)
261281
{
262-
ModelSend(new BasicReject(deliveryTag, requeue));
282+
var cmd = new BasicReject(deliveryTag, requeue);
283+
ModelSend(ref cmd);
263284
}
264285

265286
public override IBasicProperties CreateBasicProperties()
@@ -269,22 +290,26 @@ public override IBasicProperties CreateBasicProperties()
269290

270291
public override void QueueUnbind(string queue, string exchange, string routingKey, IDictionary<string, object> arguments)
271292
{
272-
ModelRpc(new QueueUnbind(queue, exchange, routingKey, arguments), ProtocolCommandId.QueueUnbindOk);
293+
var cmd = new QueueUnbind(queue, exchange, routingKey, arguments);
294+
ModelRpc(ref cmd, ProtocolCommandId.QueueUnbindOk);
273295
}
274296

275297
public override void TxCommit()
276298
{
277-
ModelRpc(new TxCommit(), ProtocolCommandId.TxCommitOk);
299+
var cmd = new TxCommit();
300+
ModelRpc(ref cmd, ProtocolCommandId.TxCommitOk);
278301
}
279302

280303
public override void TxRollback()
281304
{
282-
ModelRpc(new TxRollback(), ProtocolCommandId.TxRollbackOk);
305+
var cmd = new TxRollback();
306+
ModelRpc(ref cmd, ProtocolCommandId.TxRollbackOk);
283307
}
284308

285309
public override void TxSelect()
286310
{
287-
ModelRpc(new TxSelect(), ProtocolCommandId.TxSelectOk);
311+
var cmd = new TxSelect();
312+
ModelRpc(ref cmd, ProtocolCommandId.TxSelectOk);
288313
}
289314

290315
protected override bool DispatchAsynchronous(in IncomingCommand cmd)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ private void HardProtocolExceptionHandler(HardProtocolException hpe)
147147
_session0.SetSessionClosing(false);
148148
try
149149
{
150-
_session0.Transmit(new ConnectionClose(hpe.ShutdownReason.ReplyCode, hpe.ShutdownReason.ReplyText, 0, 0));
150+
var cmd = new ConnectionClose(hpe.ShutdownReason.ReplyCode, hpe.ShutdownReason.ReplyText, 0, 0);
151+
_session0.Transmit(ref cmd);
151152
ClosingLoop();
152153
}
153154
catch (IOException ioe)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ internal void Close(ShutdownEventArgs reason, bool abort, TimeSpan timeout)
270270
try
271271
{
272272
// Try to send connection.close wait for CloseOk in the MainLoop
273-
_session0.Transmit(new ConnectionClose(reason.ReplyCode, reason.ReplyText, 0, 0));
273+
var cmd = new ConnectionClose(reason.ReplyCode, reason.ReplyText, 0, 0);
274+
_session0.Transmit(ref cmd);
274275
}
275276
catch (AlreadyClosedException)
276277
{

0 commit comments

Comments
 (0)