Skip to content

Commit

Permalink
perf: make CompareCacheBlockByKey constexpr (#5678)
Browse files Browse the repository at this point in the history
  • Loading branch information
tearfur authored and ckerr committed Jun 28, 2023
1 parent 115a069 commit 87388ee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
18 changes: 8 additions & 10 deletions libtransmission/cache.cc
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
{
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);
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
26 changes: 13 additions & 13 deletions libtransmission/cache.h
Expand Up @@ -54,19 +54,7 @@ class Cache
using Blocks = std::vector<CacheBlock>;
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<CIter, CIter> find_biggest_span(CIter begin, CIter end) noexcept;

Expand Down Expand Up @@ -98,4 +86,16 @@ class Cache
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{};
};

0 comments on commit 87388ee

Please sign in to comment.