Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AkkaPduCodec performance fixes [remoting] #3299

Merged
merged 4 commits into from
Jan 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/core/Akka.Remote/Transport/AkkaPduCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,19 @@ protected AkkaPduCodec(ActorSystem system)
/// <returns>TBD</returns>
public virtual ByteString EncodePdu(IAkkaPdu pdu)
{
ByteString finalBytes = null;
pdu.Match()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of the old .Match extension method, which allocates. Replace with a C# 7 switch statement instead. Does not allocate.

.With<Associate>(a => finalBytes = ConstructAssociate(a.Info))
.With<Payload>(p => finalBytes = ConstructPayload(p.Bytes))
.With<Disassociate>(d => finalBytes = ConstructDisassociate(d.Reason))
.With<Heartbeat>(h => finalBytes = ConstructHeartbeat());

return finalBytes;
switch (pdu)
{
case Payload p:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the order in which we filter for events in Akka.Remote serialization; Payload makes up the vast majority of network traffic, since that's all of the messages that aren't part of the Akka.Remote association protocol itself. Payload was previously handled second, so we always had one case we needed to fall through each time.

Heartbeat is the second most frequently used message type, since these are emitted once per second.

return ConstructPayload(p.Bytes);
case Heartbeat h:
return ConstructHeartbeat();
case Associate a:
return ConstructAssociate(a.Info);
case Disassociate d:
return ConstructDisassociate(d.Reason);
default:
return null; // unsupported message type
}
}

/// <summary>
Expand Down Expand Up @@ -376,13 +381,20 @@ public override ByteString ConstructDisassociate(DisassociateInfo reason)
}
}

/*
* Since there's never any ActorSystem-specific information coded directly
* into the heartbeat messages themselves (i.e. no handshake info,) there's no harm in caching in the
* same heartbeat byte buffer and re-using it.
*/
private static readonly ByteString HeartbeatPdu = ConstructControlMessagePdu(CommandType.Heartbeat);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was able to make this static and immutable as well.


/// <summary>
/// TBD
/// Creates a new Heartbeat message instance.
/// </summary>
/// <returns>TBD</returns>
/// <returns>The Heartbeat message.</returns>
public override ByteString ConstructHeartbeat()
{
return ConstructControlMessagePdu(CommandType.Heartbeat);
return HeartbeatPdu;
}

/// <summary>
Expand Down Expand Up @@ -533,7 +545,7 @@ private ByteString DISASSOCIATE_QUARANTINED
get { return ConstructControlMessagePdu(CommandType.DisassociateQuarantined); }
}

private ByteString ConstructControlMessagePdu(CommandType code, AkkaHandshakeInfo handshakeInfo = null)
private static ByteString ConstructControlMessagePdu(CommandType code, AkkaHandshakeInfo handshakeInfo = null)
{
var controlMessage = new AkkaControlMessage() { CommandType = code };
if (handshakeInfo != null)
Expand All @@ -544,12 +556,12 @@ private ByteString ConstructControlMessagePdu(CommandType code, AkkaHandshakeInf
return new AkkaProtocolMessage() { Instruction = controlMessage }.ToByteString();
}

private Address DecodeAddress(AddressData origin)
private static Address DecodeAddress(AddressData origin)
{
return new Address(origin.Protocol, origin.System, origin.Hostname, (int)origin.Port);
}

private ActorRefData SerializeActorRef(Address defaultAddress, IActorRef actorRef)
private static ActorRefData SerializeActorRef(Address defaultAddress, IActorRef actorRef)
{
return new ActorRefData()
{
Expand All @@ -559,7 +571,7 @@ private ActorRefData SerializeActorRef(Address defaultAddress, IActorRef actorRe
};
}

private AddressData SerializeAddress(Address address)
private static AddressData SerializeAddress(Address address)
{
if (string.IsNullOrEmpty(address.Host) || !address.Port.HasValue)
throw new ArgumentException($"Address {address} could not be serialized: host or port missing");
Expand Down