Skip to content

Commit

Permalink
Merge pull request #848 from bollhals/reduce.allocation
Browse files Browse the repository at this point in the history
eliminate allocations from InboundFrame

(cherry picked from commit 2bf5849)
  • Loading branch information
michaelklishin committed May 25, 2020
1 parent dee726a commit ce9b039
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/CommandAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public CommandAssembler(ProtocolBase protocol)
Reset();
}

public Command HandleFrame(InboundFrame f)
public Command HandleFrame(in InboundFrame f)
{
switch (m_state)
{
Expand Down
4 changes: 2 additions & 2 deletions projects/RabbitMQ.Client/client/impl/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public void MainLoopIteration()
// quiescing situation, even though technically we
// should be ignoring everything except
// connection.close-ok.
_session0.HandleFrame(frame);
_session0.HandleFrame(in frame);
}
else
{
Expand All @@ -608,7 +608,7 @@ public void MainLoopIteration()
}
else
{
session.HandleFrame(frame);
session.HandleFrame(in frame);
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions projects/RabbitMQ.Client/client/impl/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,17 @@ internal int GetMinimumBufferSize()
}
}

class InboundFrame : Frame, IDisposable
internal readonly struct InboundFrame : IDisposable
{
private InboundFrame(FrameType type, int channel, ReadOnlyMemory<byte> payload) : base(type, channel, payload)
public readonly ReadOnlyMemory<byte> Payload;
public readonly int Channel;
public readonly FrameType Type;

private InboundFrame(FrameType type, int channel, ReadOnlyMemory<byte> payload)
{
Payload = payload;
Type = type;
Channel = channel;
}

public bool IsMethod()
Expand Down Expand Up @@ -289,6 +296,11 @@ public void Dispose()
ArrayPool<byte>.Shared.Return(segment.Array);
}
}

public override string ToString()
{
return $"(type={Type}, channel={Channel}, {Payload.Length} bytes of payload)";
}
}

class Frame
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ interface ISession

void Close(ShutdownEventArgs reason);
void Close(ShutdownEventArgs reason, bool notify);
void HandleFrame(InboundFrame frame);
void HandleFrame(in InboundFrame frame);
void Notify();
void Transmit(Command cmd);
void Transmit(IList<Command> cmd);
Expand Down
6 changes: 3 additions & 3 deletions projects/RabbitMQ.Client/client/impl/MainSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ public MainSession(Connection connection) : base(connection, 0)

public Action Handler { get; set; }

public override void HandleFrame(InboundFrame frame)
public override void HandleFrame(in InboundFrame frame)
{
lock (_closingLock)
{
if (!_closing)
{
base.HandleFrame(frame);
base.HandleFrame(in frame);
return;
}
}
Expand All @@ -88,7 +88,7 @@ public override void HandleFrame(InboundFrame frame)
if ((method.ProtocolClassId == _closeClassId)
&& (method.ProtocolMethodId == _closeMethodId))
{
base.HandleFrame(frame);
base.HandleFrame(in frame);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/QuiescingSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public QuiescingSession(Connection connection,
m_reason = reason;
}

public override void HandleFrame(InboundFrame frame)
public override void HandleFrame(in InboundFrame frame)
{
if (frame.IsMethod())
{
Expand Down
4 changes: 2 additions & 2 deletions projects/RabbitMQ.Client/client/impl/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public Session(Connection connection, int channelNumber)
_assembler = new CommandAssembler(connection.Protocol);
}

public override void HandleFrame(InboundFrame frame)
public override void HandleFrame(in InboundFrame frame)
{
using (Command cmd = _assembler.HandleFrame(frame))
using (Command cmd = _assembler.HandleFrame(in frame))
{
if (cmd != null)
{
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/SessionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void Close(ShutdownEventArgs reason, bool notify)
}
}

public abstract void HandleFrame(InboundFrame frame);
public abstract void HandleFrame(in InboundFrame frame);

public void Notify()
{
Expand Down

0 comments on commit ce9b039

Please sign in to comment.