Bug 1382645: Part 3 - Throw away description strings before blobbifying schema JSON. r?mixedpuppy draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 20 Jul 2017 14:44:35 -0700
changeset 612647 46d8be5ca880a54ae533f58ce71401e07601c718
parent 612586 42263850ff33ef1934e6678398376900d2ee540e
child 613434 683f829fb86df93407bdbc6e6d1baac61889a654
push id69553
push usermaglione.k@gmail.com
push dateThu, 20 Jul 2017 22:21:06 +0000
reviewersmixedpuppy
bugs1382645
milestone56.0a1
Bug 1382645: Part 3 - Throw away description strings before blobbifying schema JSON. r?mixedpuppy MozReview-Commit-ID: 8rWQQhaTRr8
toolkit/components/extensions/Schemas.jsm
--- a/toolkit/components/extensions/Schemas.jsm
+++ b/toolkit/components/extensions/Schemas.jsm
@@ -62,19 +62,55 @@ function readJSON(url) {
         resolve(JSON.parse(text));
       } catch (e) {
         reject(e);
       }
     });
   });
 }
 
+function stripDescriptions(json, stripThis = true) {
+  if (Array.isArray(json)) {
+    for (let i = 0; i < json.length; i++) {
+      if (typeof json[i] === "object" && json[i] !== null) {
+        json[i] = stripDescriptions(json[i]);
+      }
+    }
+    return json;
+  }
+
+  let result = {};
+
+  // Objects are handled much more efficiently, both in terms of memory and
+  // CPU, if they have the same shape as other objects that serve the same
+  // purpose. So, normalize the order of properties to increase the chances
+  // that the majority of schema objects wind up in large shape groups.
+  for (let key of Object.keys(json).sort()) {
+    if (stripThis && key === "description" && typeof json[key] === "string") {
+      continue;
+    }
+
+    if (typeof json[key] === "object" && json[key] !== null) {
+      result[key] = stripDescriptions(json[key], key !== "properties");
+    } else {
+      result[key] = json[key];
+    }
+  }
+
+  return result;
+}
+
 async function readJSONAndBlobbify(url) {
   let json = await readJSON(url);
 
+  // We don't actually use descriptions at runtime, and they make up about a
+  // third of the size of our structured clone data, so strip them before
+  // blobbifying.
+  json = stripDescriptions(json);
+
   return new StructuredCloneHolder(json);
 }
 
 /**
  * Defines a lazy getter for the given property on the given object. Any
  * security wrappers are waived on the object before the property is
  * defined, and the getter and setter methods are wrapped for the target
  * scope.