@@ -28,6 +28,7 @@ import (
2828 "github.com/anacrolix/dht/v2/krpc"
2929 . "github.com/anacrolix/generics"
3030 g "github.com/anacrolix/generics"
31+ "github.com/anacrolix/generics/heap"
3132 "github.com/anacrolix/log"
3233 "github.com/anacrolix/missinggo/v2"
3334 "github.com/anacrolix/missinggo/v2/bitmap"
@@ -1996,19 +1997,28 @@ func (cl *Client) startPieceHashers() {
19961997 if ! cl .canStartPieceHashers () {
19971998 return
19981999 }
1999- ts := make ([]* Torrent , 0 , len (cl .torrents ))
2000+ var ts []* Torrent
2001+ // Maybe we don't actually want to preallocate all this. It might often be empty.
2002+ //ts := make([]*Torrent, 0, len(cl.torrents))
2003+
20002004 for t := range cl .torrents {
20012005 if ! t .considerStartingHashers () {
20022006 continue
20032007 }
20042008 ts = append (ts , t )
20052009 }
2006- // Sort largest torrents first, as those are preferred by webseeds, and will cause less thrashing.
2007- slices .SortFunc (ts , func (a , b * Torrent ) int {
2008- return - cmp .Compare (a .length (), b .length ())
2010+ if len (ts ) == 0 {
2011+ return
2012+ }
2013+ // Sort largest torrents first, as those are preferred by webseeds, and will cause less
2014+ // thrashing.
2015+ h := heap .InterfaceForSlice (& ts , func (a , b * Torrent ) bool {
2016+ return a .length () > b .length ()
20092017 })
2010- for _ , t := range ts {
2011- t .startPieceHashers ()
2018+ heap .Init (h )
2019+ for h .Len () > 0 {
2020+ t := heap .Pop (h )
2021+ _ = t .startPieceHashers ()
20122022 if ! cl .canStartPieceHashers () {
20132023 break
20142024 }
0 commit comments