From d5e6d161f2bd7a7e4f6307b2660b3c2476dd98ca Mon Sep 17 00:00:00 2001 From: Prince Gupta <34717789+jagannatharjun@users.noreply.github.com> Date: Wed, 15 Jun 2022 22:44:53 +0530 Subject: [PATCH] Cache last traverse result while parsing file tree PR #16376. --- src/gui/torrentcontentmodel.cpp | 38 ++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/gui/torrentcontentmodel.cpp b/src/gui/torrentcontentmodel.cpp index 1064029c7..4663007e1 100644 --- a/src/gui/torrentcontentmodel.cpp +++ b/src/gui/torrentcontentmodel.cpp @@ -505,34 +505,46 @@ void TorrentContentModel::setupModelData(const BitTorrent::AbstractFileStorage & qDebug("Torrent contains %d files", filesCount); m_filesIndex.reserve(filesCount); - TorrentContentModelFolder *currentParent; QHash> folderMap; + QVector lastParentPath; + TorrentContentModelFolder *lastParent = m_rootItem; // Iterate over files for (int i = 0; i < filesCount; ++i) { - currentParent = m_rootItem; const QString path = info.filePath(i).data(); // Iterate of parts of the path to create necessary folders QList pathFolders = QStringView(path).split(u'/', Qt::SkipEmptyParts); - pathFolders.removeLast(); + const QString fileName = pathFolders.takeLast().toString(); - for (const QStringView pathPart : asConst(pathFolders)) + if (!std::equal(lastParentPath.begin(), lastParentPath.end() + , pathFolders.begin(), pathFolders.end())) { - const QString folderName = pathPart.toString(); - TorrentContentModelFolder *&newParent = folderMap[currentParent][folderName]; - if (!newParent) + lastParentPath.clear(); + lastParentPath.reserve(pathFolders.size()); + + // rebuild the path from the root + lastParent = m_rootItem; + for (const QStringView pathPart : asConst(pathFolders)) { - newParent = new TorrentContentModelFolder(folderName, currentParent); - currentParent->appendChild(newParent); - } + const QString folderName = pathPart.toString(); + lastParentPath.push_back(folderName); - currentParent = newParent; + TorrentContentModelFolder *&newParent = folderMap[lastParent][folderName]; + if (!newParent) + { + newParent = new TorrentContentModelFolder(folderName, lastParent); + lastParent->appendChild(newParent); + } + + lastParent = newParent; + } } + // Actually create the file TorrentContentModelFile *fileItem = new TorrentContentModelFile( - info.filePath(i).filename(), info.fileSize(i), currentParent, i); - currentParent->appendChild(fileItem); + fileName, info.fileSize(i), lastParent, i); + lastParent->appendChild(fileItem); m_filesIndex.push_back(fileItem); } emit layoutChanged();