Skip to content

Commit d0d3032

Browse files
committed
spanification of WireFormatting & Reader/Writer
1 parent 4c34f6c commit d0d3032

15 files changed

+152
-163
lines changed

projects/Apigen/apigen/Apigen.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -952,14 +952,14 @@ public void EmitClassMethodImplementations(AmqpClass c)
952952

953953
public void EmitMethodArgumentReader()
954954
{
955-
EmitLine(" internal override Client.Impl.MethodBase DecodeMethodFrom(ReadOnlyMemory<byte> memory)");
955+
EmitLine(" internal override Client.Impl.MethodBase DecodeMethodFrom(ReadOnlySpan<byte> span)");
956956
EmitLine(" {");
957-
EmitLine(" ushort classId = Util.NetworkOrderDeserializer.ReadUInt16(memory.Span);");
958-
EmitLine(" ushort methodId = Util.NetworkOrderDeserializer.ReadUInt16(memory.Slice(2).Span);");
957+
EmitLine(" ushort classId = Util.NetworkOrderDeserializer.ReadUInt16(span);");
958+
EmitLine(" ushort methodId = Util.NetworkOrderDeserializer.ReadUInt16(span.Slice(2));");
959959
EmitLine(" Client.Impl.MethodBase result = DecodeMethodFrom(classId, methodId);");
960960
EmitLine(" if(result != null)");
961961
EmitLine(" {");
962-
EmitLine(" Client.Impl.MethodArgumentReader reader = new Client.Impl.MethodArgumentReader(memory.Slice(4));");
962+
EmitLine(" Client.Impl.MethodArgumentReader reader = new Client.Impl.MethodArgumentReader(span.Slice(4));");
963963
EmitLine(" result.ReadArgumentsFrom(ref reader);");
964964
EmitLine(" return result;");
965965
EmitLine(" }");

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@ public Command HandleFrame(in InboundFrame f)
8181
{
8282
throw new UnexpectedFrameException(f.Type);
8383
}
84-
m_method = m_protocol.DecodeMethodFrom(f.Payload);
84+
m_method = m_protocol.DecodeMethodFrom(f.Payload.Span);
8585
m_state = m_method.HasContent ? AssemblyState.ExpectingContentHeader : AssemblyState.Complete;
8686
return CompletedCommand();
8787
case AssemblyState.ExpectingContentHeader:
8888
if (!f.IsHeader())
8989
{
9090
throw new UnexpectedFrameException(f.Type);
9191
}
92-
m_header = m_protocol.DecodeContentHeaderFrom(NetworkOrderDeserializer.ReadUInt16(f.Payload.Span));
93-
ulong totalBodyBytes = m_header.ReadFrom(f.Payload.Slice(2));
92+
93+
ReadOnlySpan<byte> span = f.Payload.Span;
94+
m_header = m_protocol.DecodeContentHeaderFrom(NetworkOrderDeserializer.ReadUInt16(span));
95+
ulong totalBodyBytes = m_header.ReadFrom(span.Slice(2));
9496
if (totalBodyBytes > MaxArrayOfBytesSize)
9597
{
9698
throw new UnexpectedFrameException(f.Type);

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public virtual object Clone()
6767
///<summary>
6868
/// Fill this instance from the given byte buffer stream.
6969
///</summary>
70-
internal ulong ReadFrom(ReadOnlyMemory<byte> memory)
70+
internal ulong ReadFrom(ReadOnlySpan<byte> span)
7171
{
7272
// Skipping the first two bytes since they arent used (weight - not currently used)
73-
ulong bodySize = NetworkOrderDeserializer.ReadUInt64(memory.Slice(2).Span);
74-
ContentHeaderPropertyReader reader = new ContentHeaderPropertyReader(memory.Slice(10));
73+
ulong bodySize = NetworkOrderDeserializer.ReadUInt64(span.Slice(2));
74+
ContentHeaderPropertyReader reader = new ContentHeaderPropertyReader(span.Slice(10));
7575
ReadPropertiesFrom(ref reader);
7676
return bodySize;
7777
}
@@ -81,13 +81,12 @@ internal ulong ReadFrom(ReadOnlyMemory<byte> memory)
8181

8282
private const ushort ZERO = 0;
8383

84-
internal int WriteTo(Memory<byte> memory, ulong bodySize)
84+
internal int WriteTo(Span<byte> span, ulong bodySize)
8585
{
86-
var span = memory.Span;
8786
NetworkOrderSerializer.WriteUInt16(span, ZERO); // Weight - not used
8887
NetworkOrderSerializer.WriteUInt64(span.Slice(2), bodySize);
8988

90-
ContentHeaderPropertyWriter writer = new ContentHeaderPropertyWriter(memory.Slice(10));
89+
ContentHeaderPropertyWriter writer = new ContentHeaderPropertyWriter(span.Slice(10));
9190
WritePropertiesTo(ref writer);
9291
return 10 + writer.Offset;
9392
}

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,16 @@ internal ref struct ContentHeaderPropertyReader
5050
private const int StartBitMask = 0b1000_0000_0000_0000;
5151
private const int EndBitMask = 0b0000_0000_0000_0001;
5252

53-
private readonly ReadOnlyMemory<byte> _memory;
5453
private readonly ReadOnlySpan<byte> _span;
5554
private int _offset;
5655
private int _bitMask;
5756
private int _bits;
5857

5958
private ReadOnlySpan<byte> Span => _span.Slice(_offset);
60-
private ReadOnlyMemory<byte> Memory => _memory.Slice(_offset);
6159

62-
public ContentHeaderPropertyReader(ReadOnlyMemory<byte> memory)
60+
public ContentHeaderPropertyReader(ReadOnlySpan<byte> span)
6361
{
64-
_memory = memory;
65-
_span = memory.Span;
62+
_span = span;
6663
_offset = 0;
6764
_bitMask = EndBitMask; // force a flag read
6865
_bits = 1; // just the continuation bit
@@ -141,15 +138,15 @@ public ushort ReadShort()
141138

142139
public string ReadShortstr()
143140
{
144-
string result = WireFormatting.ReadShortstr(Memory, out int bytesRead);
141+
string result = WireFormatting.ReadShortstr(Span, out int bytesRead);
145142
_offset += bytesRead;
146143
return result;
147144
}
148145

149146
/// <returns>A type of <seealso cref="System.Collections.Generic.IDictionary{TKey,TValue}"/>.</returns>
150147
public Dictionary<string, object> ReadTable()
151148
{
152-
Dictionary<string, object> result = WireFormatting.ReadTable(Memory, out int bytesRead);
149+
Dictionary<string, object> result = WireFormatting.ReadTable(Span, out int bytesRead);
153150
_offset += bytesRead;
154151
return result;
155152
}

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ internal ref struct ContentHeaderPropertyWriter
5050
private const ushort StartBitMask = 0b1000_0000_0000_0000;
5151
private const ushort EndBitMask = 0b0000_0000_0000_0001;
5252

53-
private readonly Memory<byte> _memory;
5453
private readonly Span<byte> _span;
5554
private int _offset;
5655
private ushort _bitAccumulator;
@@ -59,12 +58,10 @@ internal ref struct ContentHeaderPropertyWriter
5958
public int Offset => _offset;
6059

6160
private Span<byte> Span => _span.Slice(_offset);
62-
private Memory<byte> Memory => _memory.Slice(_offset);
6361

64-
public ContentHeaderPropertyWriter(Memory<byte> memory)
62+
public ContentHeaderPropertyWriter(Span<byte> span)
6563
{
66-
_memory = memory;
67-
_span = _memory.Span;
64+
_span = span;
6865
_offset = 0;
6966
_bitAccumulator = 0;
7067
_bitMask = StartBitMask;
@@ -124,12 +121,12 @@ public void WriteShort(ushort val)
124121

125122
public void WriteShortstr(string val)
126123
{
127-
_offset += WireFormatting.WriteShortstr(Memory, val);
124+
_offset += WireFormatting.WriteShortstr(Span, val);
128125
}
129126

130127
public void WriteTable(IDictionary<string, object> val)
131128
{
132-
_offset += WireFormatting.WriteTable(Memory, val);
129+
_offset += WireFormatting.WriteTable(Span, val);
133130
}
134131

135132
public void WriteTimestamp(AmqpTimestamp val)

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

+11-13
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ internal override int GetMinimumPayloadBufferSize()
6666
return 2 + _header.GetRequiredBufferSize();
6767
}
6868

69-
internal override int WritePayload(Memory<byte> memory)
69+
internal override int WritePayload(Span<byte> span)
7070
{
7171
// write protocol class id (2 bytes)
72-
NetworkOrderSerializer.WriteUInt16(memory.Span, _header.ProtocolClassId);
72+
NetworkOrderSerializer.WriteUInt16(span, _header.ProtocolClassId);
7373
// write header (X bytes)
74-
int bytesWritten = _header.WriteTo(memory.Slice(2), (ulong)_bodyLength);
74+
int bytesWritten = _header.WriteTo(span.Slice(2), (ulong)_bodyLength);
7575
return bytesWritten + 2;
7676
}
7777
}
@@ -90,9 +90,9 @@ internal override int GetMinimumPayloadBufferSize()
9090
return _body.Length;
9191
}
9292

93-
internal override int WritePayload(Memory<byte> memory)
93+
internal override int WritePayload(Span<byte> span)
9494
{
95-
_body.CopyTo(memory);
95+
_body.Span.CopyTo(span);
9696
return _body.Length;
9797
}
9898
}
@@ -112,12 +112,11 @@ internal override int GetMinimumPayloadBufferSize()
112112
return 4 + _method.GetRequiredBufferSize();
113113
}
114114

115-
internal override int WritePayload(Memory<byte> memory)
115+
internal override int WritePayload(Span<byte> span)
116116
{
117-
var span = memory.Span;
118117
NetworkOrderSerializer.WriteUInt16(span, _method.ProtocolClassId);
119118
NetworkOrderSerializer.WriteUInt16(span.Slice(2), _method.ProtocolMethodId);
120-
var argWriter = new MethodArgumentWriter(memory.Slice(4));
119+
var argWriter = new MethodArgumentWriter(span.Slice(4));
121120
_method.WriteArgumentsTo(ref argWriter);
122121
return 4 + argWriter.Offset;
123122
}
@@ -134,7 +133,7 @@ internal override int GetMinimumPayloadBufferSize()
134133
return 0;
135134
}
136135

137-
internal override int WritePayload(Memory<byte> memory)
136+
internal override int WritePayload(Span<byte> span)
138137
{
139138
return 0;
140139
}
@@ -151,17 +150,16 @@ protected OutboundFrame(FrameType type, int channel)
151150
Channel = channel;
152151
}
153152

154-
internal void WriteTo(Memory<byte> memory)
153+
internal void WriteTo(Span<byte> span)
155154
{
156-
var span = memory.Span;
157155
span[0] = (byte)Type;
158156
NetworkOrderSerializer.WriteUInt16(span.Slice(1), (ushort)Channel);
159-
int bytesWritten = WritePayload(memory.Slice(7));
157+
int bytesWritten = WritePayload(span.Slice(7));
160158
NetworkOrderSerializer.WriteUInt32(span.Slice(3), (uint)bytesWritten);
161159
span[bytesWritten + 7] = Constants.FrameEnd;
162160
}
163161

164-
internal abstract int WritePayload(Memory<byte> memory);
162+
internal abstract int WritePayload(Span<byte> span);
165163
internal abstract int GetMinimumPayloadBufferSize();
166164
internal int GetMinimumBufferSize()
167165
{

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public override void HandleFrame(in InboundFrame frame)
8484

8585
if (!_closeServerInitiated && frame.IsMethod())
8686
{
87-
MethodBase method = Connection.Protocol.DecodeMethodFrom(frame.Payload);
87+
MethodBase method = Connection.Protocol.DecodeMethodFrom(frame.Payload.Span);
8888
if ((method.ProtocolClassId == _closeClassId)
8989
&& (method.ProtocolMethodId == _closeMethodId))
9090
{

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,16 @@ namespace RabbitMQ.Client.Impl
4747
{
4848
internal ref struct MethodArgumentReader
4949
{
50-
private readonly ReadOnlyMemory<byte> _memory;
5150
private readonly ReadOnlySpan<byte> _span;
5251
private int _offset;
5352
private int _bitMask;
5453
private int _bits;
5554

5655
private ReadOnlySpan<byte> Span => _span.Slice(_offset);
57-
private ReadOnlyMemory<byte> Memory => _memory.Slice(_offset);
5856

59-
public MethodArgumentReader(ReadOnlyMemory<byte> memory)
57+
public MethodArgumentReader(ReadOnlySpan<byte> span)
6058
{
61-
_memory = memory;
62-
_span = memory.Span;
59+
_span = span;
6360
_offset = 0;
6461
_bitMask = 0;
6562
_bits = 0;
@@ -119,14 +116,14 @@ public ushort ReadShort()
119116

120117
public string ReadShortstr()
121118
{
122-
string result = WireFormatting.ReadShortstr(Memory, out int bytesRead);
119+
string result = WireFormatting.ReadShortstr(Span, out int bytesRead);
123120
_offset += bytesRead;
124121
return result;
125122
}
126123

127124
public Dictionary<string, object> ReadTable()
128125
{
129-
Dictionary<string, object> result = WireFormatting.ReadTable(Memory, out int bytesRead);
126+
Dictionary<string, object> result = WireFormatting.ReadTable(Span, out int bytesRead);
130127
_offset += bytesRead;
131128
return result;
132129
}

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ namespace RabbitMQ.Client.Impl
4646
{
4747
internal ref struct MethodArgumentWriter
4848
{
49-
private readonly Memory<byte> _memory;
5049
private readonly Span<byte> _span;
5150
private int _offset;
5251
private int _bitAccumulator;
@@ -55,12 +54,10 @@ internal ref struct MethodArgumentWriter
5554
public int Offset => _offset;
5655

5756
private Span<byte> Span => _span.Slice(_offset);
58-
private Memory<byte> Memory => _memory.Slice(_offset);
5957

60-
public MethodArgumentWriter(Memory<byte> memory)
58+
public MethodArgumentWriter(Span<byte> span)
6159
{
62-
_memory = memory;
63-
_span = memory.Span;
60+
_span = span;
6461
_offset = 0;
6562
_bitAccumulator = 0;
6663
_bitMask = 1;
@@ -117,17 +114,17 @@ public void WriteShort(ushort val)
117114

118115
public void WriteShortstr(string val)
119116
{
120-
_offset += WireFormatting.WriteShortstr(Memory, val);
117+
_offset += WireFormatting.WriteShortstr(Span, val);
121118
}
122119

123120
public void WriteTable(IDictionary val)
124121
{
125-
_offset += WireFormatting.WriteTable(Memory, val);
122+
_offset += WireFormatting.WriteTable(Span, val);
126123
}
127124

128125
public void WriteTable(IDictionary<string, object> val)
129126
{
130-
_offset += WireFormatting.WriteTable(Memory, val);
127+
_offset += WireFormatting.WriteTable(Span, val);
131128
}
132129

133130
public void WriteTimestamp(AmqpTimestamp val)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void CreateConnectionClose(ushort reasonCode,
106106
}
107107

108108
internal abstract ContentHeaderBase DecodeContentHeaderFrom(ushort classId);
109-
internal abstract MethodBase DecodeMethodFrom(ReadOnlyMemory<byte> reader);
109+
internal abstract MethodBase DecodeMethodFrom(ReadOnlySpan<byte> reader);
110110

111111
public override bool Equals(object obj)
112112
{

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override void HandleFrame(in InboundFrame frame)
5959
{
6060
if (frame.IsMethod())
6161
{
62-
MethodBase method = Connection.Protocol.DecodeMethodFrom(frame.Payload);
62+
MethodBase method = Connection.Protocol.DecodeMethodFrom(frame.Payload.Span);
6363
if ((method.ProtocolClassId == ClassConstants.Channel)
6464
&& (method.ProtocolMethodId == ChannelMethodConstants.CloseOk))
6565
{

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ public async Task WriteFrameImpl()
252252
{
253253
int bufferSize = frame.GetMinimumBufferSize();
254254
byte[] memoryArray = ArrayPool<byte>.Shared.Rent(bufferSize);
255-
Memory<byte> slice = new Memory<byte>(memoryArray, 0, bufferSize);
256-
frame.WriteTo(slice);
255+
frame.WriteTo(new Span<byte>(memoryArray, 0, bufferSize));
257256
_writer.Write(memoryArray, 0, bufferSize);
258257
ArrayPool<byte>.Shared.Return(memoryArray);
259258
}

0 commit comments

Comments
 (0)