From 87388ee7c8243edeeb0f70855c13dd8492ab16da Mon Sep 17 00:00:00 2001 From: tearfur <46261767+tearfur@users.noreply.github.com> Date: Wed, 28 Jun 2023 23:23:38 +0800 Subject: [PATCH] perf: make `CompareCacheBlockByKey` constexpr (#5678) --- libtransmission/cache.cc | 18 ++++++++---------- libtransmission/cache.h | 26 +++++++++++++------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/libtransmission/cache.cc b/libtransmission/cache.cc index 3b58722b2..a4feb0428 100644 --- a/libtransmission/cache.cc +++ b/libtransmission/cache.cc @@ -23,7 +23,7 @@ #include "tr-assert.h" #include "utils.h" // tr_time(), tr_formatter -Cache::Key Cache::makeKey(tr_torrent const* torrent, tr_block_info::Location loc) noexcept +Cache::Key Cache::make_key(tr_torrent const* torrent, tr_block_info::Location loc) noexcept { return std::make_pair(torrent->id(), loc.block); } @@ -136,7 +136,7 @@ Cache::Cache(tr_torrents& torrents, int64_t max_bytes) int Cache::writeBlock(tr_torrent_id_t tor_id, tr_block_index_t block, std::unique_ptr> writeme) { auto const key = Key{ tor_id, block }; - auto iter = std::lower_bound(std::begin(blocks_), std::end(blocks_), key, CompareCacheBlockByKey{}); + auto iter = std::lower_bound(std::begin(blocks_), std::end(blocks_), key, CompareCacheBlockByKey); if (iter == std::end(blocks_) || iter->key != key) { iter = blocks_.emplace(iter); @@ -156,8 +156,8 @@ Cache::CIter Cache::getBlock(tr_torrent const* torrent, tr_block_info::Location if (auto const [begin, end] = std::equal_range( std::begin(blocks_), std::end(blocks_), - makeKey(torrent, loc), - CompareCacheBlockByKey{}); + make_key(torrent, loc), + CompareCacheBlockByKey); begin < end) { return begin; @@ -209,23 +209,21 @@ int Cache::flush_span(CIter const begin, CIter const end) int Cache::flushFile(tr_torrent const* torrent, tr_file_index_t file) { - auto const compare = CompareCacheBlockByKey{}; auto const tor_id = torrent->id(); auto const [block_begin, block_end] = tr_torGetFileBlockSpan(torrent, file); return flush_span( - std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, block_begin), compare), - std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, block_end), compare)); + std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, block_begin), CompareCacheBlockByKey), + std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, block_end), CompareCacheBlockByKey)); } int Cache::flushTorrent(tr_torrent const* torrent) { - auto const compare = CompareCacheBlockByKey{}; auto const tor_id = torrent->id(); return flush_span( - std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, 0), compare), - std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id + 1, 0), compare)); + std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, 0), CompareCacheBlockByKey), + std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id + 1, 0), CompareCacheBlockByKey)); } int Cache::flush_biggest() diff --git a/libtransmission/cache.h b/libtransmission/cache.h index 9ff0a972b..14ddd4977 100644 --- a/libtransmission/cache.h +++ b/libtransmission/cache.h @@ -54,19 +54,7 @@ private: using Blocks = std::vector; using CIter = Blocks::const_iterator; - struct CompareCacheBlockByKey - { - [[nodiscard]] constexpr bool operator()(Key const& key, CacheBlock const& block) - { - return key < block.key; - } - [[nodiscard]] constexpr bool operator()(CacheBlock const& block, Key const& key) - { - return block.key < key; - } - }; - - [[nodiscard]] static Key makeKey(tr_torrent const* torrent, tr_block_info::Location loc) noexcept; + [[nodiscard]] static Key make_key(tr_torrent const* torrent, tr_block_info::Location loc) noexcept; [[nodiscard]] static std::pair find_biggest_span(CIter begin, CIter end) noexcept; @@ -98,4 +86,16 @@ private: mutable size_t disk_write_bytes_ = 0; mutable size_t cache_writes_ = 0; mutable size_t cache_write_bytes_ = 0; + + static constexpr struct + { + [[nodiscard]] constexpr bool operator()(Key const& key, CacheBlock const& block) + { + return key < block.key; + } + [[nodiscard]] constexpr bool operator()(CacheBlock const& block, Key const& key) + { + return block.key < key; + } + } CompareCacheBlockByKey{}; };