From 198a938700efa74fbd605d9a01fc5590f21a16b2 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 11 Oct 2021 15:46:40 +1100 Subject: [PATCH] Optimize Peer.peerHasWantedPieces --- peerconn.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/peerconn.go b/peerconn.go index a387e1e6..c54be39f 100644 --- a/peerconn.go +++ b/peerconn.go @@ -1425,10 +1425,15 @@ func (cn *Peer) netGoodPiecesDirtied() int64 { } func (c *Peer) peerHasWantedPieces() bool { - // TODO: Can this be done just with AndCardinality? - missingPeerHas := c.newPeerPieces() - missingPeerHas.AndNot(&c.t._completedPieces) - return !missingPeerHas.IsEmpty() + if c.peerSentHaveAll { + return !c.t.haveAllPieces() + } + if !c.t.haveInfo() { + return c._peerPieces.GetCardinality() != 0 + } + return c._peerPieces.Intersects( + roaring.FlipInt(&c.t._completedPieces, 0, c.t.numPieces()), + ) } func (c *Peer) deleteRequest(r RequestIndex) bool { -- 2.48.1