]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Avoid triggering writer cond where possible
authorMatt Joiner <anacrolix@gmail.com>
Fri, 1 Sep 2017 02:09:41 +0000 (12:09 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 1 Sep 2017 02:09:41 +0000 (12:09 +1000)
client.go
connection.go

index d3ee0703c3e725e30051ed3a71847b00de5eb87d..41fa89e289561008198d600d9530d444b90aeb3f 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1035,14 +1035,6 @@ func (cl *Client) sendInitialMessages(conn *connection, torrent *Torrent) {
        }
 }
 
-func (cl *Client) connDeleteRequest(t *Torrent, cn *connection, r request) bool {
-       if !cn.RequestPending(r) {
-               return false
-       }
-       delete(cn.requests, r)
-       return true
-}
-
 // Process incoming ut_metadata message.
 func (cl *Client) gotMetadataExtensionMsg(payload []byte, t *Torrent, c *connection) error {
        var d map[string]int
index da1d97b7aaebc2a9860ec8d0e50ae3a7f05f2bd7..bf418416df87adfb1151680af8fafcae50a09e0e 100644 (file)
@@ -239,11 +239,6 @@ func (cn *connection) Post(msg pp.Message) {
        cn.writerCond.Broadcast()
 }
 
-func (cn *connection) RequestPending(r request) bool {
-       _, ok := cn.requests[r]
-       return ok
-}
-
 func (cn *connection) requestMetadataPiece(index int) {
        eID := cn.PeerExtensionIDs["ut_metadata"]
        if eID == 0 {
@@ -528,8 +523,9 @@ func undirtiedChunks(piece int, t *Torrent, f func(chunkSpec) bool) bool {
 }
 
 func (cn *connection) stopRequestingPiece(piece int) {
-       cn.pieceRequestOrder.Remove(piece)
-       cn.writerCond.Broadcast()
+       if cn.pieceRequestOrder.Remove(piece) {
+               cn.writerCond.Broadcast()
+       }
 }
 
 // This is distinct from Torrent piece priority, which is the user's
@@ -556,8 +552,9 @@ func (cn *connection) updatePiecePriority(piece int) {
                panic(tpp)
        }
        prio += piece / 3
-       cn.pieceRequestOrder.Set(piece, prio)
-       cn.updateRequests()
+       if cn.pieceRequestOrder.Set(piece, prio) {
+               cn.updateRequests()
+       }
 }
 
 func (cn *connection) getPieceInclination() []int {
@@ -752,8 +749,7 @@ func (c *connection) mainReadLoop() error {
                        // We can then reset our interest.
                        c.updateRequests()
                case pp.Reject:
-                       cl.connDeleteRequest(t, c, newRequest(msg.Index, msg.Begin, msg.Length))
-                       c.updateRequests()
+                       c.deleteRequest(newRequest(msg.Index, msg.Begin, msg.Length))
                case pp.Unchoke:
                        c.PeerChoked = false
                        c.writerCond.Broadcast()
@@ -954,9 +950,7 @@ func (c *connection) receiveChunk(msg *pp.Message) {
        req := newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
 
        // Request has been satisfied.
-       if cl.connDeleteRequest(t, c, req) {
-               defer c.updateRequests()
-       } else {
+       if !c.deleteRequest(req) {
                unexpectedChunksReceived.Add(1)
        }
 
@@ -1092,3 +1086,12 @@ func (c *connection) peerHasWantedPieces() bool {
 func (c *connection) numLocalRequests() int {
        return len(c.requests)
 }
+
+func (c *connection) deleteRequest(r request) bool {
+       if _, ok := c.requests[r]; !ok {
+               return false
+       }
+       delete(c.requests, r)
+       c.writerCond.Broadcast()
+       return true
+}