From 0e6f8d4cc19ef5bb5d9c47e357a3ffb862b8b067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 8 Apr 2019 15:36:25 +0200 Subject: [PATCH] bootstrap: cleanup randomSubsetOfPeers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/bootstrap/bootstrap.go | 16 +++++++--------- thirdparty/math2/math2.go | 9 --------- 2 files changed, 7 insertions(+), 18 deletions(-) delete mode 100644 thirdparty/math2/math2.go diff --git a/core/bootstrap/bootstrap.go b/core/bootstrap/bootstrap.go index e6b4f826d..d7c107690 100644 --- a/core/bootstrap/bootstrap.go +++ b/core/bootstrap/bootstrap.go @@ -20,8 +20,6 @@ import ( "github.com/libp2p/go-libp2p-peer" "github.com/libp2p/go-libp2p-peerstore" "github.com/libp2p/go-libp2p-routing" - - "github.com/ipfs/go-ipfs/thirdparty/math2" ) var log = logging.Logger("bootstrap") @@ -208,13 +206,13 @@ func bootstrapConnect(ctx context.Context, ph host.Host, peers []peerstore.PeerI } func randomSubsetOfPeers(in []peerstore.PeerInfo, max int) []peerstore.PeerInfo { - n := math2.IntMin(max, len(in)) - var out []peerstore.PeerInfo - for _, val := range rand.Perm(len(in)) { - out = append(out, in[val]) - if len(out) >= n { - break - } + if max > len(in) { + max = len(in) + } + + out := make([]peerstore.PeerInfo, max) + for i, val := range rand.Perm(len(in))[:max] { + out[i] = in[val] } return out } diff --git a/thirdparty/math2/math2.go b/thirdparty/math2/math2.go deleted file mode 100644 index e8a75b5f7..000000000 --- a/thirdparty/math2/math2.go +++ /dev/null @@ -1,9 +0,0 @@ -package math2 - -// IntMin returns the smaller of x or y. -func IntMin(x, y int) int { - if x < y { - return x - } - return y -}