diff --git a/libtransmission/rpcimpl.cc b/libtransmission/rpcimpl.cc index b7e74f347..f3164ade6 100644 --- a/libtransmission/rpcimpl.cc +++ b/libtransmission/rpcimpl.cc @@ -128,10 +128,11 @@ auto getTorrents(tr_session* session, tr_variant* args) { time_t const cutoff = tr_time() - RecentlyActiveSeconds; - torrents.reserve(std::size(session->torrents())); + auto const& by_id = session->torrents().sorted_by_id(); + torrents.reserve(std::size(by_id)); std::copy_if( - std::begin(session->torrents()), - std::end(session->torrents()), + std::begin(by_id), + std::end(by_id), std::back_inserter(torrents), [&cutoff](auto const* tor) { return tor->has_changed_since(cutoff); }); } @@ -146,8 +147,8 @@ auto getTorrents(tr_session* session, tr_variant* args) } else // all of them { - torrents.reserve(std::size(session->torrents())); - std::copy(std::begin(session->torrents()), std::end(session->torrents()), std::back_inserter(torrents)); + auto const& by_id = session->torrents().sorted_by_id(); + torrents = std::vector{ std::begin(by_id), std::end(by_id) }; } return torrents; diff --git a/libtransmission/torrents.h b/libtransmission/torrents.h index 8ff149af5..bd5921ed7 100644 --- a/libtransmission/torrents.h +++ b/libtransmission/torrents.h @@ -107,6 +107,11 @@ public: return std::empty(by_hash_); } + [[nodiscard]] constexpr auto const& sorted_by_id() const noexcept + { + return by_id_; + } + private: std::vector by_hash_;