From 0cc318664d97697d64a48955aaeb360b6f4d32ab Mon Sep 17 00:00:00 2001 From: Prince Gupta <34717789+jagannatharjun@users.noreply.github.com> Date: Mon, 24 Jan 2022 08:25:06 +0530 Subject: [PATCH] Improve Torrent content tree structure creation Use QHash to cache folder items. PR #16183. --- src/gui/torrentcontentmodel.cpp | 8 +++++--- src/gui/torrentcontentmodelfolder.cpp | 11 ----------- src/gui/torrentcontentmodelfolder.h | 1 - 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/gui/torrentcontentmodel.cpp b/src/gui/torrentcontentmodel.cpp index 1eb0176af..733f90587 100644 --- a/src/gui/torrentcontentmodel.cpp +++ b/src/gui/torrentcontentmodel.cpp @@ -498,6 +498,7 @@ void TorrentContentModel::setupModelData(const BitTorrent::AbstractFileStorage & m_filesIndex.reserve(filesCount); TorrentContentModelFolder *currentParent; + QHash> folderMap; // Iterate over files for (int i = 0; i < filesCount; ++i) { @@ -510,13 +511,14 @@ void TorrentContentModel::setupModelData(const BitTorrent::AbstractFileStorage & for (const QStringView pathPart : asConst(pathFolders)) { - const QString folderPath = pathPart.toString(); - TorrentContentModelFolder *newParent = currentParent->childFolderWithName(folderPath); + const QString folderName = pathPart.toString(); + TorrentContentModelFolder *&newParent = folderMap[currentParent][folderName]; if (!newParent) { - newParent = new TorrentContentModelFolder(folderPath, currentParent); + newParent = new TorrentContentModelFolder(folderName, currentParent); currentParent->appendChild(newParent); } + currentParent = newParent; } // Actually create the file diff --git a/src/gui/torrentcontentmodelfolder.cpp b/src/gui/torrentcontentmodelfolder.cpp index c68b82d67..abece9c9b 100644 --- a/src/gui/torrentcontentmodelfolder.cpp +++ b/src/gui/torrentcontentmodelfolder.cpp @@ -81,17 +81,6 @@ TorrentContentModelItem *TorrentContentModelFolder::child(int row) const { return m_childItems.value(row, nullptr); } - -TorrentContentModelFolder *TorrentContentModelFolder::childFolderWithName(const QString &name) const -{ - for (TorrentContentModelItem *child : asConst(m_childItems)) - { - if ((child->itemType() == FolderType) && (child->name() == name)) - return static_cast(child); - } - return nullptr; -} - int TorrentContentModelFolder::childCount() const { return m_childItems.count(); diff --git a/src/gui/torrentcontentmodelfolder.h b/src/gui/torrentcontentmodelfolder.h index 80ba601ee..d200a12c1 100644 --- a/src/gui/torrentcontentmodelfolder.h +++ b/src/gui/torrentcontentmodelfolder.h @@ -59,7 +59,6 @@ public: const QVector &children() const; void appendChild(TorrentContentModelItem *item); TorrentContentModelItem *child(int row) const; - TorrentContentModelFolder *childFolderWithName(const QString &name) const; int childCount() const; private: