From da3d83eaed48de7391bef625685fc0a5f2bb2b80 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 20 Aug 2022 10:40:18 -0500 Subject: [PATCH] refactor: make blockinfo methods constexpr (#3682) this can be constexpr and is an outlier in CPU profiling. --- libtransmission/block-info.cc | 44 ------------------ libtransmission/block-info.h | 84 ++++++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 71 deletions(-) diff --git a/libtransmission/block-info.cc b/libtransmission/block-info.cc index 40e4e81c2..7682d188f 100644 --- a/libtransmission/block-info.cc +++ b/libtransmission/block-info.cc @@ -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(loc.byte - (uint64_t{ loc.block } * BlockSize)); - loc.piece_offset = static_cast(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); -} diff --git a/libtransmission/block-info.h b/libtransmission/block-info.h index 4bde8175b..b71274f2b 100644 --- a/libtransmission/block-info.h +++ b/libtransmission/block-info.h @@ -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(loc.byte - (uint64_t{ loc.block } * BlockSize)); + loc.piece_offset = static_cast(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(piece) * pieceSize() + pieceSize(piece) - 1); } - [[nodiscard]] bool constexpr isInitialized() const noexcept + [[nodiscard]] constexpr bool isInitialized() const noexcept { return piece_size_ != 0; }