feat: new default piece size calculation for transmission-create (#5615)

This commit is contained in:
tearfur 2023-06-23 00:50:57 +08:00 committed by GitHub
parent 3b03494580
commit b562b2de4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 38 deletions

View File

@ -5,6 +5,7 @@
#include <algorithm>
#include <cerrno> // for ENOENT
#include <cmath>
#include <optional>
#include <set>
#include <string>
@ -376,3 +377,18 @@ std::string tr_metainfo_builder::benc(tr_error** error) const
tr_variantClear(&top);
return ret;
}
uint32_t tr_metainfo_builder::default_piece_size(uint64_t total_size) noexcept
{
TR_ASSERT(total_size != 0);
// Ideally, we want approximately 2^10 = 1024 pieces, give or take a few hundred pieces.
// So we subtract 10 from the log2 of total size.
// The ideal number of pieces is up for debate.
auto exp = std::log2(total_size) - 10;
// We want a piece size between 16KiB (2^14 bytes) and 16MiB (2^24 bytes) for maximum compatibility
exp = std::clamp(exp, 14., 24.);
return uint32_t{ 1U } << std::lround(exp);
}

View File

@ -180,44 +180,7 @@ public:
///
[[nodiscard]] constexpr static uint32_t default_piece_size(uint64_t total_size) noexcept
{
uint32_t const KiB = 1024;
uint32_t const MiB = 1048576;
uint32_t const GiB = 1073741824;
if (total_size >= 2 * GiB)
{
return 2 * MiB;
}
if (total_size >= 1 * GiB)
{
return 1 * MiB;
}
if (total_size >= 512 * MiB)
{
return 512 * KiB;
}
if (total_size >= 350 * MiB)
{
return 256 * KiB;
}
if (total_size >= 150 * MiB)
{
return 128 * KiB;
}
if (total_size >= 50 * MiB)
{
return 64 * KiB;
}
return 32 * KiB; /* less than 50 meg */
}
[[nodiscard]] static uint32_t default_piece_size(uint64_t total_size) noexcept;
[[nodiscard]] constexpr static bool is_legal_piece_size(uint32_t x)
{