optimize construction of disk io storage objects

This commit is contained in:
arvidn 2017-03-23 08:18:04 -04:00 committed by Arvid Norberg
parent fcbcc250bb
commit 206a80acfb
2 changed files with 22 additions and 10 deletions

View File

@ -579,6 +579,9 @@ namespace libtorrent
aux::vector<std::shared_ptr<storage_interface>, storage_index_t> m_torrents; aux::vector<std::shared_ptr<storage_interface>, storage_index_t> m_torrents;
// indices into m_torrents to empty slots
std::vector<storage_index_t> m_free_slots;
#if TORRENT_USE_ASSERTS #if TORRENT_USE_ASSERTS
int m_magic = 0x1337; int m_magic = 0x1337;
std::atomic<bool> m_jobs_aborted{false}; std::atomic<bool> m_jobs_aborted{false};

View File

@ -209,21 +209,30 @@ namespace libtorrent
storage_holder disk_io_thread::new_torrent(std::unique_ptr<storage_interface> storage) storage_holder disk_io_thread::new_torrent(std::unique_ptr<storage_interface> storage)
{ {
TORRENT_ASSERT(storage); TORRENT_ASSERT(storage);
auto it = std::find(m_torrents.begin(), m_torrents.end() if (m_free_slots.empty())
, std::shared_ptr<storage_interface>()); {
storage_index_t const idx((it == m_torrents.end()) storage_index_t const idx = m_torrents.end_index();
? m_torrents.end_index() m_torrents.emplace_back(std::move(storage));
: storage_index_t(std::uint32_t(it - m_torrents.begin()))); m_torrents.back()->set_storage_index(idx);
storage->set_storage_index(idx); return storage_holder(idx, *this);
if (it == m_torrents.end()) m_torrents.emplace_back(std::move(storage)); }
else m_torrents[idx] = std::move(storage); else
return storage_holder(idx, *this); {
storage_index_t const idx = m_free_slots.back();
m_free_slots.pop_back();
(m_torrents[idx] = std::move(storage))->set_storage_index(idx);
return storage_holder(idx, *this);
}
} }
void disk_io_thread::remove_torrent(storage_index_t const idx) void disk_io_thread::remove_torrent(storage_index_t const idx)
{ {
auto& pos = m_torrents[idx]; auto& pos = m_torrents[idx];
if (pos->dec_refcount() == 0) pos.reset(); if (pos->dec_refcount() == 0)
{
pos.reset();
m_free_slots.push_back(idx);
}
} }
disk_io_thread::~disk_io_thread() disk_io_thread::~disk_io_thread()