Bug 1459205 - Speed up dict type by preventing duplicated calls to `getType` from its write method. r=jryans draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Thu, 03 May 2018 12:47:27 -0700
changeset 791606 6419af4ede9514bb82c4bd574437537690da4a96
parent 791510 eaf7dc0250517875ee0a1972e0714a2cc5100fe6
push id108844
push userbmo:poirot.alex@gmail.com
push dateFri, 04 May 2018 17:26:30 +0000
reviewersjryans
bugs1459205
milestone61.0a1
Bug 1459205 - Speed up dict type by preventing duplicated calls to `getType` from its write method. r=jryans MozReview-Commit-ID: Dn37LGEpB6q
devtools/shared/protocol.js
--- a/devtools/shared/protocol.js
+++ b/devtools/shared/protocol.js
@@ -236,36 +236,49 @@ types.addArrayType = function(subtype) {
  *
  * Properties of the value that aren't included in the specializations
  * will be serialized as primitive values.
  *
  * @param object specializations
  *    A dict of property names => type
  */
 types.addDictType = function(name, specializations) {
+  let specTypes = {};
+  for (let prop in specializations) {
+    try {
+      specTypes[prop] = types.getType(specializations[prop]);
+    } catch (e) {
+      // Types may not be defined yet. Sometimes, we define the type *after* using it, but
+      // also, we have cyclic definitions on types. So lazily load them when they are not
+      // immediately available.
+      loader.lazyGetter(specTypes, prop, () => {
+        return types.getType(specializations[prop]);
+      });
+    }
+  }
   return types.addType(name, {
     category: "dict",
-    specializations: specializations,
+    specializations,
     read: (v, ctx) => {
       let ret = {};
       for (let prop in v) {
-        if (prop in specializations) {
-          ret[prop] = types.getType(specializations[prop]).read(v[prop], ctx);
+        if (prop in specTypes) {
+          ret[prop] = specTypes[prop].read(v[prop], ctx);
         } else {
           ret[prop] = v[prop];
         }
       }
       return ret;
     },
 
     write: (v, ctx) => {
       let ret = {};
       for (let prop in v) {
-        if (prop in specializations) {
-          ret[prop] = types.getType(specializations[prop]).write(v[prop], ctx);
+        if (prop in specTypes) {
+          ret[prop] = specTypes[prop].write(v[prop], ctx);
         } else {
           ret[prop] = v[prop];
         }
       }
       return ret;
     }
   });
 };