Skip to content

Commit

Permalink
feat: new default piece size calculation for transmission-create (#5615)
Browse files Browse the repository at this point in the history
  • Loading branch information
tearfur committed Jun 22, 2023
1 parent 3b03494 commit b562b2d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 38 deletions.
16 changes: 16 additions & 0 deletions libtransmission/makemeta.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>
#include <cerrno> // for ENOENT
#include <cmath>
#include <optional>
#include <set>
#include <string>
Expand Down Expand Up @@ -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);
}
39 changes: 1 addition & 38 deletions libtransmission/makemeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,44 +180,7 @@ class tr_metainfo_builder

///

[[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)
{
Expand Down

0 comments on commit b562b2d

Please sign in to comment.