From 14e31babbb94644fdaebddf7a4af29911be314cd Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 2 Jul 2023 15:23:32 -0500 Subject: [PATCH] perf: faster Cache::write_contiguous() (#5715) --- libtransmission/cache.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libtransmission/cache.cc b/libtransmission/cache.cc index a4feb0428..2026c9a05 100644 --- a/libtransmission/cache.cc +++ b/libtransmission/cache.cc @@ -72,20 +72,21 @@ int Cache::write_contiguous(CIter const begin, CIter const end) const if (end - begin > 1) { - // Yes, there are. + // copy blocks into contiguous memory auto const buflen = std::accumulate( begin, end, size_t{}, [](size_t sum, auto const& block) { return sum + std::size(*block.buf); }); - buf.reserve(buflen); + buf.resize(buflen); + auto* walk = std::data(buf); for (auto iter = begin; iter != end; ++iter) { TR_ASSERT(begin->key.first == iter->key.first); TR_ASSERT(begin->key.second + std::distance(begin, iter) == iter->key.second); - buf.insert(std::end(buf), std::begin(*iter->buf), std::end(*iter->buf)); + walk = std::copy_n(std::data(*iter->buf), std::size(*iter->buf), walk); } - TR_ASSERT(std::size(buf) == buflen); + TR_ASSERT(std::data(buf) + std::size(buf) == walk); towrite = &buf; }