From 0420d48e5634d0ceff81dc88910d6d9d1adb571d Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 15 Jan 2020 13:16:09 +0300 Subject: [PATCH] network: micro-optimize relayInventory Do less allocations, we're sending the same message with the same payload. --- pkg/network/server.go | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/pkg/network/server.go b/pkg/network/server.go index b0b3eef89..5ab280f19 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -676,12 +676,14 @@ func (s *Server) requestTx(hashes ...util.Uint256) { } func (s *Server) relayInventory(t payload.InventoryType, hashes ...util.Uint256) { + payload := payload.NewInventory(t, hashes) + msg := NewMessage(s.Net, CMDInv, payload) + for peer := range s.Peers() { - if !peer.Handshaked() { + if !peer.Handshaked() || !peer.Version().Relay { continue } - payload := payload.NewInventory(t, hashes) - s.RelayDirectly(peer, payload) + peer.WriteMsg(msg) } } @@ -713,14 +715,3 @@ func (s *Server) RelayTxn(t *transaction.Transaction) RelayReason { return RelaySucceed } - -// RelayDirectly relays directly the inventory to the remote peers. -// Reference: the method OnRelayDirectly in C#: https://github.com/neo-project/neo/blob/master/neo/Network/P2P/LocalNode.cs#L166 -func (s *Server) RelayDirectly(p Peer, inv *payload.Inventory) { - if !p.Version().Relay { - return - } - - p.WriteMsg(NewMessage(s.Net, CMDInv, inv)) - -}