perf: make `CompareCacheBlockByKey` constexpr (#5678)

This commit is contained in:
tearfur 2023-06-28 23:23:38 +08:00 committed by Charles Kerr
parent 115a069d8d
commit 87388ee7c8
2 changed files with 21 additions and 23 deletions

View File

@ -23,7 +23,7 @@
#include "tr-assert.h" #include "tr-assert.h"
#include "utils.h" // tr_time(), tr_formatter #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); 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<std::vector<uint8_t>> writeme) int Cache::writeBlock(tr_torrent_id_t tor_id, tr_block_index_t block, std::unique_ptr<std::vector<uint8_t>> writeme)
{ {
auto const key = Key{ tor_id, block }; 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) if (iter == std::end(blocks_) || iter->key != key)
{ {
iter = blocks_.emplace(iter); 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( if (auto const [begin, end] = std::equal_range(
std::begin(blocks_), std::begin(blocks_),
std::end(blocks_), std::end(blocks_),
makeKey(torrent, loc), make_key(torrent, loc),
CompareCacheBlockByKey{}); CompareCacheBlockByKey);
begin < end) begin < end)
{ {
return begin; 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) int Cache::flushFile(tr_torrent const* torrent, tr_file_index_t file)
{ {
auto const compare = CompareCacheBlockByKey{};
auto const tor_id = torrent->id(); auto const tor_id = torrent->id();
auto const [block_begin, block_end] = tr_torGetFileBlockSpan(torrent, file); auto const [block_begin, block_end] = tr_torGetFileBlockSpan(torrent, file);
return flush_span( 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_begin), CompareCacheBlockByKey),
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_end), CompareCacheBlockByKey));
} }
int Cache::flushTorrent(tr_torrent const* torrent) int Cache::flushTorrent(tr_torrent const* torrent)
{ {
auto const compare = CompareCacheBlockByKey{};
auto const tor_id = torrent->id(); auto const tor_id = torrent->id();
return flush_span( 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, 0), CompareCacheBlockByKey),
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 + 1, 0), CompareCacheBlockByKey));
} }
int Cache::flush_biggest() int Cache::flush_biggest()

View File

@ -54,19 +54,7 @@ private:
using Blocks = std::vector<CacheBlock>; using Blocks = std::vector<CacheBlock>;
using CIter = Blocks::const_iterator; using CIter = Blocks::const_iterator;
struct CompareCacheBlockByKey [[nodiscard]] static Key make_key(tr_torrent const* torrent, tr_block_info::Location loc) noexcept;
{
[[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 std::pair<CIter, CIter> find_biggest_span(CIter begin, CIter end) noexcept; [[nodiscard]] static std::pair<CIter, CIter> find_biggest_span(CIter begin, CIter end) noexcept;
@ -98,4 +86,16 @@ private:
mutable size_t disk_write_bytes_ = 0; mutable size_t disk_write_bytes_ = 0;
mutable size_t cache_writes_ = 0; mutable size_t cache_writes_ = 0;
mutable size_t cache_write_bytes_ = 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{};
}; };