Skip to content

Commit 8778edf

Browse files
Stefán J. Sigurðarsonlukebakken
authored andcommitted
Use ReadOnlyMemory where possible.
1 parent 3a2d8fc commit 8778edf

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
internal 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
{

0 commit comments

Comments
 (0)