Performance optimizations to calculating bytes left in Torrent
Was generating significant overhead in cmd/torrent
This commit is contained in:
parent
018afed5a7
commit
17ea4f7fdd
@ -126,9 +126,6 @@ func (info *Info) TotalLength() (ret int64) {
|
||||
}
|
||||
|
||||
func (info *Info) NumPieces() int {
|
||||
if len(info.Pieces)%20 != 0 {
|
||||
panic(len(info.Pieces))
|
||||
}
|
||||
return len(info.Pieces) / 20
|
||||
}
|
||||
|
||||
|
10
piece.go
10
piece.go
@ -128,11 +128,11 @@ func (p *Piece) chunkIndexSpec(chunk int) chunkSpec {
|
||||
}
|
||||
|
||||
func (p *Piece) numDirtyBytes() (ret pp.Integer) {
|
||||
defer func() {
|
||||
if ret > p.length() {
|
||||
panic("too many dirty bytes")
|
||||
}
|
||||
}()
|
||||
// defer func() {
|
||||
// if ret > p.length() {
|
||||
// panic("too many dirty bytes")
|
||||
// }
|
||||
// }()
|
||||
numRegularDirtyChunks := p.numDirtyChunks()
|
||||
if p.chunkIndexDirty(p.numChunks() - 1) {
|
||||
numRegularDirtyChunks--
|
||||
|
23
torrent.go
23
torrent.go
@ -558,9 +558,11 @@ func (t *Torrent) bytesMissingLocked() int64 {
|
||||
}
|
||||
|
||||
func (t *Torrent) bytesLeft() (left int64) {
|
||||
for i := 0; i < t.numPieces(); i++ {
|
||||
left += int64(t.pieces[i].bytesLeft())
|
||||
}
|
||||
bitmap.Flip(t.completedPieces, 0, t.numPieces()).IterTyped(func(piece int) bool {
|
||||
p := t.pieces[piece]
|
||||
left += int64(p.length() - p.numDirtyBytes())
|
||||
return true
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
@ -658,17 +660,14 @@ type Peer struct {
|
||||
SupportsEncryption bool
|
||||
}
|
||||
|
||||
func (t *Torrent) pieceLength(piece int) (len_ pp.Integer) {
|
||||
if piece < 0 || piece >= t.info.NumPieces() {
|
||||
return
|
||||
}
|
||||
func (t *Torrent) pieceLength(piece int) pp.Integer {
|
||||
if piece == t.numPieces()-1 {
|
||||
len_ = pp.Integer(t.length % t.info.PieceLength)
|
||||
ret := pp.Integer(t.length % t.info.PieceLength)
|
||||
if ret != 0 {
|
||||
return ret
|
||||
}
|
||||
}
|
||||
if len_ == 0 {
|
||||
len_ = pp.Integer(t.info.PieceLength)
|
||||
}
|
||||
return
|
||||
return pp.Integer(t.info.PieceLength)
|
||||
}
|
||||
|
||||
func (t *Torrent) hashPiece(piece int) (ret metainfo.Hash) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user