Reduce the number of torrent_handle::torrent_file() calls in QTorrentHandle::prioritize_files(). Closes #2161.

It was reported (#2161) that enabling/disabling a downloading of a file
is considerably slow on libtorrent 1.0.3, but not on 0.16.x. The problem
is that a function torrent_file() in libttorrent 1.0.3 does a deep copy
of torrent_info, while get_torrent_info() in libtorrent 0.16.x only
returns a reference.
This commit is contained in:
Ivan Sorokin 2014-11-15 01:58:52 +03:00
parent 4dbc235c70
commit 404c8972af
2 changed files with 19 additions and 10 deletions

View file

@ -245,9 +245,9 @@ size_type QTorrentHandle::filesize_at(unsigned int index) const {
QString QTorrentHandle::filepath_at(unsigned int index) const {
#if LIBTORRENT_VERSION_NUM < 10000
return fsutils::fromNativePath(misc::toQStringU(torrent_handle::get_torrent_info().files().file_path(index)));
return filepath_at(torrent_handle::get_torrent_info(), index);
#else
return fsutils::fromNativePath(misc::toQStringU(torrent_handle::torrent_file()->files().file_path(index)));
return filepath_at(*torrent_handle::torrent_file(), index);
#endif
}
@ -508,10 +508,12 @@ void QTorrentHandle::file_priority(int index, int priority) const {
void QTorrentHandle::prioritize_files(const vector<int> &files) const {
#if LIBTORRENT_VERSION_NUM < 10000
if ((int)files.size() != torrent_handle::get_torrent_info().num_files()) return;
torrent_info const& info = torrent_handle::get_torrent_info();
#else
if ((int)files.size() != torrent_handle::torrent_file()->num_files()) return;
boost::intrusive_ptr<torrent_info const> info_ptr = torrent_handle::torrent_file();
torrent_info const& info = *info_ptr;
#endif
if ((int)files.size() != info.num_files()) return;
qDebug() << Q_FUNC_INFO;
bool was_seed = is_seed();
qDebug() << Q_FUNC_INFO << "Changing files priorities...";
@ -521,14 +523,15 @@ void QTorrentHandle::prioritize_files(const vector<int> &files) const {
QString spath = save_path();
for (uint i = 0; i < files.size(); ++i) {
QString filepath = filepath_at(info, i);
// Move unwanted files to a .unwanted subfolder
if (files[i] == 0) {
QString old_abspath = QDir(spath).absoluteFilePath(filepath_at(i));
QString old_abspath = QDir(spath).absoluteFilePath(filepath);
QString parent_abspath = fsutils::branchPath(old_abspath);
// Make sure the file does not already exists
if (QDir(parent_abspath).dirName() != ".unwanted") {
QString unwanted_abspath = parent_abspath+"/.unwanted";
QString new_abspath = unwanted_abspath+"/"+filename_at(i);
QString new_abspath = unwanted_abspath+"/"+fsutils::fileName(filepath);
qDebug() << "Unwanted path is" << unwanted_abspath;
if (QFile::exists(new_abspath)) {
qWarning() << "File" << new_abspath << "already exists at destination.";
@ -548,17 +551,17 @@ void QTorrentHandle::prioritize_files(const vector<int> &files) const {
#else
Q_UNUSED(created);
#endif
QString parent_path = fsutils::branchPath(filepath_at(i));
QString parent_path = fsutils::branchPath(filepath);
if (!parent_path.isEmpty() && !parent_path.endsWith("/"))
parent_path += "/";
rename_file(i, parent_path+".unwanted/"+filename_at(i));
rename_file(i, parent_path+".unwanted/"+fsutils::fileName(filepath));
}
}
// Move wanted files back to their original folder
if (files[i] > 0) {
QString parent_relpath = fsutils::branchPath(filepath_at(i));
QString parent_relpath = fsutils::branchPath(filepath);
if (QDir(parent_relpath).dirName() == ".unwanted") {
QString old_name = filename_at(i);
QString old_name = fsutils::fileName(filepath);
QString new_relpath = fsutils::branchPath(parent_relpath);
if (new_relpath.isEmpty())
rename_file(i, old_name);
@ -664,3 +667,8 @@ float QTorrentHandle::progress(const libtorrent::torrent_status &status) {
Q_ASSERT(progress >= 0.f && progress <= 1.f);
return progress;
}
QString QTorrentHandle::filepath_at(const libtorrent::torrent_info &info, unsigned int index) {
return fsutils::fromNativePath(misc::toQStringU(info.files().file_path(index)));
}

View file

@ -121,6 +121,7 @@ public:
static bool is_checking(const libtorrent::torrent_status &status);
static bool has_error(const libtorrent::torrent_status &status);
static float progress(const libtorrent::torrent_status &status);
static QString filepath_at(const libtorrent::torrent_info &info, unsigned int index);
private:
void prioritize_first_last_piece(int file_index, bool b) const;