refactor: make blockinfo methods constexpr (#3682)

this can be constexpr and is an outlier in CPU profiling.
This commit is contained in:
Charles Kerr 2022-08-20 10:40:18 -05:00 committed by GitHub
parent 7dd55b64e1
commit da3d83eaed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 71 deletions

View File

@ -28,47 +28,3 @@ void tr_block_info::initSizes(uint64_t total_size_in, uint32_t piece_size_in) no
remainder = total_size_ % 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);
}

View File

@ -64,27 +64,6 @@ public:
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
{
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`.
[[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`
[[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]] Location byteLoc(uint64_t byte_idx) const noexcept;
[[nodiscard]] constexpr 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]] 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:
// 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);
}
[[nodiscard]] bool constexpr isInitialized() const noexcept
[[nodiscard]] constexpr bool isInitialized() const noexcept
{
return piece_size_ != 0;
}