From 93856a5ce8d6a75a16fae0bd62f7b063991590fe Mon Sep 17 00:00:00 2001 From: Alexandre Poirot Date: Thu, 3 May 2018 12:47:27 -0700 Subject: [PATCH] Bug 1459205 - Speed up dict type by preventing duplicated calls to `getType` from its write method. r=jryans MozReview-Commit-ID: Dn37LGEpB6q --HG-- extra : rebase_source : 11cf83fdc1dba6d8b13c9440adf1e6d1d2b6d554 --- devtools/shared/protocol.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/devtools/shared/protocol.js b/devtools/shared/protocol.js index 161373a978c0..1d1246373f14 100644 --- a/devtools/shared/protocol.js +++ b/devtools/shared/protocol.js @@ -241,14 +241,27 @@ types.addArrayType = function(subtype) { * 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]; } @@ -259,8 +272,8 @@ types.addDictType = function(name, specializations) { 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]; }