Browse Source

Avoid holding encoded resume data in memory

Now it the encoded resume data will be streamed to file instead of a
temporary buffer holding the whole of it.
adaptive-webui-19844
Chocobo1 5 years ago
parent
commit
d8401c76f5
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 22
      src/base/bittorrent/private/resumedatasavingmanager.cpp
  2. 7
      src/base/bittorrent/private/resumedatasavingmanager.h
  3. 15
      src/base/bittorrent/session.cpp
  4. 3
      src/base/bittorrent/session.h
  5. 15
      src/base/bittorrent/torrenthandle.cpp

22
src/base/bittorrent/private/resumedatasavingmanager.cpp

@ -28,11 +28,15 @@ @@ -28,11 +28,15 @@
#include "resumedatasavingmanager.h"
#include <libtorrent/bencode.hpp>
#include <libtorrent/entry.hpp>
#include <QByteArray>
#include <QSaveFile>
#include "base/logger.h"
#include "base/utils/fs.h"
#include "base/utils/io.h"
ResumeDataSavingManager::ResumeDataSavingManager(const QString &resumeFolderPath)
: m_resumeDataDir(resumeFolderPath)
@ -50,6 +54,24 @@ void ResumeDataSavingManager::save(const QString &filename, const QByteArray &da @@ -50,6 +54,24 @@ void ResumeDataSavingManager::save(const QString &filename, const QByteArray &da
}
}
void ResumeDataSavingManager::save(const QString &filename, const std::shared_ptr<lt::entry> &data) const
{
const QString filepath = m_resumeDataDir.absoluteFilePath(filename);
QSaveFile file {filepath};
if (!file.open(QIODevice::WriteOnly)) {
LogMsg(tr("Couldn't save data to '%1'. Error: %2")
.arg(filepath, file.errorString()), Log::CRITICAL);
return;
}
lt::bencode(Utils::IO::FileDeviceOutputIterator {file}, *data);
if ((file.error() != QFileDevice::NoError) || !file.commit()) {
LogMsg(tr("Couldn't save data to '%1'. Error: %2")
.arg(filepath, file.errorString()), Log::CRITICAL);
}
}
void ResumeDataSavingManager::remove(const QString &filename) const
{
const QString filepath = m_resumeDataDir.absoluteFilePath(filename);

7
src/base/bittorrent/private/resumedatasavingmanager.h

@ -28,6 +28,10 @@ @@ -28,6 +28,10 @@
#pragma once
#include <memory>
#include <libtorrent/fwd.hpp>
#include <QDir>
#include <QObject>
@ -43,8 +47,9 @@ public: @@ -43,8 +47,9 @@ public:
public slots:
void save(const QString &filename, const QByteArray &data) const;
void save(const QString &filename, const std::shared_ptr<lt::entry> &data) const;
void remove(const QString &filename) const;
private:
QDir m_resumeDataDir;
const QDir m_resumeDataDir;
};

15
src/base/bittorrent/session.cpp

@ -3989,25 +3989,20 @@ void Session::handleTorrentFinished(TorrentHandle *const torrent) @@ -3989,25 +3989,20 @@ void Session::handleTorrentFinished(TorrentHandle *const torrent)
emit allTorrentsFinished();
}
void Session::handleTorrentResumeDataReady(TorrentHandle *const torrent, const lt::entry &data)
void Session::handleTorrentResumeDataReady(TorrentHandle *const torrent, const std::shared_ptr<lt::entry> &data)
{
--m_numResumeData;
// Separated thread is used for the blocking IO which results in slow processing of many torrents.
// Encoding data in parallel while doing IO saves time. Copying lt::entry objects around
// isn't cheap too.
QByteArray out;
out.reserve(1024 * 1024); // most fastresume file sizes are under 1 MB
lt::bencode(std::back_inserter(out), data);
// Copying lt::entry objects around isn't cheap.
const QString filename = QString::fromLatin1("%1.fastresume").arg(torrent->hash());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
QMetaObject::invokeMethod(m_resumeDataSavingManager
, [this, filename, out]() { m_resumeDataSavingManager->save(filename, out); });
, [this, filename, data]() { m_resumeDataSavingManager->save(filename, data); });
#else
QMetaObject::invokeMethod(m_resumeDataSavingManager, "save",
Q_ARG(QString, filename), Q_ARG(QByteArray, out));
QMetaObject::invokeMethod(m_resumeDataSavingManager, "save"
, Q_ARG(QString, filename), Q_ARG(std::shared_ptr<lt::entry>, data));
#endif
}

3
src/base/bittorrent/session.h

@ -30,6 +30,7 @@ @@ -30,6 +30,7 @@
#ifndef BITTORRENT_SESSION_H
#define BITTORRENT_SESSION_H
#include <memory>
#include <vector>
#include <libtorrent/fwd.hpp>
@ -459,7 +460,7 @@ namespace BitTorrent @@ -459,7 +460,7 @@ namespace BitTorrent
void handleTorrentTrackersChanged(TorrentHandle *const torrent);
void handleTorrentUrlSeedsAdded(TorrentHandle *const torrent, const QVector<QUrl> &newUrlSeeds);
void handleTorrentUrlSeedsRemoved(TorrentHandle *const torrent, const QVector<QUrl> &urlSeeds);
void handleTorrentResumeDataReady(TorrentHandle *const torrent, const lt::entry &data);
void handleTorrentResumeDataReady(TorrentHandle *const torrent, const std::shared_ptr<lt::entry> &data);
void handleTorrentResumeDataFailed(TorrentHandle *const torrent);
void handleTorrentTrackerReply(TorrentHandle *const torrent, const QString &trackerUrl);
void handleTorrentTrackerWarning(TorrentHandle *const torrent, const QString &trackerUrl);

15
src/base/bittorrent/torrenthandle.cpp

@ -30,6 +30,7 @@ @@ -30,6 +30,7 @@
#include "torrenthandle.h"
#include <algorithm>
#include <memory>
#include <type_traits>
#ifdef Q_OS_WIN
@ -1647,14 +1648,16 @@ void TorrentHandle::handleSaveResumeDataAlert(const lt::save_resume_data_alert * @@ -1647,14 +1648,16 @@ void TorrentHandle::handleSaveResumeDataAlert(const lt::save_resume_data_alert *
{
#if (LIBTORRENT_VERSION_NUM < 10200)
const bool useDummyResumeData = !(p && p->resume_data);
lt::entry dummyEntry;
lt::entry &resumeData = useDummyResumeData ? dummyEntry : *(p->resume_data);
auto resumeDataPtr = std::make_shared<lt::entry>(useDummyResumeData
? lt::entry {}
: *(p->resume_data));
#else
const bool useDummyResumeData = !p;
lt::entry resumeData = useDummyResumeData ? lt::entry() : lt::write_resume_data(p->params);
auto resumeDataPtr = std::make_shared<lt::entry>(useDummyResumeData
? lt::entry {}
: lt::write_resume_data(p->params));
#endif
lt::entry &resumeData = *resumeDataPtr;
updateStatus();
@ -1697,7 +1700,7 @@ void TorrentHandle::handleSaveResumeDataAlert(const lt::save_resume_data_alert * @@ -1697,7 +1700,7 @@ void TorrentHandle::handleSaveResumeDataAlert(const lt::save_resume_data_alert *
resumeData["auto_managed"] = false;
}
m_session->handleTorrentResumeDataReady(this, resumeData);
m_session->handleTorrentResumeDataReady(this, resumeDataPtr);
}
void TorrentHandle::handleSaveResumeDataFailedAlert(const lt::save_resume_data_failed_alert *p)

Loading…
Cancel
Save