bug 441794: cache the keys to make setting up the UI O(n) rather than O(n^2)
authorDaniel Brooks <db48x@db48x.net>
Sun, 06 Jul 2008 05:01:33 -0500
changeset 64874 5dd4f9e9247dc0c75ee4a92b05e7baccbcdb7368
parent 64873 d054bcd4db2ca68b99f85c0013d4b86491f9814f
child 64875 1902ec08fc178003efa291595e7ef6963d53c5c5
push id19389
push userffxbld
push dateWed, 06 Apr 2011 21:33:21 +0000
treeherdermozilla-central@8e9f90073a20 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs441794
bug 441794: cache the keys to make setting up the UI O(n) rather than O(n^2)
mobile/chrome/content/shortcuts.js
--- a/mobile/chrome/content/shortcuts.js
+++ b/mobile/chrome/content/shortcuts.js
@@ -33,47 +33,51 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 // TODO: need to make the listbox editable, and to save the changes to prefs
 // TODO: read the prefs when the window is opened, and make the required changes
+// TODO: see about grouping the keys into categories
 
 //Components.utils.import("resource://gre/modules/json.jsm");
 
 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 
 function ShortcutEditor()
 {
     var prefsvc = Components.classes["@mozilla.org/preferences-service;1"]
                             .getService(Components.interfaces.nsIPrefService);
     var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                           .getService(Components.interfaces.nsIPrefBranch2);
     var keyPrefs = prefsvc.getBranch("shortcut.");
+    var keyCache;
 
     // first, we need to be able to manipulate the keys and commands themselves
     function getCommandNames()
     {
         return Array.map(document.getElementsByTagNameNS(XUL_NS, "command"), function(c) { return c.getAttribute("id"); });
     }
 
+    function getKeys()
+    {
+        if (keys)
+            return keys;
+
+        keyCache = { };
+        Array.map(document.getElementsByTagNameNS(XUL_NS, "key"), function(k) { keyCache[k.getAttribute("command")] = k; });
+        return keyCache;
+    }
+
     function findKeyForCommand(command)
     {
-        // looks for a key the calls the named command, or null if there aren't any
-        // TODO: cache the list of keys, invalidating the cache when we change it
-        // TODO: the chache should be a hash of command names to key elements,
-        //       other wise fillShortcutList will be O(n^2) on the number of
-        //       commands.
-        var keys = document.getElementsByTagNameNS(XUL_NS, "key");
-        var l = keys.length;
-        for (var i = 0; i < l; i++)
-            if (keys[i].getAttribute("command") == command)
-                return keys[i];
+        // returns the key the calls the named command, or null if there isn't one
+        return getKeys()[command];
     }
 
     function findCommandForKey(keySpec)
     {
         // TODO: This is a bit simplistic as yet. For example, we should match
         //       a key with an optional modifier even if that modifier isn't
         //       specified in our arguments. Also, we need to differentiate
         //       between a key element with an attribute that is an empty string
@@ -108,19 +112,20 @@ function ShortcutEditor()
         else
         {
             key = document.createElementNS(XUL_NS, "key");
             key.setAttribute("modifiers") = keySpec.modifiers;
             key.setAttribute("key") = keySpec.key;
             key.setAttribute("keycode") = keySpec.keycode;
             key.setAttribute("command") = command;
             document.getElementById("mainKeyset").appendChild(k);
+            keys[command] = key;
         }
 
-        return k;
+        return key;
     }
 
     function makeKeySpec(modifiers, key, keycode)
     {
         if (modifiers instanceof Components.interfaces.nsIDOMElement)
             return {
                 modifiers: modifiers.getAttribute("modifiers"),
                 key: modifiers.getAttribute("key"),