Bug 1296618 - Part 2: Optimize entities with null values and no default traits. r?gandalf
MozReview-Commit-ID: 9CP9lFmz5h0
--- 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);