From d51db20405cce39debba9689a4e31865c633b58e Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 6 Aug 2021 00:03:46 +0300 Subject: [PATCH] network: randomize peer iteration order MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While iterating over map in getPeers() is non-deterministic it's not really random enough for our purposes (usually maps have 2-3 paths through them), we need to fill our peers queues more uniformly. Believe it or not, but it does affect performance metrics, baseline (four nodes, 10 workers): RPS ≈ 7791.675 7996.559 7834.504 7746.705 7891.614 ≈ 7852 ± 1.10% TPS ≈ 7241.497 7711.765 7520.211 7425.890 7334.443 ≈ 7447 ± 2.17% CPU % 29.853 39.936 39.945 36.371 39.999 ≈ 37.2 ± 10.57% Mem MB 2749.635 2791.609 2828.610 2910.431 2863.344 ≈ 2829 ± 1.97% Patched: RPS 8180.760 8137.822 7858.358 7820.011 8051.076 ≈ 8010 ± 2.04% ↑ 2.01% TPS 7819.831 7521.172 7519.023 7242.965 7426.000 ≈ 7506 ± 2.78% ↑ 0.79% CPU % 41.983 38.775 40.606 39.375 35.537 ≈ 39.3 ± 6.15% ↑ 5.65% Mem MB 2947.189 2743.658 2896.688 2813.276 2863.108 ≈ 2853 ± 2.74% ↑ 0.85% --- pkg/network/server.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/network/server.go b/pkg/network/server.go index b4ea1c073..0eb6e2a77 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -1146,6 +1146,9 @@ func (s *Server) iteratePeersWithSendMsg(msg *Message, send func(Peer, bool, []b if len(peers) == 0 { return } + mrand.Shuffle(len(peers), func(i, j int) { + peers[i], peers[j] = peers[j], peers[i] + }) pkt, err := msg.Bytes() if err != nil { return