From 16debff467d9f83ae832cf76035b2df07086a1b8 Mon Sep 17 00:00:00 2001 From: whr819987540 <819987540@qq.com> Date: Thu, 27 Apr 2023 12:04:53 +0800 Subject: [PATCH] minimumPieceLength from 16KB to 64KB, add maximumPieceLength 2MB-, piecelength priority is over piece numbr priority --- metainfo/piece-length.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/metainfo/piece-length.go b/metainfo/piece-length.go index c483e037..0372f79b 100644 --- a/metainfo/piece-length.go +++ b/metainfo/piece-length.go @@ -30,9 +30,14 @@ package metainfo +import ( + "math" +) + // For more context on why these numbers, see http://wiki.vuze.com/w/Torrent_Piece_Size const ( - minimumPieceLength = 16 * 1024 + minimumPieceLength = 64 * 1024 + maximumPieceLength = 2 * 1024 * 1024 targetPieceCountLog2 = 10 targetPieceCountMin = 1 << targetPieceCountLog2 ) @@ -41,16 +46,19 @@ const ( const targetPieceCountMax = targetPieceCountMin << 1 // Choose a good piecelength. -// piecelength >= 16KB -// piecelength = 16KB*X -// totalLength / piecelength = piecenumber < 2048 +// piecelength >= 64KB and piecelength <= 2MB +// piecelength = 64KB*X +// totalLength / piecelength = piecenumber <= 2048 func ChoosePieceLength(totalLength int64) (pieceLength int64) { // 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. pieceLength = minimumPieceLength - pieces := totalLength / pieceLength - for pieces >= targetPieceCountMax { + pieces := int64(math.Ceil(float64(totalLength) / float64(pieceLength))) + for pieces > targetPieceCountMax { + if (pieceLength << 1) > maximumPieceLength { + break + } pieceLength <<= 1 pieces >>= 1 }