Bug 1296618 - Part 2: Optimize entities with null values and no default traits. r?gandalf draft
authorStaś Małolepszy <stas@mozilla.com>
Thu, 25 Aug 2016 17:41:33 +0200
changeset 405645 2632e11d4b5b9993fed07c4ee58858b5f81304bc
parent 405644 b8d1b89a3f6f9663e71c1e77387720b9e1b68ffd
child 405646 73eec1e64bc48d1685969888f13a06529fa0506f
push id27552
push usersmalolepszy@mozilla.com
push dateThu, 25 Aug 2016 21:48:38 +0000
reviewersgandalf
bugs1296618
milestone51.0a1
Bug 1296618 - Part 2: Optimize entities with null values and no default traits. r?gandalf MozReview-Commit-ID: 9CP9lFmz5h0
toolkit/modules/IntlMessageContext.jsm
--- a/toolkit/modules/IntlMessageContext.jsm
+++ b/toolkit/modules/IntlMessageContext.jsm
@@ -1165,22 +1165,16 @@ function* toFTLType(entity, opts) {
   return yield* Entity(entity, opts.allowNoDefault);
 }
 
 const _opts = {
   allowNoDefault: false
 };
 
 function format(ctx, args, entity, opts = _opts) {
-  // optimization: many translations are simple strings and we can very easily
-  // avoid the cost of a proper resolution by having this shortcut here
-  if (typeof entity === 'string') {
-    return [entity, []];
-  }
-
   return resolve(toFTLType(entity, opts)).run({
     ctx, args, dirty: new WeakSet()
   });
 }
 
 const optsPrimitive = { allowNoDefault: true };
 
 class MessageContext {
@@ -1197,23 +1191,43 @@ class MessageContext {
       this.messages.set(id, entries[id]);
     }
 
     return errors;
   }
 
   // format `entity` to a string or null
   formatToPrimitive(entity, args) {
+    // optimize entities which are simple strings by skipping resultion
+    if (typeof entity === 'string') {
+      return [entity, []];
+    }
+
+    // optimize entities with null values and no default traits
+    if (!entity.val && entity.traits && !(entity.traits.some(t => t.def))) {
+      return [null, []];
+    }
+
     const result = format(this, args, entity, optsPrimitive);
     return (result[0] instanceof FTLNone) ?
       [null, result[1]] : result;
   }
 
   // format `entity` to a string
   format(entity, args) {
+    // optimize entities which are simple strings by skipping resultion
+    if (typeof entity === 'string') {
+      return [entity, []];
+    }
+
+    // optimize entities with null values and no default traits
+    if (!entity.val && entity.traits && !(entity.traits.some(t => t.def))) {
+      return [null, []];
+    }
+
     const result = format(this, args, entity);
     return [result[0].toString(), result[1]];
   }
 
   _memoizeIntlObject(ctor, opts) {
     const cache = this.intls.get(ctor) || {};
     const id = JSON.stringify(opts);