-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,14 +228,19 @@ protected AkkaPduCodec(ActorSystem system) | |
/// <returns>TBD</returns> | ||
public virtual ByteString EncodePdu(IAkkaPdu pdu) | ||
{ | ||
ByteString finalBytes = null; | ||
pdu.Match() | ||
.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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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;
|
||
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> | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
@@ -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) | ||
|
@@ -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() | ||
{ | ||
|
@@ -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"); | ||
|
There was a problem hiding this comment.
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# 7switch
statement instead. Does not allocate.