Bug 1592256 - Cache WebExtension message identifiers to improve lookup performance from O(n) to O(1). r=mkmelin DONTBUILD
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 04 Nov 2019 21:17:26 +1300
changeset 37422 4865f56365ce8111c4627f13961c805463395dea
parent 37421 2e016c75c928684dab9fe9b6ca28b6514c94a2e9
child 37423 b2f5e2c9055bbeecdace64905846797d4935aef2
push id396
push userclokep@gmail.com
push dateMon, 06 Jan 2020 23:11:57 +0000
reviewersmkmelin
bugs1592256
Bug 1592256 - Cache WebExtension message identifiers to improve lookup performance from O(n) to O(1). r=mkmelin DONTBUILD
mail/components/extensions/parent/ext-mail.js
--- a/mail/components/extensions/parent/ext-mail.js
+++ b/mail/components/extensions/parent/ext-mail.js
@@ -1363,32 +1363,36 @@ function convertMessage(msgHdr, extensio
 }
 
 /**
  * A map of numeric identifiers to messages for easy reference.
  */
 var messageTracker = {
   _nextId: 1,
   _messages: new Map(),
+  _messageIds: new Map(),
 
   /**
    * Finds a message in the map or adds it to the map.
    * @return {int} The identifier of the message
    */
   getId(msgHdr) {
-    for (let [key, value] of this._messages.entries()) {
-      if (
-        value.folderURI == msgHdr.folder.URI &&
-        value.messageId == msgHdr.messageId
-      ) {
-        return key;
-      }
+    // Using stringify avoids potential issues with unexpected characters.
+    // This hash could be anything as long as it is unique for each
+    // [folder, message] combination.
+    let hash = JSON.stringify([msgHdr.folder.URI, msgHdr.messageId]);
+    if (this._messageIds.has(hash)) {
+      return this._messageIds.get(hash);
     }
     let id = this._nextId++;
-    this.setId(msgHdr, id);
+    this._messages.set(id, {
+      folderURI: msgHdr.folder.URI,
+      messageId: msgHdr.messageId,
+    });
+    this._messageIds.set(hash, id);
     return id;
   },
 
   /**
    * Retrieves a message from the map. If the message no longer exists,
    * it is removed from the map.
    * @return {nsIMsgHdr} The identifier of the message
    */
@@ -1404,26 +1408,16 @@ var messageTracker = {
       if (msgHdr) {
         return msgHdr;
       }
     }
 
     this._messages.delete(id);
     return null;
   },
-
-  /**
-   * Adds a message to the map.
-   */
-  setId(msgHdr, id) {
-    this._messages.set(id, {
-      folderURI: msgHdr.folder.URI,
-      messageId: msgHdr.messageId,
-    });
-  },
 };
 
 /**
  * Tracks lists of messages so that an extension can consume them in chunks.
  * Any WebExtensions method that could return multiple messages should instead call
  * messageListTracker.startList and return the results, which contain the first
  * chunk. Further chunks can be fetched by the extension calling
  * browser.messages.continueList. Chunk size is controlled by a pref.