Optimize message formatting

RESOLVED FIXED

Status

RESOLVED FIXED
2 years ago
2 years ago

People

(Reporter: stas, Assigned: stas)

Tracking

Details

(Whiteboard: [gecko-l20n])

Attachments

(5 attachments)

(Assignee)

Description

2 years ago
formatEntities is an internal method used only in LocalizationObserver.getElementsTranslation.  As evidenced by bug 1289530 we spend quite a lot of time in getElementsTranslation.  I'd like to ry to optimize both methods perf-wise.
(Assignee)

Comment 1

2 years ago
I run a few tests using Zibi's l20n-perf-monitor [1].  At first I suspected the slowness to be related to this.iteractive in formatEntities or the destructuring assignment in getElementsTranslation's map() (which tends to be slow as of today).  I can now confirm that formatting takes up to 20 ms which is almost all of what getElementsTranslation takes. 

[1] https://github.com/zbraniecki/gecko-dev/blob/l20n-zibi/toolkit/content/l20n-perf-monitor.js
Not sure if it's relevant here, but I have seen things like map(), etc. perform slower than a traditional `for (var i < 0; i < length; i++) { ... }` loop, so it might be worth an attempt if you are in a hot code path.
(Assignee)

Comment 3

2 years ago
Thanks, :jryans!  I'll experiment
Assignee: nobody → stas
(Assignee)

Comment 4

2 years ago
(Accidental Submit.(

I'll experiment with regular for loops (and perhaps for-of) but so far it looks like the most expensive thing is iterating over the NodeList returned from querySelectorAll('[data-l10n-id]'), no matter how.
(Assignee)

Comment 5

2 years ago
Most of the perf problems are in format* functions or in the MessageContext class.

I found three major bottlenecks:

* destructuring assignment is slow in hotter loops,

* formatting traits bails out of the simple-string optimization because the entire trait object is passed to format() instead of its <code>val</code>,

* formatting entities with null values and no default traits is slow because we check each trait if it's a default one deep in the generator-enabled code of MessageContext;  we could make this check much earlier.
Summary: Optimize getElementsTranslation → Optimize message formatting
(Assignee)

Comment 6

2 years ago
Created attachment 8784500 [details] [review]
Pull request

This brings formatting (keysFromContext) from 20ms down to 9ms. I'll run a few more experiments before asking for review.
(Assignee)

Comment 8

2 years ago
Perfherder numbers have low confidence due to the small sample but they're consistent with what I'm seeing locally:

https://treeherder.mozilla.org/perf.html#/compare?originalProject=try&originalRevision=ccade2a11d17&newProject=try&newRevision=c57005f7a569&framework=1&filter=tpaint&showOnlyImportant=0

There seems to be a small improvement in tpaint numbers.  I think this is ready to land.
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)

Comment 13

2 years ago
mozreview-review
Comment on attachment 8785010 [details]
Bug 1296618 - Part 4: Remove destructuring of [value, errors] arrays.

https://reviewboard.mozilla.org/r/74318/#review72310
Attachment #8785010 - Flags: review?(gandalf) → review+

Comment 14

2 years ago
mozreview-review
Comment on attachment 8785009 [details]
Bug 1296618 - Part 3: Remove destructuring of [id, args] arrays.

https://reviewboard.mozilla.org/r/74316/#review72308
Attachment #8785009 - Flags: review?(gandalf) → review+

Comment 15

2 years ago
mozreview-review
Comment on attachment 8785008 [details]
Bug 1296618 - Part 2: Optimize entities with null values and no default traits.

https://reviewboard.mozilla.org/r/74314/#review72306
Attachment #8785008 - Flags: review?(gandalf) → review+

Comment 16

2 years ago
mozreview-review
Comment on attachment 8785007 [details]
Bug 1296618 - Part 1: Format trait.val directly.

https://reviewboard.mozilla.org/r/74312/#review72304
Attachment #8785007 - Flags: review?(gandalf) → review+
(Assignee)

Comment 18

2 years ago
https://hg.mozilla.org/projects/larch/rev/aa591eaf759c385e131bec79f0262976d195d209
Bug 1296618 - Part 1: Format trait.val directly. r=gandalf

https://hg.mozilla.org/projects/larch/rev/38d37db7b551c7ecfa286178001b42502b9bc3bc
Bug 1296618 - Part 2: Optimize entities with null values and no default traits. r=gandalf

https://hg.mozilla.org/projects/larch/rev/5d24f47b89d6d130a18d0b1a883c1e8c35c10f67
Bug 1296618 - Part 3: Remove destructuring of [id, args] arrays. r=gandalf

https://hg.mozilla.org/projects/larch/rev/468244986b20a0bdf22a5e36b55ae43ac09599c8
Bug 1296618 - Part 4: Remove destructuring of [value, errors] arrays. r=gandalf
(Assignee)

Updated

2 years ago
Status: NEW → RESOLVED
Last Resolved: 2 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.