Skip to content

Commit ce9b039

Browse files
Merge pull request #848 from bollhals/reduce.allocation
eliminate allocations from InboundFrame (cherry picked from commit 2bf5849)
1 parent dee726a commit ce9b039

File tree

8 files changed

+25
-13
lines changed

8 files changed

+25
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public CommandAssembler(ProtocolBase protocol)
7272
Reset();
7373
}
7474

75-
public Command HandleFrame(InboundFrame f)
75+
public Command HandleFrame(in InboundFrame f)
7676
{
7777
switch (m_state)
7878
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ public void MainLoopIteration()
586586
// quiescing situation, even though technically we
587587
// should be ignoring everything except
588588
// connection.close-ok.
589-
_session0.HandleFrame(frame);
589+
_session0.HandleFrame(in frame);
590590
}
591591
else
592592
{
@@ -608,7 +608,7 @@ public void MainLoopIteration()
608608
}
609609
else
610610
{
611-
session.HandleFrame(frame);
611+
session.HandleFrame(in frame);
612612
}
613613
}
614614
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,17 @@ internal int GetMinimumBufferSize()
165165
}
166166
}
167167

168-
class InboundFrame : Frame, IDisposable
168+
internal readonly struct InboundFrame : IDisposable
169169
{
170-
private InboundFrame(FrameType type, int channel, ReadOnlyMemory<byte> payload) : base(type, channel, payload)
170+
public readonly ReadOnlyMemory<byte> Payload;
171+
public readonly int Channel;
172+
public readonly FrameType Type;
173+
174+
private InboundFrame(FrameType type, int channel, ReadOnlyMemory<byte> payload)
171175
{
176+
Payload = payload;
177+
Type = type;
178+
Channel = channel;
172179
}
173180

174181
public bool IsMethod()
@@ -289,6 +296,11 @@ public void Dispose()
289296
ArrayPool<byte>.Shared.Return(segment.Array);
290297
}
291298
}
299+
300+
public override string ToString()
301+
{
302+
return $"(type={Type}, channel={Channel}, {Payload.Length} bytes of payload)";
303+
}
292304
}
293305

294306
class Frame

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ interface ISession
7878

7979
void Close(ShutdownEventArgs reason);
8080
void Close(ShutdownEventArgs reason, bool notify);
81-
void HandleFrame(InboundFrame frame);
81+
void HandleFrame(in InboundFrame frame);
8282
void Notify();
8383
void Transmit(Command cmd);
8484
void Transmit(IList<Command> cmd);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public MainSession(Connection connection) : base(connection, 0)
7171

7272
public Action Handler { get; set; }
7373

74-
public override void HandleFrame(InboundFrame frame)
74+
public override void HandleFrame(in InboundFrame frame)
7575
{
7676
lock (_closingLock)
7777
{
7878
if (!_closing)
7979
{
80-
base.HandleFrame(frame);
80+
base.HandleFrame(in frame);
8181
return;
8282
}
8383
}
@@ -88,7 +88,7 @@ public override void HandleFrame(InboundFrame frame)
8888
if ((method.ProtocolClassId == _closeClassId)
8989
&& (method.ProtocolMethodId == _closeMethodId))
9090
{
91-
base.HandleFrame(frame);
91+
base.HandleFrame(in frame);
9292
return;
9393
}
9494

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public QuiescingSession(Connection connection,
5555
m_reason = reason;
5656
}
5757

58-
public override void HandleFrame(InboundFrame frame)
58+
public override void HandleFrame(in InboundFrame frame)
5959
{
6060
if (frame.IsMethod())
6161
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public Session(Connection connection, int channelNumber)
5353
_assembler = new CommandAssembler(connection.Protocol);
5454
}
5555

56-
public override void HandleFrame(InboundFrame frame)
56+
public override void HandleFrame(in InboundFrame frame)
5757
{
58-
using (Command cmd = _assembler.HandleFrame(frame))
58+
using (Command cmd = _assembler.HandleFrame(in frame))
5959
{
6060
if (cmd != null)
6161
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void Close(ShutdownEventArgs reason, bool notify)
153153
}
154154
}
155155

156-
public abstract void HandleFrame(InboundFrame frame);
156+
public abstract void HandleFrame(in InboundFrame frame);
157157

158158
public void Notify()
159159
{

0 commit comments

Comments
 (0)