Bug 1459205 - Speed up dict type by preventing duplicated calls to `getType` from its write method. r=jryans
MozReview-Commit-ID: Dn37LGEpB6q
--- 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;
}
});
};