Skip to content

Commit bad96c3

Browse files
author
Stefán J. Sigurðarson
committed
Use ReadOnlyMemory where possible.
1 parent 8f931f3 commit bad96c3

File tree

13 files changed

+44
-55
lines changed

13 files changed

+44
-55
lines changed

projects/client/Apigen/src/apigen/Apigen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ public void EmitClassMethodImplementations(AmqpClass c)
929929

930930
public void EmitMethodArgumentReader()
931931
{
932-
EmitLine(" internal override Client.Impl.MethodBase DecodeMethodFrom(Memory<byte> memory)");
932+
EmitLine(" internal override Client.Impl.MethodBase DecodeMethodFrom(ReadOnlyMemory<byte> memory)");
933933
EmitLine(" {");
934934
EmitLine(" ushort classId = Util.NetworkOrderDeserializer.ReadUInt16(memory);");
935935
EmitLine(" ushort methodId = Util.NetworkOrderDeserializer.ReadUInt16(memory.Slice(2));");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Command : IDisposable
5555
// - 1 byte of payload trailer FrameEnd byte
5656
private const int EmptyFrameSize = 8;
5757
private static readonly byte[] s_emptyByteArray = new byte[0];
58-
private IMemoryOwner<byte> _body;
58+
private readonly IMemoryOwner<byte> _body;
5959

6060
static Command()
6161
{
@@ -167,7 +167,7 @@ internal static List<OutboundFrame> CalculateFrames(int channelNumber, Connectio
167167

168168
public void Dispose()
169169
{
170-
if(_body is IMemoryOwner<byte>)
170+
if (_body is IMemoryOwner<byte>)
171171
{
172172
_body.Dispose();
173173
}

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

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

41-
using System;
4241
using System.Buffers;
43-
using System.IO;
4442

4543
using RabbitMQ.Client.Framing.Impl;
4644
using RabbitMQ.Util;
@@ -82,17 +80,16 @@ public Command HandleFrame(InboundFrame f)
8280
{
8381
throw new UnexpectedFrameException(f);
8482
}
85-
m_method = m_protocol.DecodeMethodFrom(f.Payload.AsMemory(0, f.PayloadSize));
83+
m_method = m_protocol.DecodeMethodFrom(f.Payload);
8684
m_state = m_method.HasContent ? AssemblyState.ExpectingContentHeader : AssemblyState.Complete;
8785
return CompletedCommand();
8886
case AssemblyState.ExpectingContentHeader:
8987
if (!f.IsHeader())
9088
{
9189
throw new UnexpectedFrameException(f);
9290
}
93-
Memory<byte> memory = f.Payload.AsMemory(0, f.PayloadSize);
94-
m_header = m_protocol.DecodeContentHeaderFrom(NetworkOrderDeserializer.ReadUInt16(memory));
95-
ulong totalBodyBytes = m_header.ReadFrom(memory.Slice(2));
91+
m_header = m_protocol.DecodeContentHeaderFrom(NetworkOrderDeserializer.ReadUInt16(f.Payload));
92+
ulong totalBodyBytes = m_header.ReadFrom(f.Payload.Slice(2));
9693
if (totalBodyBytes > MaxArrayOfBytesSize)
9794
{
9895
throw new UnexpectedFrameException(f);
@@ -108,14 +105,14 @@ public Command HandleFrame(InboundFrame f)
108105
throw new UnexpectedFrameException(f);
109106
}
110107

111-
if (f.PayloadSize > m_remainingBodyBytes)
108+
if (f.Payload.Length > m_remainingBodyBytes)
112109
{
113-
throw new MalformedFrameException($"Overlong content body received - {m_remainingBodyBytes} bytes remaining, {f.PayloadSize} bytes received");
110+
throw new MalformedFrameException($"Overlong content body received - {m_remainingBodyBytes} bytes remaining, {f.Payload.Length} bytes received");
114111
}
115112

116-
f.Payload.AsMemory().Slice(0, f.PayloadSize).CopyTo(m_body.Memory.Slice(_offset, f.PayloadSize));
117-
m_remainingBodyBytes -= f.PayloadSize;
118-
_offset += f.PayloadSize;
113+
f.Payload.CopyTo(m_body.Memory.Slice(_offset));
114+
m_remainingBodyBytes -= f.Payload.Length;
115+
_offset += f.Payload.Length;
119116
UpdateContentBodyState();
120117
return CompletedCommand();
121118
case AssemblyState.Complete:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public virtual object Clone()
6767
///<summary>
6868
/// Fill this instance from the given byte buffer stream.
6969
///</summary>
70-
internal ulong ReadFrom(Memory<byte> memory)
70+
internal ulong ReadFrom(ReadOnlyMemory<byte> memory)
7171
{
7272
// Skipping the first two bytes since they arent used (weight - not currently used)
7373
ulong bodySize = NetworkOrderDeserializer.ReadUInt64(memory.Slice(2));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class ContentHeaderPropertyReader
5050
protected ushort m_bitCount;
5151
protected ushort m_flagWord;
5252
private int _memoryOffset = 0;
53-
private readonly Memory<byte> _memory;
53+
private readonly ReadOnlyMemory<byte> _memory;
5454

55-
public ContentHeaderPropertyReader(Memory<byte> memory)
55+
public ContentHeaderPropertyReader(ReadOnlyMemory<byte> memory)
5656
{
5757
_memory = memory;
5858
m_flagWord = 1; // just the continuation bit

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal override int WritePayload(Memory<byte> memory)
7878

7979
class BodySegmentOutboundFrame : OutboundFrame
8080
{
81-
private ReadOnlyMemory<byte> _body;
81+
private readonly ReadOnlyMemory<byte> _body;
8282

8383
public BodySegmentOutboundFrame(int channel, ReadOnlyMemory<byte> bodySegment) : base(FrameType.FrameBody, channel)
8484
{
@@ -167,10 +167,10 @@ internal int GetMinimumBufferSize()
167167

168168
class InboundFrame : Frame, IDisposable
169169
{
170-
public int PayloadSize { get; private set; }
171-
private InboundFrame(FrameType type, int channel, byte[] payload, int payloadSize) : base(type, channel, payload)
170+
private IMemoryOwner<byte> _payload;
171+
private InboundFrame(FrameType type, int channel, IMemoryOwner<byte> payload, int payloadSize) : base(type, channel, payload.Memory.Slice(0, payloadSize))
172172
{
173-
PayloadSize = payloadSize;
173+
_payload = payload;
174174
}
175175

176176
private static void ProcessProtocolHeader(Stream reader)
@@ -242,13 +242,13 @@ internal static InboundFrame ReadFrom(Stream reader)
242242
reader.Read(headerSlice);
243243
int channel = NetworkOrderDeserializer.ReadUInt16(headerSlice);
244244
int payloadSize = NetworkOrderDeserializer.ReadInt32(headerSlice.Slice(2)); // FIXME - throw exn on unreasonable value
245-
byte[] payload = ArrayPool<byte>.Shared.Rent(payloadSize);
245+
IMemoryOwner<byte> payload = MemoryPool<byte>.Shared.Rent(payloadSize);
246246
int bytesRead = 0;
247247
try
248248
{
249249
while (bytesRead < payloadSize)
250250
{
251-
bytesRead += reader.Read(payload, bytesRead, payloadSize - bytesRead);
251+
bytesRead += reader.Read(payload.Memory.Slice(bytesRead, payloadSize - bytesRead));
252252
}
253253
}
254254
catch (Exception)
@@ -269,9 +269,9 @@ internal static InboundFrame ReadFrom(Stream reader)
269269

270270
public void Dispose()
271271
{
272-
if (Payload != null)
272+
if (_payload is IMemoryOwner<byte>)
273273
{
274-
ArrayPool<byte>.Shared.Return(Payload);
274+
_payload.Dispose();
275275
}
276276
}
277277
}
@@ -285,7 +285,7 @@ public Frame(FrameType type, int channel)
285285
Payload = null;
286286
}
287287

288-
public Frame(FrameType type, int channel, byte[] payload)
288+
public Frame(FrameType type, int channel, ReadOnlyMemory<byte> payload)
289289
{
290290
Type = type;
291291
Channel = channel;
@@ -294,7 +294,7 @@ public Frame(FrameType type, int channel, byte[] payload)
294294

295295
public int Channel { get; private set; }
296296

297-
public byte[] Payload { get; private set; }
297+
public ReadOnlyMemory<byte> Payload { get; private set; }
298298

299299
public FrameType Type { get; private set; }
300300

@@ -303,9 +303,7 @@ public override string ToString()
303303
return string.Format("(type={0}, channel={1}, {2} bytes of payload)",
304304
Type,
305305
Channel,
306-
Payload == null
307-
? "(null)"
308-
: Payload.Length.ToString());
306+
Payload.Length.ToString());
309307
}
310308

311309
public bool IsMethod()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public override void HandleFrame(InboundFrame frame)
8484

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ class MethodArgumentReader
5050
private int? _bit;
5151
private int _bits;
5252

53-
public MethodArgumentReader(Memory<byte> memory)
53+
public MethodArgumentReader(ReadOnlyMemory<byte> memory)
5454
{
5555
_memory = memory;
5656
_memoryOffset = 0;
5757
ClearBits();
5858
}
5959

60-
private readonly Memory<byte> _memory;
60+
private readonly ReadOnlyMemory<byte> _memory;
6161
private int _memoryOffset;
6262

6363
public bool ReadBit()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void CreateConnectionClose(ushort reasonCode,
103103
}
104104

105105
internal abstract ContentHeaderBase DecodeContentHeaderFrom(ushort classId);
106-
internal abstract MethodBase DecodeMethodFrom(Memory<byte> reader);
106+
internal abstract MethodBase DecodeMethodFrom(ReadOnlyMemory<byte> reader);
107107

108108
public override bool Equals(object obj)
109109
{

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

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

41-
using System;
42-
4341
using RabbitMQ.Client.Framing.Impl;
4442

4543
namespace RabbitMQ.Client.Impl
@@ -61,7 +59,7 @@ public override void HandleFrame(InboundFrame frame)
6159
{
6260
if (frame.IsMethod())
6361
{
64-
MethodBase method = Connection.Protocol.DecodeMethodFrom(frame.Payload.AsMemory(0, frame.PayloadSize));
62+
MethodBase method = Connection.Protocol.DecodeMethodFrom(frame.Payload);
6563
if ((method.ProtocolClassId == ClassConstants.Channel)
6664
&& (method.ProtocolMethodId == ChannelMethodConstants.CloseOk))
6765
{

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@
3939
//---------------------------------------------------------------------------
4040

4141
using System;
42-
using System.Buffers;
4342
using System.Collections;
4443
using System.Collections.Generic;
45-
using System.IO;
4644
using System.Runtime.InteropServices;
4745
using System.Text;
4846

@@ -95,7 +93,7 @@ public static void DecimalToAmqp(decimal value, out byte scale, out int mantissa
9593
(((uint)bitRepresentation[0]) & 0x7FFFFFFF));
9694
}
9795

98-
public static IList ReadArray(Memory<byte> memory, out int bytesRead)
96+
public static IList ReadArray(ReadOnlyMemory<byte> memory, out int bytesRead)
9997
{
10098
IList array = new List<object>();
10199
long arrayLength = NetworkOrderDeserializer.ReadUInt32(memory);
@@ -110,17 +108,17 @@ public static IList ReadArray(Memory<byte> memory, out int bytesRead)
110108
return array;
111109
}
112110

113-
public static decimal ReadDecimal(Memory<byte> memory)
111+
public static decimal ReadDecimal(ReadOnlyMemory<byte> memory)
114112
{
115113
byte scale = memory.Span[0];
116114
uint unsignedMantissa = NetworkOrderDeserializer.ReadUInt32(memory.Slice(1));
117115
return AmqpToDecimal(scale, unsignedMantissa);
118116
}
119117

120-
public static object ReadFieldValue(Memory<byte> memory, out int bytesRead)
118+
public static object ReadFieldValue(ReadOnlyMemory<byte> memory, out int bytesRead)
121119
{
122120
bytesRead = 1;
123-
Memory<byte> slice = memory.Slice(1);
121+
ReadOnlyMemory<byte> slice = memory.Slice(1);
124122
switch ((char)memory.Span[0])
125123
{
126124
case 'S':
@@ -179,7 +177,7 @@ public static object ReadFieldValue(Memory<byte> memory, out int bytesRead)
179177
}
180178
}
181179

182-
public static byte[] ReadLongstr(Memory<byte> memory)
180+
public static byte[] ReadLongstr(ReadOnlyMemory<byte> memory)
183181
{
184182
int byteCount = (int)NetworkOrderDeserializer.ReadUInt32(memory);
185183
if (byteCount > int.MaxValue)
@@ -190,10 +188,10 @@ public static byte[] ReadLongstr(Memory<byte> memory)
190188
return memory.Slice(4, byteCount).ToArray();
191189
}
192190

193-
public static string ReadShortstr(Memory<byte> memory, out int bytesRead)
191+
public static string ReadShortstr(ReadOnlyMemory<byte> memory, out int bytesRead)
194192
{
195193
int byteCount = memory.Span[0];
196-
Memory<byte> stringSlice = memory.Slice(1, byteCount);
194+
ReadOnlyMemory<byte> stringSlice = memory.Slice(1, byteCount);
197195
if (MemoryMarshal.TryGetArray(stringSlice, out ArraySegment<byte> segment))
198196
{
199197
bytesRead = 1 + byteCount;
@@ -210,7 +208,7 @@ public static string ReadShortstr(Memory<byte> memory, out int bytesRead)
210208
/// x and V types and the AMQP 0-9-1 A type.
211209
///</remarks>
212210
/// <returns>A <seealso cref="System.Collections.Generic.IDictionary{TKey,TValue}"/>.</returns>
213-
public static IDictionary<string, object> ReadTable(Memory<byte> memory, out int bytesRead)
211+
public static IDictionary<string, object> ReadTable(ReadOnlyMemory<byte> memory, out int bytesRead)
214212
{
215213
IDictionary<string, object> table = new Dictionary<string, object>();
216214
long tableLength = NetworkOrderDeserializer.ReadUInt32(memory);
@@ -231,7 +229,7 @@ public static IDictionary<string, object> ReadTable(Memory<byte> memory, out int
231229
return table;
232230
}
233231

234-
public static AmqpTimestamp ReadTimestamp(Memory<byte> memory)
232+
public static AmqpTimestamp ReadTimestamp(ReadOnlyMemory<byte> memory)
235233
{
236234
ulong stamp = NetworkOrderDeserializer.ReadUInt64(memory);
237235
// 0-9 is afaict silent on the signedness of the timestamp.

projects/client/RabbitMQ.Client/src/util/NetworkOrderDeserializer.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Buffers.Binary;
3-
using System.Linq;
43
using System.Runtime.CompilerServices;
5-
using System.Runtime.InteropServices;
64

75
namespace RabbitMQ.Util
86
{
@@ -70,7 +68,7 @@ internal static float ReadSingle(ReadOnlySpan<byte> span)
7068
return Unsafe.As<int, float>(ref num);
7169
}
7270

73-
internal static ushort ReadUInt16(Memory<byte> memory) => ReadUInt16(memory.Span);
71+
internal static ushort ReadUInt16(ReadOnlyMemory<byte> memory) => ReadUInt16(memory.Span);
7472

7573
internal static ushort ReadUInt16(ReadOnlySpan<byte> span)
7674
{
@@ -82,7 +80,7 @@ internal static ushort ReadUInt16(ReadOnlySpan<byte> span)
8280
return BinaryPrimitives.ReadUInt16BigEndian(span);
8381
}
8482

85-
internal static uint ReadUInt32(Memory<byte> memory) => ReadUInt32(memory.Span);
83+
internal static uint ReadUInt32(ReadOnlyMemory<byte> memory) => ReadUInt32(memory.Span);
8684

8785
internal static uint ReadUInt32(ReadOnlySpan<byte> span)
8886
{
@@ -94,7 +92,7 @@ internal static uint ReadUInt32(ReadOnlySpan<byte> span)
9492
return BinaryPrimitives.ReadUInt32BigEndian(span);
9593
}
9694

97-
internal static ulong ReadUInt64(Memory<byte> memory) => ReadUInt64(memory.Span);
95+
internal static ulong ReadUInt64(ReadOnlyMemory<byte> memory) => ReadUInt64(memory.Span);
9896

9997
internal static ulong ReadUInt64(ReadOnlySpan<byte> span)
10098
{

projects/client/Unit/src/unit/TestNetworkByteOrderSerialization.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public void Check(byte[] actual, byte[] expected)
6767
}
6868
}
6969

70-
byte[] expectedDoubleBytes = new byte[] { 63, 243, 190, 118, 200, 180, 57, 88 };
71-
byte[] expectedSingleBytes = new byte[] { 63, 157, 243, 182 };
70+
readonly byte[] expectedDoubleBytes = new byte[] { 63, 243, 190, 118, 200, 180, 57, 88 };
71+
readonly byte[] expectedSingleBytes = new byte[] { 63, 157, 243, 182 };
7272

7373
[Test]
7474
public void TestSingleDecoding()

0 commit comments

Comments
 (0)