minimumPieceLength from 16KB to 64KB, add maximumPieceLength 2MB-, piecelength priority is over piece numbr priority
This commit is contained in:
parent
ff11917fa8
commit
16debff467
|
@ -30,9 +30,14 @@
|
||||||
|
|
||||||
package metainfo
|
package metainfo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
// For more context on why these numbers, see http://wiki.vuze.com/w/Torrent_Piece_Size
|
// For more context on why these numbers, see http://wiki.vuze.com/w/Torrent_Piece_Size
|
||||||
const (
|
const (
|
||||||
minimumPieceLength = 16 * 1024
|
minimumPieceLength = 64 * 1024
|
||||||
|
maximumPieceLength = 2 * 1024 * 1024
|
||||||
targetPieceCountLog2 = 10
|
targetPieceCountLog2 = 10
|
||||||
targetPieceCountMin = 1 << targetPieceCountLog2
|
targetPieceCountMin = 1 << targetPieceCountLog2
|
||||||
)
|
)
|
||||||
|
@ -41,16 +46,19 @@ const (
|
||||||
const targetPieceCountMax = targetPieceCountMin << 1
|
const targetPieceCountMax = targetPieceCountMin << 1
|
||||||
|
|
||||||
// Choose a good piecelength.
|
// Choose a good piecelength.
|
||||||
// piecelength >= 16KB
|
// piecelength >= 64KB and piecelength <= 2MB
|
||||||
// piecelength = 16KB*X
|
// piecelength = 64KB*X
|
||||||
// totalLength / piecelength = piecenumber < 2048
|
// totalLength / piecelength = piecenumber <= 2048
|
||||||
func ChoosePieceLength(totalLength int64) (pieceLength int64) {
|
func ChoosePieceLength(totalLength int64) (pieceLength int64) {
|
||||||
// Must be a power of 2.
|
// Must be a power of 2.
|
||||||
// Must be a multiple of 16KB
|
// Must be a multiple of 64KB
|
||||||
// Prefer to provide around 1024..2048 pieces.
|
// Prefer to provide around 1024..2048 pieces.
|
||||||
pieceLength = minimumPieceLength
|
pieceLength = minimumPieceLength
|
||||||
pieces := totalLength / pieceLength
|
pieces := int64(math.Ceil(float64(totalLength) / float64(pieceLength)))
|
||||||
for pieces >= targetPieceCountMax {
|
for pieces > targetPieceCountMax {
|
||||||
|
if (pieceLength << 1) > maximumPieceLength {
|
||||||
|
break
|
||||||
|
}
|
||||||
pieceLength <<= 1
|
pieceLength <<= 1
|
||||||
pieces >>= 1
|
pieces >>= 1
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue