]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Ditch the use of generic slices.HeapInterface for finding the worst "bad" connection
authorMatt Joiner <anacrolix@gmail.com>
Mon, 31 Oct 2016 05:24:48 +0000 (16:24 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 31 Oct 2016 05:24:48 +0000 (16:24 +1100)
It's become a bottleneck. Implement it directly using heap.Interface.

torrent.go
worst_conns.go

index 280850bb1cf508d857296ae50eef2221c18660e6..c8514654c4751f7f00a1aad1fa467f6bb3f416e6 100644 (file)
@@ -779,9 +779,9 @@ func (t *Torrent) extentPieces(off, _len int64) (pieces []int) {
 // pieces, or has been in worser half of the established connections for more
 // than a minute.
 func (t *Torrent) worstBadConn() *connection {
-       wcs := slices.HeapInterface(t.worstUnclosedConns(), worseConn)
+       wcs := worseConnSlice{t.worstUnclosedConns()}
        for wcs.Len() != 0 {
-               c := heap.Pop(wcs).(*connection)
+               c := heap.Pop(&wcs).(*connection)
                if c.UnwantedChunksReceived >= 6 && c.UnwantedChunksReceived > c.UsefulChunksReceived {
                        return c
                }
index 4085e0c9a8227246d579c9e86246066812707640..dbbc3c279cb5006c8d4479966a16d24a580fac22 100644 (file)
@@ -1,5 +1,7 @@
 package torrent
 
+import "container/heap"
+
 func worseConn(l, r *connection) bool {
        if l.useful() != r.useful() {
                return r.useful()
@@ -9,3 +11,32 @@ func worseConn(l, r *connection) bool {
        }
        return l.completedHandshake.Before(r.completedHandshake)
 }
+
+type worseConnSlice struct {
+       conns []*connection
+}
+
+var _ heap.Interface = &worseConnSlice{}
+
+func (me worseConnSlice) Len() int {
+       return len(me.conns)
+}
+
+func (me worseConnSlice) Less(i, j int) bool {
+       return worseConn(me.conns[i], me.conns[j])
+}
+
+func (me *worseConnSlice) Pop() interface{} {
+       i := len(me.conns) - 1
+       ret := me.conns[i]
+       me.conns = me.conns[:i]
+       return ret
+}
+
+func (me *worseConnSlice) Push(x interface{}) {
+       me.conns = append(me.conns, x.(*connection))
+}
+
+func (me worseConnSlice) Swap(i, j int) {
+       me.conns[i], me.conns[j] = me.conns[j], me.conns[i]
+}