From 7fe199992c7efabba3ce947902e41382bff33bf1 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 27 Feb 2020 18:33:44 +1100 Subject: [PATCH] Use stable bit set for pending pieces PriorityBitmap Fixes #382. --- client.go | 6 ++++-- cmd/torrent/main.go | 9 ++++++--- go.mod | 3 ++- go.sum | 6 ++++-- torrent_pending_pieces.go | 40 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 torrent_pending_pieces.go diff --git a/client.go b/client.go index 0e83374e..42a280d5 100644 --- a/client.go +++ b/client.go @@ -22,8 +22,6 @@ import ( "github.com/anacrolix/missinggo/pproffd" "github.com/anacrolix/missinggo/pubsub" "github.com/anacrolix/missinggo/slices" - "github.com/anacrolix/missinggo/v2" - "github.com/anacrolix/missinggo/v2/conntrack" "github.com/anacrolix/sync" "github.com/davecgh/go-spew/spew" "github.com/dustin/go-humanize" @@ -31,6 +29,9 @@ import ( "golang.org/x/time/rate" "golang.org/x/xerrors" + "github.com/anacrolix/missinggo/v2" + "github.com/anacrolix/missinggo/v2/conntrack" + "github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/iplist" "github.com/anacrolix/torrent/metainfo" @@ -1041,6 +1042,7 @@ func (cl *Client) newTorrent(ih metainfo.Hash, specStorage storage.ClientImpl) ( L: cl.locker(), }, } + t._pendingPieces.NewSet = priorityBitmapStableNewSet t.requestStrategy = cl.config.DefaultRequestStrategy(t.requestStrategyCallbacks(), &cl._mu) t.logger = cl.logger.WithValues(t).WithText(func(m log.Msg) string { return fmt.Sprintf("%v: %s", t, m.Text()) diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 28da282d..12705405 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -28,7 +28,7 @@ import ( "github.com/anacrolix/torrent/storage" ) -func torrentBar(t *torrent.Torrent) { +func torrentBar(t *torrent.Torrent, pieceStates bool) { go func() { if t.Info() == nil { fmt.Printf("getting info for %q\n", t.Name()) @@ -54,7 +54,9 @@ func torrentBar(t *torrent.Torrent) { t.NumPieces(), partialPieces, ) - //fmt.Println(psrs) + if pieceStates { + fmt.Println(psrs) + } time.Sleep(time.Second) } }() @@ -104,7 +106,7 @@ func addTorrents(client *torrent.Client) error { return xerrors.Errorf("adding torrent for %q: %w", arg, err) } if flags.Progress { - torrentBar(t) + torrentBar(t, flags.PieceStates) } t.AddPeers(func() (ret []torrent.Peer) { for _, ta := range flags.TestPeer { @@ -134,6 +136,7 @@ var flags = struct { Stats *bool PublicIP net.IP Progress bool + PieceStates bool Quiet bool `help:"discard client logging"` Dht bool tagflag.StartPos diff --git a/go.mod b/go.mod index b7a9a2d7..00578c10 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/anacrolix/log v0.6.0 github.com/anacrolix/missinggo v1.2.1 github.com/anacrolix/missinggo/perf v1.0.0 - github.com/anacrolix/missinggo/v2 v2.4.0 + github.com/anacrolix/missinggo/v2 v2.4.1-0.20200227072623-f02f6484f997 github.com/anacrolix/multiless v0.0.0-20191223025854-070b7994e841 github.com/anacrolix/sync v0.2.0 github.com/anacrolix/tagflag v1.0.1 @@ -19,6 +19,7 @@ require ( github.com/davecgh/go-spew v1.1.1 github.com/dustin/go-humanize v1.0.0 github.com/edsrzf/mmap-go v1.0.0 + github.com/elliotchance/orderedmap v1.2.0 github.com/etcd-io/bbolt v1.3.3 github.com/fsnotify/fsnotify v1.4.7 github.com/google/btree v1.0.0 diff --git a/go.sum b/go.sum index ec3240c4..8e3c25d5 100644 --- a/go.sum +++ b/go.sum @@ -69,8 +69,8 @@ github.com/anacrolix/missinggo/v2 v2.2.1-0.20191103010835-12360f38ced0 h1:CKpmXo github.com/anacrolix/missinggo/v2 v2.2.1-0.20191103010835-12360f38ced0/go.mod h1:ZzG3/cc3t+5zcYWAgYrJW0MBsSwNwOkTlNquBbP51Bc= github.com/anacrolix/missinggo/v2 v2.3.0/go.mod h1:ZzG3/cc3t+5zcYWAgYrJW0MBsSwNwOkTlNquBbP51Bc= github.com/anacrolix/missinggo/v2 v2.3.1/go.mod h1:3XNH0OEmyMUZuvXmYdl+FDfXd0vvSZhvOLy8CFx8tLg= -github.com/anacrolix/missinggo/v2 v2.4.0 h1:qWSaH/JZyjKBgJ1hrCmNaGUzeEvKrGCbzv/8HjATBFY= -github.com/anacrolix/missinggo/v2 v2.4.0/go.mod h1:sjPqWXxdr3jWcMO/tXhhshXAaiTkGIgJpN93clGzGr8= +github.com/anacrolix/missinggo/v2 v2.4.1-0.20200227072623-f02f6484f997 h1:ufGZtSzDyevqcHyFtAWm2HMjmezh/mCemzC1VFEjsu0= +github.com/anacrolix/missinggo/v2 v2.4.1-0.20200227072623-f02f6484f997/go.mod h1:KY+ij+mWvwGuqSuecLjjPv5LFw5ICUc1UvRems3VAZE= github.com/anacrolix/mmsg v0.0.0-20180515031531-a4a3ba1fc8bb h1:2Or5ccMoY4Kfao+WdL2w6tpY6ZEe+2VTVbIPd7A/Ajk= github.com/anacrolix/mmsg v0.0.0-20180515031531-a4a3ba1fc8bb/go.mod h1:x2/ErsYUmT77kezS63+wzZp8E3byYB0gzirM/WMBLfw= github.com/anacrolix/mmsg v1.0.0 h1:btC7YLjOn29aTUAExJiVUhQOuf/8rhm+/nWCMAnL3Hg= @@ -135,6 +135,8 @@ github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712 h1:aaQcKT9WumO6JEJc github.com/edsrzf/mmap-go v0.0.0-20170320065105-0bce6a688712/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= github.com/edsrzf/mmap-go v1.0.0 h1:CEBF7HpRnUCSJgGUb5h1Gm7e3VkmVDrR8lvWVLtrOFw= github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= +github.com/elliotchance/orderedmap v1.2.0 h1:Z2kiPPgjjlS8NN+1EFzE4ZO/HpW02gl9ZH3MHL+bNkg= +github.com/elliotchance/orderedmap v1.2.0/go.mod h1:8hdSl6jmveQw8ScByd3AaNHNk51RhbTazdqtTty+NFw= github.com/etcd-io/bbolt v1.3.3 h1:gSJmxrs37LgTqR/oyJBWok6k6SvXEUerFTbltIhXkBM= github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= diff --git a/torrent_pending_pieces.go b/torrent_pending_pieces.go new file mode 100644 index 00000000..bfcf7bfb --- /dev/null +++ b/torrent_pending_pieces.go @@ -0,0 +1,40 @@ +package torrent + +import ( + "github.com/elliotchance/orderedmap" + + "github.com/anacrolix/missinggo/v2/prioritybitmap" +) + +type stableSet struct { + om *orderedmap.OrderedMap +} + +func (s stableSet) Has(bit int) bool { + _, ok := s.om.Get(bit) + return ok +} + +func (s stableSet) Delete(bit int) { + s.om.Delete(bit) +} + +func (s stableSet) Len() int { + return s.om.Len() +} + +func (s stableSet) Set(bit int) { + s.om.Set(bit, struct{}{}) +} + +func (s stableSet) Range(f func(int) bool) { + for e := s.om.Front(); e != nil; e = e.Next() { + if !f(e.Key.(int)) { + break + } + } +} + +func priorityBitmapStableNewSet() prioritybitmap.Set { + return stableSet{om: orderedmap.NewOrderedMap()} +} -- 2.48.1