Bug 1366693 - add throttling when retrieving mutations in inspector front;r=pbro draft
authorJulian Descottes <jdescottes@mozilla.com>
Mon, 09 Oct 2017 13:20:25 +0200
changeset 676701 b3dfd2a2ac99baa8f66246ebd5ea24565d9a3817
parent 676700 8aac53786cd6a3b8aae21a11999d512043590c8f
child 735027 8e1afeea293b9dc6f19b33d7158d7936fb643477
push id83594
push userjdescottes@mozilla.com
push dateMon, 09 Oct 2017 11:22:07 +0000
reviewerspbro
bugs1366693
milestone58.0a1
Bug 1366693 - add throttling when retrieving mutations in inspector front;r=pbro MozReview-Commit-ID: KaXW7UeNQny
devtools/shared/fronts/inspector.js
--- a/devtools/shared/fronts/inspector.js
+++ b/devtools/shared/fronts/inspector.js
@@ -15,16 +15,22 @@ const {
   walkerSpec
 } = require("devtools/shared/specs/inspector");
 const defer = require("devtools/shared/defer");
 const { Task } = require("devtools/shared/task");
 loader.lazyRequireGetter(this, "nodeConstants",
   "devtools/shared/dom-node-constants");
 loader.lazyRequireGetter(this, "CommandUtils",
   "devtools/client/shared/developer-toolbar", true);
+const { throttle } = require("devtools/client/inspector/shared/utils");
+
+/**
+ * Delay between two calls to getMutations.
+ */
+const MUTATIONS_THROTTLING_DELAY = 500;
 
 /**
  * Client side of the DOM walker.
  */
 const WalkerFront = FrontClassWithSpec(walkerSpec, {
   // Set to true if cleanup should be requested after every mutation list.
   autoCleanup: true,
 
@@ -38,16 +44,25 @@ const WalkerFront = FrontClassWithSpec(w
     });
   }, {impl: "_pick"}),
 
   initialize: function (client, form) {
     this._createRootNodePromise();
     Front.prototype.initialize.call(this, client, form);
     this._orphaned = new Set();
     this._retainedOrphans = new Set();
+
+    /**
+     * If many mutations are fired at the same time, clients might sequentially request
+     * children/siblings for updated nodes, which can be costly. By throttling the calls
+     * to getMutations, duplicated mutations will be ignored.
+     */
+    this._throttledGetMutations = throttle(() => {
+      this.getMutations({cleanup: this.autoCleanup}).catch(() => {});
+    }, MUTATIONS_THROTTLING_DELAY);
   },
 
   destroy: function () {
     Front.prototype.destroy.call(this);
   },
 
   // Update the object given a form representation off the wire.
   form: function (json) {
@@ -432,17 +447,17 @@ const WalkerFront = FrontClassWithSpec(w
   }),
 
   /**
    * Handle the `new-mutations` notification by fetching the
    * available mutation records.
    */
   onMutations: preEvent("new-mutations", function () {
     // Fetch and process the mutations.
-    this.getMutations({cleanup: this.autoCleanup}).catch(() => {});
+    this._throttledGetMutations();
   }),
 
   isLocal: function () {
     return !!this.conn._transport._serverConnection;
   },
 
   // XXX hack during transition to remote inspector: get a proper NodeFront
   // for a given local node.  Only works locally.