Skip to content

Commit 5357ded

Browse files
committed
adjust reading to span
1 parent fd4f1f4 commit 5357ded

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+611
-81
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using RabbitMQ.Client.Framing.Impl;
4+
using RabbitMQ.Client.Impl;
5+
6+
namespace Benchmarks.WireFormatting
7+
{
8+
[ShortRunJob]
9+
[MemoryDiagnoser]
10+
public class WireFormatting_Read_BasicAck
11+
{
12+
private readonly byte[] _buffer = new byte[1024];
13+
14+
public WireFormatting_Read_BasicAck()
15+
{
16+
new BasicAck(ulong.MaxValue, true).WriteArgumentsTo(_buffer);
17+
}
18+
19+
[Benchmark]
20+
public object ReadArgumentsFrom_MethodArgumentReader()
21+
{
22+
var reader = new MethodArgumentReader(new ReadOnlySpan<byte>(_buffer));
23+
MethodBase basicAck = new BasicAck();
24+
basicAck.ReadArgumentsFrom(ref reader);
25+
return basicAck;
26+
}
27+
28+
[Benchmark(Baseline = true)]
29+
public object ReadFromSpan()
30+
{
31+
return new BasicAck(new ReadOnlySpan<byte>(_buffer));
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using RabbitMQ.Client.Impl;
4+
using BasicDeliver = RabbitMQ.Client.Framing.Impl.BasicDeliver;
5+
6+
namespace Benchmarks.WireFormatting
7+
{
8+
[ShortRunJob]
9+
[MemoryDiagnoser]
10+
public class WireFormatting_Read_BasicDeliver
11+
{
12+
private readonly byte[] _buffer = new byte[1024];
13+
14+
public WireFormatting_Read_BasicDeliver()
15+
{
16+
new BasicDeliver(string.Empty, 0, false, string.Empty, string.Empty).WriteArgumentsTo(_buffer);
17+
}
18+
19+
[Benchmark]
20+
public object ReadArgumentsFrom_MethodArgumentReader()
21+
{
22+
var reader = new MethodArgumentReader(new ReadOnlySpan<byte>(_buffer));
23+
MethodBase basicAck = new BasicDeliver();
24+
basicAck.ReadArgumentsFrom(ref reader);
25+
return basicAck;
26+
}
27+
28+
[Benchmark(Baseline = true)]
29+
public object ReadFromSpan()
30+
{
31+
return new BasicDeliver(new ReadOnlySpan<byte>(_buffer));
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using RabbitMQ.Client.Framing.Impl;
4+
using RabbitMQ.Client.Impl;
5+
6+
namespace Benchmarks.WireFormatting
7+
{
8+
[ShortRunJob]
9+
[MemoryDiagnoser]
10+
public class WireFormatting_Read_ChannelClose
11+
{
12+
private readonly byte[] _buffer = new byte[1024];
13+
14+
public WireFormatting_Read_ChannelClose()
15+
{
16+
new ChannelClose(333, string.Empty, 0099, 2999).WriteArgumentsTo(_buffer);
17+
}
18+
19+
[Benchmark]
20+
public object ReadArgumentsFrom_MethodArgumentReader()
21+
{
22+
var reader = new MethodArgumentReader(new ReadOnlySpan<byte>(_buffer));
23+
MethodBase basicAck = new ChannelClose();
24+
basicAck.ReadArgumentsFrom(ref reader);
25+
return basicAck;
26+
}
27+
28+
[Benchmark(Baseline = true)]
29+
public object ReadFromSpan()
30+
{
31+
return new ChannelClose(new ReadOnlySpan<byte>(_buffer));
32+
}
33+
}
34+
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
//---------------------------------------------------------------------------
3131

3232
using System;
33-
using System.Buffers.Binary;
3433
using RabbitMQ.Client.client.framing;
3534
using RabbitMQ.Client.Impl;
3635

@@ -51,6 +50,12 @@ public BasicAck(ulong DeliveryTag, bool Multiple)
5150
_multiple = Multiple;
5251
}
5352

53+
public BasicAck(ReadOnlySpan<byte> span)
54+
{
55+
int offset = WireFormatting.ReadLonglong(span, out _deliveryTag);
56+
WireFormatting.ReadBits(span.Slice(offset), out _multiple);
57+
}
58+
5459
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicAck;
5560
public override string ProtocolMethodName => "basic.ack";
5661
public override bool HasContent => false;
@@ -74,12 +79,6 @@ public override int WriteArgumentsTo(Span<byte> span)
7479
return offset + WireFormatting.WriteBits(span.Slice(offset), _multiple);
7580
}
7681

77-
public void WriteArgumentsToSpan(Span<byte> span)
78-
{
79-
BinaryPrimitives.WriteUInt64BigEndian(span, _deliveryTag);
80-
span[8] = _multiple ? (byte) 1 : (byte) 0;
81-
}
82-
8382
public override int GetRequiredBufferSize()
8483
{
8584
return 8 + 1;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public BasicCancel(string ConsumerTag, bool Nowait)
5151
_nowait = Nowait;
5252
}
5353

54+
public BasicCancel(ReadOnlySpan<byte> span)
55+
{
56+
int offset = WireFormatting.ReadShortstr(span, out _consumerTag);
57+
WireFormatting.ReadBits(span.Slice(offset), out _nowait);
58+
}
59+
5460
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicCancel;
5561
public override string ProtocolMethodName => "basic.cancel";
5662
public override bool HasContent => false;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public BasicCancelOk(string ConsumerTag)
4949
_consumerTag = ConsumerTag;
5050
}
5151

52+
public BasicCancelOk(ReadOnlySpan<byte> span)
53+
{
54+
WireFormatting.ReadShortstr(span, out _consumerTag);
55+
}
56+
5257
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicCancelOk;
5358
public override string ProtocolMethodName => "basic.cancel-ok";
5459
public override bool HasContent => false;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ public BasicConsume(ushort Reserved1, string Queue, string ConsumerTag, bool NoL
6464
_arguments = Arguments;
6565
}
6666

67+
public BasicConsume(ReadOnlySpan<byte> span)
68+
{
69+
int offset = WireFormatting.ReadShort(span, out _reserved1);
70+
offset += WireFormatting.ReadShortstr(span, out _queue);
71+
offset += WireFormatting.ReadShortstr(span, out _consumerTag);
72+
offset += WireFormatting.ReadBits(span.Slice(offset), out _noLocal, out _noAck, out _exclusive, out _nowait);
73+
WireFormatting.ReadDictionary(span.Slice(offset), out var tmpDictionary);
74+
_arguments = tmpDictionary;
75+
}
76+
6777
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicConsume;
6878
public override string ProtocolMethodName => "basic.consume";
6979
public override bool HasContent => false;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public BasicConsumeOk(string ConsumerTag)
4949
_consumerTag = ConsumerTag;
5050
}
5151

52+
public BasicConsumeOk(ReadOnlySpan<byte> span)
53+
{
54+
WireFormatting.ReadShortstr(span, out _consumerTag);
55+
}
56+
5257
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicConsumeOk;
5358
public override string ProtocolMethodName => "basic.consume-ok";
5459
public override bool HasContent => false;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ public BasicDeliver(string ConsumerTag, ulong DeliveryTag, bool Redelivered, str
5858
_routingKey = RoutingKey;
5959
}
6060

61+
public BasicDeliver(ReadOnlySpan<byte> span)
62+
{
63+
int offset = WireFormatting.ReadShortstr(span, out _consumerTag);
64+
offset += WireFormatting.ReadLonglong(span.Slice(offset), out _deliveryTag);
65+
offset += WireFormatting.ReadBits(span.Slice(offset), out _redelivered);
66+
offset += WireFormatting.ReadShortstr(span.Slice(offset), out _exchange);
67+
WireFormatting.ReadShortstr(span.Slice(offset), out _routingKey);
68+
}
69+
6170
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicDeliver;
6271
public override string ProtocolMethodName => "basic.deliver";
6372
public override bool HasContent => true;
@@ -90,15 +99,6 @@ public override int WriteArgumentsTo(Span<byte> span)
9099
return offset + WireFormatting.WriteShortstr(span.Slice(offset), _routingKey);
91100
}
92101

93-
public void WriteArgumentsToSpan(Span<byte> span)
94-
{
95-
WireFormatting.WriteShortstr(ref span, _consumerTag);
96-
WireFormatting.WriteLonglong(ref span, _deliveryTag);
97-
WireFormatting.WriteBits(ref span, _redelivered);
98-
WireFormatting.WriteShortstr(ref span, _exchange);
99-
WireFormatting.WriteShortstr(ref span, _routingKey);
100-
}
101-
102102
public override int GetRequiredBufferSize()
103103
{
104104
int bufferSize = 1 + 8 + 1 + 1 + 1; // bytes for length of _consumerTag, _deliveryTag, bit fields, length of _exchange, length of _routingKey

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public BasicGet(ushort Reserved1, string Queue, bool NoAck)
5353
_noAck = NoAck;
5454
}
5555

56+
public BasicGet(ReadOnlySpan<byte> span)
57+
{
58+
int offset = WireFormatting.ReadShort(span, out _reserved1);
59+
offset += WireFormatting.ReadShortstr(span.Slice(offset), out _queue);
60+
WireFormatting.ReadBits(span.Slice(offset), out _noAck);
61+
}
62+
5663
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicGet;
5764
public override string ProtocolMethodName => "basic.get";
5865
public override bool HasContent => false;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public BasicGetEmpty(string Reserved1)
4949
_reserved1 = Reserved1;
5050
}
5151

52+
public BasicGetEmpty(ReadOnlySpan<byte> span)
53+
{
54+
WireFormatting.ReadShortstr(span, out _reserved1);
55+
}
56+
5257
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicGetEmpty;
5358
public override string ProtocolMethodName => "basic.get-empty";
5459
public override bool HasContent => false;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ public BasicGetOk(ulong DeliveryTag, bool Redelivered, string Exchange, string R
5757
_messageCount = MessageCount;
5858
}
5959

60+
public BasicGetOk(ReadOnlySpan<byte> span)
61+
{
62+
int offset = WireFormatting.ReadLonglong(span, out _deliveryTag);
63+
offset += WireFormatting.ReadBits(span.Slice(offset), out _redelivered);
64+
offset += WireFormatting.ReadShortstr(span.Slice(offset), out _exchange);
65+
offset += WireFormatting.ReadShortstr(span.Slice(offset), out _routingKey);
66+
WireFormatting.ReadLong(span.Slice(offset), out _messageCount);
67+
}
68+
6069
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicGetOk;
6170
public override string ProtocolMethodName => "basic.get-ok";
6271
public override bool HasContent => true;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public BasicNack(ulong DeliveryTag, bool Multiple, bool Requeue)
5252
_requeue = Requeue;
5353
}
5454

55+
public BasicNack(ReadOnlySpan<byte> span)
56+
{
57+
int offset = WireFormatting.ReadLonglong(span, out _deliveryTag);
58+
WireFormatting.ReadBits(span.Slice(offset), out _multiple, out _requeue);
59+
}
60+
5561
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicNack;
5662
public override string ProtocolMethodName => "basic.nack";
5763
public override bool HasContent => false;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public BasicPublish(ushort Reserved1, string Exchange, string RoutingKey, bool M
5757
_immediate = Immediate;
5858
}
5959

60+
public BasicPublish(ReadOnlySpan<byte> span)
61+
{
62+
int offset = WireFormatting.ReadShort(span, out _reserved1);
63+
offset += WireFormatting.ReadShortstr(span.Slice(offset), out _exchange);
64+
offset += WireFormatting.ReadShortstr(span.Slice(offset), out _routingKey);
65+
WireFormatting.ReadBits(span.Slice(offset), out _mandatory, out _immediate);
66+
}
67+
6068
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicPublish;
6169
public override string ProtocolMethodName => "basic.publish";
6270
public override bool HasContent => true;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public BasicQos(uint PrefetchSize, ushort PrefetchCount, bool Global)
5252
_global = Global;
5353
}
5454

55+
public BasicQos(ReadOnlySpan<byte> span)
56+
{
57+
int offset = WireFormatting.ReadLong(span, out _prefetchSize);
58+
offset += WireFormatting.ReadShort(span.Slice(offset), out _prefetchCount);
59+
WireFormatting.ReadBits(span.Slice(offset), out _global);
60+
}
61+
5562
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicQos;
5663
public override string ProtocolMethodName => "basic.qos";
5764
public override bool HasContent => false;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public BasicRecover(bool Requeue)
4848
_requeue = Requeue;
4949
}
5050

51+
public BasicRecover(ReadOnlySpan<byte> span)
52+
{
53+
WireFormatting.ReadBits(span, out _requeue);
54+
}
55+
5156
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicRecover;
5257
public override string ProtocolMethodName => "basic.recover";
5358
public override bool HasContent => false;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public BasicRecoverAsync(bool Requeue)
4848
_requeue = Requeue;
4949
}
5050

51+
public BasicRecoverAsync(ReadOnlySpan<byte> span)
52+
{
53+
WireFormatting.ReadBits(span, out _requeue);
54+
}
55+
5156
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicRecoverAsync;
5257
public override string ProtocolMethodName => "basic.recover-async";
5358
public override bool HasContent => false;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public BasicReject(ulong DeliveryTag, bool Requeue)
5050
_requeue = Requeue;
5151
}
5252

53+
public BasicReject(ReadOnlySpan<byte> span)
54+
{
55+
int offset = WireFormatting.ReadLonglong(span, out _deliveryTag);
56+
WireFormatting.ReadBits(span.Slice(offset), out _requeue);
57+
}
58+
5359
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicReject;
5460
public override string ProtocolMethodName => "basic.reject";
5561
public override bool HasContent => false;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ public BasicReturn(ushort ReplyCode, string ReplyText, string Exchange, string R
5555
_routingKey = RoutingKey;
5656
}
5757

58+
public BasicReturn(ReadOnlySpan<byte> span)
59+
{
60+
int offset = WireFormatting.ReadShort(span, out _replyCode);
61+
offset += WireFormatting.ReadShortstr(span.Slice(offset), out _replyText);
62+
offset += WireFormatting.ReadShortstr(span.Slice(offset), out _exchange);
63+
WireFormatting.ReadShortstr(span.Slice(offset), out _routingKey);
64+
}
65+
5866
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicReturn;
5967
public override string ProtocolMethodName => "basic.return";
6068
public override bool HasContent => true;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ public ChannelClose(ushort ReplyCode, string ReplyText, ushort ClassId, ushort M
5555
_methodId = MethodId;
5656
}
5757

58+
public ChannelClose(ReadOnlySpan<byte> span)
59+
{
60+
int offset = WireFormatting.ReadShort(span, out _replyCode);
61+
offset += WireFormatting.ReadShortstr(span.Slice(offset), out _replyText);
62+
offset += WireFormatting.ReadShort(span.Slice(offset), out _classId);
63+
WireFormatting.ReadShort(span.Slice(offset), out _methodId);
64+
}
65+
5866
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.ChannelClose;
5967
public override string ProtocolMethodName => "channel.close";
6068
public override bool HasContent => false;
@@ -83,14 +91,6 @@ public override int WriteArgumentsTo(Span<byte> span)
8391
return offset + WireFormatting.WriteShort(span.Slice(offset), _methodId);
8492
}
8593

86-
public void WriteArgumentsToSpan(Span<byte> span)
87-
{
88-
WireFormatting.WriteShort(ref span, _replyCode);
89-
WireFormatting.WriteShortstr(ref span, _replyText);
90-
WireFormatting.WriteShort(ref span, _classId);
91-
WireFormatting.WriteShort(ref span, _methodId);
92-
}
93-
9494
public override int GetRequiredBufferSize()
9595
{
9696
int bufferSize = 2 + 1 + 2 + 2; // bytes for _replyCode, length of _replyText, _classId, _methodId

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
using System;
3333
using RabbitMQ.Client.client.framing;
34-
using RabbitMQ.Client.Impl;
3534

3635
namespace RabbitMQ.Client.Framing.Impl
3736
{

0 commit comments

Comments
 (0)