From 9a20f8ab26f5445bcbf35b389d9f4c664231e1f5 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Fri, 26 Jan 2018 11:45:20 -0600 Subject: [PATCH 1/3] AkkaPduCodec performance fixes --- .../Akka.Remote/Transport/AkkaPduCodec.cs | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/core/Akka.Remote/Transport/AkkaPduCodec.cs b/src/core/Akka.Remote/Transport/AkkaPduCodec.cs index a9964c52195..c1432841a1a 100644 --- a/src/core/Akka.Remote/Transport/AkkaPduCodec.cs +++ b/src/core/Akka.Remote/Transport/AkkaPduCodec.cs @@ -228,14 +228,19 @@ protected AkkaPduCodec(ActorSystem system) /// TBD public virtual ByteString EncodePdu(IAkkaPdu pdu) { - ByteString finalBytes = null; - pdu.Match() - .With(a => finalBytes = ConstructAssociate(a.Info)) - .With(p => finalBytes = ConstructPayload(p.Bytes)) - .With(d => finalBytes = ConstructDisassociate(d.Reason)) - .With(h => finalBytes = ConstructHeartbeat()); - - return finalBytes; + switch (pdu) + { + case Payload p: + 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 + } } /// @@ -376,13 +381,24 @@ public override ByteString ConstructDisassociate(DisassociateInfo reason) } } + private static ByteString _heartbeatPdu; + /// /// TBD /// /// TBD public override ByteString ConstructHeartbeat() { - return ConstructControlMessagePdu(CommandType.Heartbeat); + /* + * 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. + */ + if (_heartbeatPdu == null) + { + _heartbeatPdu = ConstructControlMessagePdu(CommandType.Heartbeat); + } + return _heartbeatPdu; } /// From ad873970074d39f8794041cf7d4761325d4ad2af Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Fri, 26 Jan 2018 11:53:06 -0600 Subject: [PATCH 2/3] made HeartbeatPdu immutable --- .../Akka.Remote/Transport/AkkaPduCodec.cs | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/core/Akka.Remote/Transport/AkkaPduCodec.cs b/src/core/Akka.Remote/Transport/AkkaPduCodec.cs index c1432841a1a..36f18953bda 100644 --- a/src/core/Akka.Remote/Transport/AkkaPduCodec.cs +++ b/src/core/Akka.Remote/Transport/AkkaPduCodec.cs @@ -381,24 +381,20 @@ public override ByteString ConstructDisassociate(DisassociateInfo reason) } } - private static ByteString _heartbeatPdu; + /* + * 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); /// - /// TBD + /// Creates a new Heartbeat message instance. /// - /// TBD + /// The Heartbeat message. public override ByteString ConstructHeartbeat() { - /* - * 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. - */ - if (_heartbeatPdu == null) - { - _heartbeatPdu = ConstructControlMessagePdu(CommandType.Heartbeat); - } - return _heartbeatPdu; + return HeartbeatPdu; } /// @@ -549,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) From 46f051f3ab378d0c44cbb584c9dfad7eadcdd8a6 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Fri, 26 Jan 2018 11:53:53 -0600 Subject: [PATCH 3/3] made all internal formatting methods static --- src/core/Akka.Remote/Transport/AkkaPduCodec.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/Akka.Remote/Transport/AkkaPduCodec.cs b/src/core/Akka.Remote/Transport/AkkaPduCodec.cs index 36f18953bda..14cf1b11a9f 100644 --- a/src/core/Akka.Remote/Transport/AkkaPduCodec.cs +++ b/src/core/Akka.Remote/Transport/AkkaPduCodec.cs @@ -556,12 +556,12 @@ private static ByteString ConstructControlMessagePdu(CommandType code, AkkaHands 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() { @@ -571,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");