From cc365c20dced1ac10b30c68193a99a97539594df Mon Sep 17 00:00:00 2001 From: John Bieling Date: Tue, 19 Nov 2024 07:49:56 +0200 Subject: [PATCH] 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 --- .../extensions/parent/ext-folders.js | 36 +++++++++++++------ mailnews/base/src/FolderUtils.sys.mjs | 5 ++- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/mail/components/extensions/parent/ext-folders.js b/mail/components/extensions/parent/ext-folders.js index d05fd90373..0c56d52955 100644 --- a/mail/components/extensions/parent/ext-folders.js +++ b/mail/components/extensions/parent/ext-folders.js @@ -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 => diff --git a/mailnews/base/src/FolderUtils.sys.mjs b/mailnews/base/src/FolderUtils.sys.mjs index 95379d048e..f7019ebe93 100644 --- a/mailnews/base/src/FolderUtils.sys.mjs +++ b/mailnews/base/src/FolderUtils.sys.mjs @@ -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