Bug 1928953 - Improve performance of recent folder queries. r=Fallen

Performance improvements as suggested by Philipp Kewisch.

Differential Revision: https://phabricator.services.mozilla.com/D227822

--HG--
extra : amend_source : 6985daf0f376ed7f7be6c7bec48b068f7eb8fa95
This commit is contained in:
John Bieling 2024-11-19 07:49:56 +02:00
Родитель 71c23c3b82
Коммит cc365c20dc
2 изменённых файлов: 29 добавлений и 12 удалений

Просмотреть файл

@ -598,6 +598,10 @@ this.folders = class extends ExtensionAPIPersistent {
extensionApi: this,
}).api(),
async query(queryInfo) {
const monthOld = Math.floor(
(Date.now() - FolderUtils.ONE_MONTH_IN_MILLISECONDS) / 1000
);
// Generator function to flatten the folder structure.
function* getFlatFolderStructure(folder) {
yield folder;
@ -629,6 +633,15 @@ this.folders = class extends ExtensionAPIPersistent {
return true;
}
function isRecent(folder) {
try {
const time = Number(folder.getStringProperty("MRUTime")) || 0;
return !(time < monthOld);
} catch (e) {
return false;
}
}
// Prepare folders, which are to be searched.
const parentFolders = [];
if (queryInfo.folderId) {
@ -739,7 +752,12 @@ this.folders = class extends ExtensionAPIPersistent {
const { accountId, rootFolder } = parentFolder;
for (const folder of getFlatFolderStructure(rootFolder)) {
// Apply search criteria.
const isServer = folder.isServer;
if (
queryInfo.recent !== null &&
queryInfo.recent != isRecent(folder)
) {
return false;
}
if (
queryInfo.isFavorite != null &&
@ -749,6 +767,7 @@ this.folders = class extends ExtensionAPIPersistent {
continue;
}
const isServer = folder.isServer;
if (queryInfo.isRoot != null && queryInfo.isRoot != isServer) {
continue;
}
@ -867,29 +886,24 @@ this.folders = class extends ExtensionAPIPersistent {
}
}
if (queryInfo.recent != null) {
// Sort by recentness for recent queries. Apply the limit, but ignore
// limit of -1 = mail.folder_widget.max_recent for non-recent queries.
if (queryInfo.recent) {
let limit = queryInfo.limit || Infinity;
if (limit == -1) {
limit = Services.prefs.getIntPref(
"mail.folder_widget.max_recent"
);
}
const recentFolders = FolderUtils.getMostRecentFolders(
foundFolders = FolderUtils.getMostRecentFolders(
foundFolders,
limit,
"MRUTime"
);
if (queryInfo.recent) {
foundFolders = recentFolders;
} else {
foundFolders = foundFolders.filter(
x => !recentFolders.includes(x)
);
}
} else if (queryInfo.limit && queryInfo.limit > 0) {
// If limit is used without recent, mail.folder_widget.max_recent is
// ignored.
foundFolders = foundFolders.slice(0, queryInfo.limit);
foundFolders.splice(queryInfo.limit);
}
return foundFolders.map(folder =>

Просмотреть файл

@ -12,6 +12,8 @@ const OUTGOING_FOLDER_FLAGS =
Ci.nsMsgFolderFlags.Queue |
Ci.nsMsgFolderFlags.Templates;
const ONE_MONTH_IN_MILLISECONDS = 31 * 24 * 60 * 60 * 1000;
export var FolderUtils = {
allAccountsSorted,
folderNameCompare,
@ -22,6 +24,7 @@ export var FolderUtils = {
canRenameDeleteJunkMail,
isSmartTagsFolder,
isSmartVirtualFolder,
ONE_MONTH_IN_MILLISECONDS,
OUTGOING_FOLDER_FLAGS,
};
@ -164,7 +167,7 @@ function allAccountsSorted(aExcludeIMAccounts) {
*/
function getMostRecentFolders(aFolderList, aMaxHits, aTimeProperty) {
const recentFolders = [];
const monthOld = Math.floor((Date.now() - 31 * 24 * 60 * 60 * 1000) / 1000);
const monthOld = Math.floor((Date.now() - ONE_MONTH_IN_MILLISECONDS) / 1000);
/**
* This sub-function will add a folder to the recentFolders array if it