1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-25 14:04:23 +00:00

Cache last traverse result while parsing file tree

PR #16376.
This commit is contained in:
Prince Gupta 2022-06-15 22:44:53 +05:30 committed by GitHub
parent d1515456bc
commit d5e6d161f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -505,34 +505,46 @@ void TorrentContentModel::setupModelData(const BitTorrent::AbstractFileStorage &
qDebug("Torrent contains %d files", filesCount); qDebug("Torrent contains %d files", filesCount);
m_filesIndex.reserve(filesCount); m_filesIndex.reserve(filesCount);
TorrentContentModelFolder *currentParent;
QHash<TorrentContentModelFolder *, QHash<QString, TorrentContentModelFolder *>> folderMap; QHash<TorrentContentModelFolder *, QHash<QString, TorrentContentModelFolder *>> folderMap;
QVector<QString> lastParentPath;
TorrentContentModelFolder *lastParent = m_rootItem;
// Iterate over files // Iterate over files
for (int i = 0; i < filesCount; ++i) for (int i = 0; i < filesCount; ++i)
{ {
currentParent = m_rootItem;
const QString path = info.filePath(i).data(); const QString path = info.filePath(i).data();
// Iterate of parts of the path to create necessary folders // Iterate of parts of the path to create necessary folders
QList<QStringView> pathFolders = QStringView(path).split(u'/', Qt::SkipEmptyParts); QList<QStringView> 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(); lastParentPath.clear();
TorrentContentModelFolder *&newParent = folderMap[currentParent][folderName]; lastParentPath.reserve(pathFolders.size());
if (!newParent)
{
newParent = new TorrentContentModelFolder(folderName, currentParent);
currentParent->appendChild(newParent);
}
currentParent = newParent; // rebuild the path from the root
lastParent = m_rootItem;
for (const QStringView pathPart : asConst(pathFolders))
{
const QString folderName = pathPart.toString();
lastParentPath.push_back(folderName);
TorrentContentModelFolder *&newParent = folderMap[lastParent][folderName];
if (!newParent)
{
newParent = new TorrentContentModelFolder(folderName, lastParent);
lastParent->appendChild(newParent);
}
lastParent = newParent;
}
} }
// Actually create the file // Actually create the file
TorrentContentModelFile *fileItem = new TorrentContentModelFile( TorrentContentModelFile *fileItem = new TorrentContentModelFile(
info.filePath(i).filename(), info.fileSize(i), currentParent, i); fileName, info.fileSize(i), lastParent, i);
currentParent->appendChild(fileItem); lastParent->appendChild(fileItem);
m_filesIndex.push_back(fileItem); m_filesIndex.push_back(fileItem);
} }
emit layoutChanged(); emit layoutChanged();