Bug 1322869: Part 2 - Memoize some relatively expensive-to-compute tab attributes. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Mon, 19 Dec 2016 12:34:50 -0800
changeset 451170 1043ce2112365fe1ed10a4ee2e52153c6f98bb05
parent 451169 3249d06fb4fb7946011625c34247168239aabbd9
child 539945 e084b79315af4f6fa45bac737538f7641ef5b9af
push id39073
push usermaglione.k@gmail.com
push dateMon, 19 Dec 2016 20:35:35 +0000
reviewersaswan
bugs1322869
milestone53.0a1
Bug 1322869: Part 2 - Memoize some relatively expensive-to-compute tab attributes. r?aswan MozReview-Commit-ID: EpjsLhlNzvG
browser/components/extensions/ext-utils.js
toolkit/components/extensions/ExtensionUtils.jsm
--- a/browser/components/extensions/ext-utils.js
+++ b/browser/components/extensions/ext-utils.js
@@ -670,17 +670,17 @@ ExtensionTabManager.prototype = {
       id: TabManager.getId(tab),
       index: tab._tPos,
       windowId: WindowManager.getId(window),
       selected: tab.selected,
       highlighted: tab.selected,
       active: tab.selected,
       pinned: tab.pinned,
       status: TabManager.getStatus(tab),
-      incognito: PrivateBrowsingUtils.isBrowserPrivate(browser),
+      incognito: WindowManager.isBrowserPrivate(browser),
       width: browser.frameLoader.lazyWidth || browser.clientWidth,
       height: browser.frameLoader.lazyHeight || browser.clientHeight,
       audible: tab.soundPlaying,
       mutedInfo,
     };
     if (this.extension.hasPermission("cookies")) {
       result.cookieStoreId = getCookieStoreIdForTab(result, tab);
     }
@@ -920,16 +920,21 @@ TabManager.for = function(extension) {
 };
 
 /* eslint-disable mozilla/balanced-listeners */
 extensions.on("shutdown", (type, extension) => {
   tabManagers.delete(extension);
 });
 /* eslint-enable mozilla/balanced-listeners */
 
+function memoize(fn) {
+  let weakMap = new DefaultWeakMap(fn);
+  return weakMap.get.bind(weakMap);
+}
+
 // Manages mapping between XUL windows and extension window IDs.
 global.WindowManager = {
   // Note: These must match the values in windows.json.
   WINDOW_ID_NONE: -1,
   WINDOW_ID_CURRENT: -2,
 
   get topWindow() {
     return Services.wm.getMostRecentWindow("navigator:browser");
@@ -959,23 +964,26 @@ global.WindowManager = {
 
     if (options.width !== null || options.height !== null) {
       let width = options.width !== null ? options.width : window.outerWidth;
       let height = options.height !== null ? options.height : window.outerHeight;
       window.resizeTo(width, height);
     }
   },
 
-  getId(window) {
-    if (!window.QueryInterface) {
-      return null;
+  isBrowserPrivate: memoize(browser => {
+    return PrivateBrowsingUtils.isBrowserPrivate(browser);
+  }),
+
+  getId: memoize(window => {
+    if (window instanceof Ci.nsIInterfaceRequestor) {
+      return window.getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
     }
-    return window.QueryInterface(Ci.nsIInterfaceRequestor)
-                 .getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
-  },
+    return null;
+  }),
 
   getWindow(id, context) {
     if (id == this.WINDOW_ID_CURRENT) {
       return currentWindow(context);
     }
 
     for (let window of WindowListManager.browserWindows(true)) {
       if (this.getId(window) == id) {
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -154,17 +154,17 @@ function extend(obj, ...args) {
 class DefaultWeakMap extends WeakMap {
   constructor(defaultConstructor, init) {
     super(init);
     this.defaultConstructor = defaultConstructor;
   }
 
   get(key) {
     if (!this.has(key)) {
-      this.set(key, this.defaultConstructor());
+      this.set(key, this.defaultConstructor(key));
     }
     return super.get(key);
   }
 }
 
 class DefaultMap extends Map {
   constructor(defaultConstructor, init) {
     super(init);