mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +00:00
refactor: make blockinfo methods constexpr (#3682)
this can be constexpr and is an outlier in CPU profiling.
This commit is contained in:
@@ -28,47 +28,3 @@ void tr_block_info::initSizes(uint64_t total_size_in, uint32_t piece_size_in) no
|
|||||||
remainder = total_size_ % BlockSize;
|
remainder = total_size_ % BlockSize;
|
||||||
final_block_size_ = remainder != 0U ? remainder : BlockSize;
|
final_block_size_ = remainder != 0U ? remainder : BlockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
tr_block_info::Location tr_block_info::byteLoc(uint64_t byte_idx) const noexcept
|
|
||||||
{
|
|
||||||
TR_ASSERT(byte_idx <= totalSize());
|
|
||||||
|
|
||||||
if (!isInitialized())
|
|
||||||
{
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
auto loc = Location{};
|
|
||||||
|
|
||||||
loc.byte = byte_idx;
|
|
||||||
|
|
||||||
if (byte_idx == totalSize()) // handle 0-byte files at the end of a torrent
|
|
||||||
{
|
|
||||||
loc.block = blockCount() - 1;
|
|
||||||
loc.piece = pieceCount() - 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
loc.block = byte_idx / BlockSize;
|
|
||||||
loc.piece = byte_idx / pieceSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
loc.block_offset = static_cast<uint32_t>(loc.byte - (uint64_t{ loc.block } * BlockSize));
|
|
||||||
loc.piece_offset = static_cast<uint32_t>(loc.byte - (uint64_t{ loc.piece } * pieceSize()));
|
|
||||||
|
|
||||||
return loc;
|
|
||||||
}
|
|
||||||
|
|
||||||
tr_block_info::Location tr_block_info::blockLoc(tr_block_index_t block) const noexcept
|
|
||||||
{
|
|
||||||
TR_ASSERT(block < blockCount());
|
|
||||||
|
|
||||||
return byteLoc(uint64_t{ block } * BlockSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
tr_block_info::Location tr_block_info::pieceLoc(tr_piece_index_t piece, uint32_t offset, uint32_t length) const noexcept
|
|
||||||
{
|
|
||||||
TR_ASSERT(piece < pieceCount());
|
|
||||||
|
|
||||||
return byteLoc(uint64_t{ piece } * pieceSize() + offset + length);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -64,27 +64,6 @@ public:
|
|||||||
return total_size_;
|
return total_size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] tr_block_span_t blockSpanForPiece(tr_piece_index_t piece) const noexcept
|
|
||||||
{
|
|
||||||
if (!isInitialized())
|
|
||||||
{
|
|
||||||
return { 0U, 0U };
|
|
||||||
}
|
|
||||||
|
|
||||||
return { pieceLoc(piece).block, pieceLastLoc(piece).block + 1 };
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] tr_byte_span_t byteSpanForPiece(tr_piece_index_t piece) const noexcept
|
|
||||||
{
|
|
||||||
if (!isInitialized())
|
|
||||||
{
|
|
||||||
return { 0U, 0U };
|
|
||||||
}
|
|
||||||
|
|
||||||
auto const offset = pieceLoc(piece).byte;
|
|
||||||
return { offset, offset + pieceSize(piece) };
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Location
|
struct Location
|
||||||
{
|
{
|
||||||
uint64_t byte = 0;
|
uint64_t byte = 0;
|
||||||
@@ -106,23 +85,74 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Location of the torrent's nth byte
|
||||||
|
[[nodiscard]] constexpr auto byteLoc(uint64_t byte_idx) const noexcept
|
||||||
|
{
|
||||||
|
auto loc = Location{};
|
||||||
|
|
||||||
|
if (isInitialized())
|
||||||
|
{
|
||||||
|
loc.byte = byte_idx;
|
||||||
|
|
||||||
|
if (byte_idx == totalSize()) // handle 0-byte files at the end of a torrent
|
||||||
|
{
|
||||||
|
loc.block = blockCount() - 1;
|
||||||
|
loc.piece = pieceCount() - 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
loc.block = byte_idx / BlockSize;
|
||||||
|
loc.piece = byte_idx / pieceSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
loc.block_offset = static_cast<uint32_t>(loc.byte - (uint64_t{ loc.block } * BlockSize));
|
||||||
|
loc.piece_offset = static_cast<uint32_t>(loc.byte - (uint64_t{ loc.piece } * pieceSize()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return loc;
|
||||||
|
}
|
||||||
|
|
||||||
// Location of the first byte in `block`.
|
// Location of the first byte in `block`.
|
||||||
[[nodiscard]] Location blockLoc(tr_block_index_t block) const noexcept;
|
[[nodiscard]] constexpr auto blockLoc(tr_block_index_t block) const noexcept
|
||||||
|
{
|
||||||
|
return byteLoc(uint64_t{ block } * BlockSize);
|
||||||
|
}
|
||||||
|
|
||||||
// Location of the first byte (+ optional offset and length) in `piece`
|
// Location of the first byte (+ optional offset and length) in `piece`
|
||||||
[[nodiscard]] Location pieceLoc(tr_piece_index_t piece, uint32_t offset = 0, uint32_t length = 0) const noexcept;
|
[[nodiscard]] constexpr auto pieceLoc(tr_piece_index_t piece, uint32_t offset = 0, uint32_t length = 0) const noexcept
|
||||||
|
{
|
||||||
|
return byteLoc(uint64_t{ piece } * pieceSize() + offset + length);
|
||||||
|
}
|
||||||
|
|
||||||
// Location of the torrent's nth byte
|
[[nodiscard]] constexpr tr_block_span_t blockSpanForPiece(tr_piece_index_t piece) const noexcept
|
||||||
[[nodiscard]] Location byteLoc(uint64_t byte_idx) const noexcept;
|
{
|
||||||
|
if (!isInitialized())
|
||||||
|
{
|
||||||
|
return { 0U, 0U };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { pieceLoc(piece).block, pieceLastLoc(piece).block + 1 };
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr tr_byte_span_t byteSpanForPiece(tr_piece_index_t piece) const noexcept
|
||||||
|
{
|
||||||
|
if (!isInitialized())
|
||||||
|
{
|
||||||
|
return { 0U, 0U };
|
||||||
|
}
|
||||||
|
|
||||||
|
auto const offset = pieceLoc(piece).byte;
|
||||||
|
return { offset, offset + pieceSize(piece) };
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Location of the last byte in `piece`.
|
// Location of the last byte in `piece`.
|
||||||
[[nodiscard]] Location pieceLastLoc(tr_piece_index_t piece) const
|
[[nodiscard]] constexpr Location pieceLastLoc(tr_piece_index_t piece) const
|
||||||
{
|
{
|
||||||
return byteLoc(static_cast<uint64_t>(piece) * pieceSize() + pieceSize(piece) - 1);
|
return byteLoc(static_cast<uint64_t>(piece) * pieceSize() + pieceSize(piece) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] bool constexpr isInitialized() const noexcept
|
[[nodiscard]] constexpr bool isInitialized() const noexcept
|
||||||
{
|
{
|
||||||
return piece_size_ != 0;
|
return piece_size_ != 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user