Closed Bug 1672317 Opened 4 years ago Closed 3 years ago

Introduce L10nFileSource

Categories

(Core :: Internationalization: Localization, task)

task

Tracking

()

RESOLVED FIXED
92 Branch
Tracking Status
firefox92 --- fixed

People

(Reporter: zbraniecki, Assigned: zbraniecki)

References

Details

(Keywords: perf-alert)

Attachments

(10 files)

(deleted), text/x-phabricator-request
Details
(deleted), application/x-javascript
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details
(deleted), text/x-phabricator-request
Details

As part of bug 1660392 we need a more defined API for providing file I/O to L10nRegistry.

The way it is meant to work is that by default Gecko will generate FileSources and use the default FileFetcher for them, but you can override that by something like this:

let mock_ff = new FileFetcher(
  async (path) => { return "string"; },
  (path) => { return "string"; },
);

let fs1 = new FileSource(
 "mock_name",
 ["pl", "en-US"],
 "firefox-langpack-pl-en-US://localization/{locale}/",
 mock_ff)
);

and this way this FIleSource will use the mock_ff for all I/O operations. If the last argument is not provided, then the default Gecko FileFetcher is going to be used.

This will replace the current model in which people override L10nRegistry.load and L10nRegistry.loadSync for their purposes.

Actually, we can do a simpler trick without having to handle async functions back and forth:

let mock_fs = new L10nFileSource(["en-US"], "pre_path");
mock_fs.set_cache({
  "path1.ftl" : "key = Value"
});

let mockReg = new L10nRegistry();
mockReg.registerSource(mock_fs);

function generateBundles(resIds) {
  let myLocales = [];
  return mockReg.generateBundles(myLocales, resIds);
}

let loc = new Localization(, {generateBundles});
Summary: Introduce L10nFileFetcher → Introduce L10nFileSource

Or maybe even better, we could replace the generateBundles callback with some object that has { registry, locales[] }.

Assignee: nobody → zbraniecki
Status: NEW → ASSIGNED
Attached file filesource-test.js (deleted) —

This is a micro benchmark for JS vs Rust FileSource performance.

Initial results:

Variant JS Rust Diff
Sync 5.2ms 4.4ms -0.8ms
Async 10.6ms 7.7ms -2.9ms
Attachment #9199573 - Attachment description: Bug 1672317 - Update fluent-rs to 0.14.3. → Bug 1672317 - Update fluent-rs to 0.14.4.
Attachment #9199573 - Attachment description: Bug 1672317 - Update fluent-rs to 0.14.4. → Bug 1672317 - Update fluent-rs to 0.15.0.

Applied reviewers feedback. Current numbers:

Variant JS Rust
Sync 2.9ms 2.0ms
Async 11ms 6.7ms

And here are profiles created with sampling interval 0.1ms using the attached test with (true, true) for sync, and (true, false) for async:

Sync Profile:

Async Profile:

:nika, :emilio - I believe this part is mostly stable now. I still need to run a full try build, but it passes all intl specific tests and fits into the whole patchset!

I think the only remaining part that may have a bigger impact is whether the I/O, which is 90% of what this PR does, is handling sync/async properly.
I shared the profiles above, can you take a look at them and verify that it seems sane and doesn't have unnecessary allocations/cloning?

This is going to be on a hot path for the startup so would like to use this opportunity to make sure that the I/O here is as lean as possible.

The patchset also builds against m-c and you can run the attached test in browser console to get your own profiling with markers if you want to.

Flags: needinfo?(nika)
Flags: needinfo?(emilio)

I don't see anything too terrible in that profile, but I just took a quick look. malloc() shows up a bunch in the syntax parser and such, but that seems mostly unrelated to this PR. Could probably be optimized.

Flags: needinfo?(emilio)

I'm running try runs against this part to make sure that we can land it in isolation.

I noticed that there's a difference in async loadFile behavior between JSM FileSource and Rust FileSource, likely due to some combination of microtasks/tasks.

You can reproduce it by building just D103002 PR and executing the following in the browser console:

{
const jsmReg = ChromeUtils.import("resource://gre/modules/L10nRegistry.jsm");

const fs = new jsmReg.FileSource("test", ["en-US"], "resource://app/localization/{locale}/");
const fs2 = new L10nFileSource("test", ["en-US"], "resource://app/localization/{locale}/");

async function test(fs) {
  let counter = 0;
  
  async function resolve(fs) {
    await fs.fetchFile("en-US", "browser/preferences/preferences.ftl");
    console.log(`resolve ${counter++}`);
  }

  console.log("before");
  await resolve(fs);
  resolve(fs);
  await new Promise((resolve) => resolve());
  console.log("after");
}

async function run() {
  console.log(`--- START ---`);
  await test(fs);

  console.log(`--- BETWEEN ---`);


  await test(fs2);

  console.log(`--- END ---`);
}

run();
}

The JSM version will return:

before
resolve 0
resolve 1
after

The Rust version will return:

before
resolve 0
after
resolve 1

There's of course a number of tests that rely on the JSM behavior, and I'm happy to fix them so that they actually work with async as it is intended to be.
But my concern is that maybe it is an indication of some potential performance loss between JSM and Rust due to getting out of microtask and spinning a new task, and if both behaviors are "acceptable", but the JSM's one is cheaper/faster, it's worth trying to get the Rust handling of tasks closer to what JSM is doing?

Flags: needinfo?(emilio)

Except of the timing question and resulting test failures, the try is green! https://treeherder.mozilla.org/jobs?repo=try&revision=7ba982a1f23b30d544f2c3779fd30f343a8f9ad4

I'll wait for feedback on the microtask/task in case there is a potential for performance optimizations, and I'll keep the r? on Nika in hopes of potential guidance that I can apply to the follow up patches.

I think the tests should probably be fixed regardless, if they're relying on an async op to happen effectively sync.

But yeah I guess the way to make the behavior match would be to resolve immediately the promise in fetch_file, rather than going through moz_task. If that's easy to do, seems worth it?

Flags: needinfo?(emilio)

Excellent! Thank you! I was able to simplify the fetch_file quite a bit and improve performance of fetching source that is already cached by ~50% by skipping the micro task!
I also fixed the test!

I pushed the whole patchset to try: https://treeherder.mozilla.org/jobs?repo=try&revision=ad8a1f9942ca4f4a25cd817e60e5aca4fa0f025a

Flags: needinfo?(nika)
Attachment #9199573 - Attachment description: Bug 1672317 - Update fluent-rs to 0.15.0. → Bug 1672317 - [l10nfilesource] part1: Update fluent-rs to 0.15.0.
Attachment #9199574 - Attachment description: Bug 1672317 - Vendor in l10nregistry-rs and fluent_fallback. → Bug 1672317 - [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback.
Attachment #9199172 - Attachment description: Bug 1672317 - Introduce L10nFileSource backed by l10nregistry-rs. → Bug 1672317 - [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs.
Attachment #9199705 - Attachment description: Bug 1672317 - Remove JSM backed FileSource. → Bug 1672317 - [l10nfilesource] part5: Migrate tests to use L10nFileSource.

(In reply to Zibi Braniecki [:zbraniecki][:gandalf] from comment #8)

Applied reviewers feedback. Current numbers:

Variant JS Rust
Sync 2.9ms 2.0ms
Async 11ms 6.7ms

And here are profiles created with sampling interval 0.1ms using the attached test with (true, true) for sync, and (true, false) for async:

Sync Profile:

Async Profile:

:nika, :emilio - I believe this part is mostly stable now. I still need to run a full try build, but it passes all intl specific tests and fits into the whole patchset!

I think the only remaining part that may have a bigger impact is whether the I/O, which is 90% of what this PR does, is handling sync/async properly.
I shared the profiles above, can you take a look at them and verify that it seems sane and doesn't have unnecessary allocations/cloning?

This is going to be on a hot path for the startup so would like to use this opportunity to make sure that the I/O here is as lean as possible.

The patchset also builds against m-c and you can run the attached test in browser console to get your own profiling with markers if you want to.

I've took a quick look at the profiles. IMO, there is nothing to be concerned.
Please let me know if you need necko team to take a further look. Thanks.

Retesting against m-c:

Variant JS Rust
Sync 2.3 1.6
Async 4.1 4.7
Attachment #9199573 - Attachment description: Bug 1672317 - [l10nfilesource] part1: Update fluent-rs to 0.15.0. → WIP: Bug 1672317 - [l10nfilesource] part1: Update fluent-rs to 0.15.0.
Attachment #9199574 - Attachment description: Bug 1672317 - [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback. → WIP: Bug 1672317 - [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback.
Attachment #9199172 - Attachment description: Bug 1672317 - [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs. → WIP: Bug 1672317 - [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs.
Attachment #9203582 - Attachment description: Bug 1672317 - [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy. → WIP: Bug 1672317 - [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy.
Attachment #9199705 - Attachment description: Bug 1672317 - [l10nfilesource] part5: Migrate tests to use L10nFileSource. → WIP: Bug 1672317 - [l10nfilesource] part5: Migrate tests to use L10nFileSource.
Attachment #9203583 - Attachment description: Bug 1672317 - [l10nfilesource] part6: Migrate callsites to L10nFileSource. → WIP: Bug 1672317 - [l10nfilesource] part6: Migrate callsites to L10nFileSource.
Attachment #9203584 - Attachment description: Bug 1672317 - [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource. → WIP: Bug 1672317 - [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource.
Attachment #9203585 - Attachment description: Bug 1672317 - [l10nfilesource] part8: Remove JSM FileSource. → WIP: Bug 1672317 - [l10nfilesource] part8: Remove JSM FileSource.
Attachment #9199573 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part1: Update fluent-rs to 0.15.0. → Bug 1672317 - [l10nfilesource] part1: Update fluent-rs to 0.15.0.
Attachment #9199574 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback. → Bug 1672317 - [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback.
Attachment #9199172 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs. → Bug 1672317 - [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs.
Attachment #9203582 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy. → Bug 1672317 - [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy.
Attachment #9199705 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part5: Migrate tests to use L10nFileSource. → Bug 1672317 - [l10nfilesource] part5: Migrate tests to use L10nFileSource.
Attachment #9203583 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part6: Migrate callsites to L10nFileSource. → Bug 1672317 - [l10nfilesource] part6: Migrate callsites to L10nFileSource.
Attachment #9203584 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource. → Bug 1672317 - [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource.
Attachment #9203585 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part8: Remove JSM FileSource. → Bug 1672317 - [l10nfilesource] part8: Remove JSM FileSource.
Attachment #9199573 - Attachment description: Bug 1672317 - [l10nfilesource] part1: Update fluent-rs to 0.15.0. → WIP: Bug 1672317 - [l10nfilesource] part1: Update fluent-rs to 0.15.0.
Attachment #9199574 - Attachment description: Bug 1672317 - [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback. → WIP: Bug 1672317 - [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback.
Attachment #9199172 - Attachment description: Bug 1672317 - [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs. → WIP: Bug 1672317 - [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs.
Attachment #9203582 - Attachment description: Bug 1672317 - [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy. → WIP: Bug 1672317 - [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy.
Attachment #9199705 - Attachment description: Bug 1672317 - [l10nfilesource] part5: Migrate tests to use L10nFileSource. → WIP: Bug 1672317 - [l10nfilesource] part5: Migrate tests to use L10nFileSource.
Attachment #9203583 - Attachment description: Bug 1672317 - [l10nfilesource] part6: Migrate callsites to L10nFileSource. → WIP: Bug 1672317 - [l10nfilesource] part6: Migrate callsites to L10nFileSource.
Attachment #9203584 - Attachment description: Bug 1672317 - [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource. → WIP: Bug 1672317 - [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource.
Attachment #9203585 - Attachment description: Bug 1672317 - [l10nfilesource] part8: Remove JSM FileSource. → WIP: Bug 1672317 - [l10nfilesource] part8: Remove JSM FileSource.
Attachment #9199573 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part1: Update fluent-rs to 0.15.0. → Bug 1672317 - [l10nfilesource] part1: Update fluent-rs to 0.15.0.
Attachment #9199574 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback. → Bug 1672317 - [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback.
Attachment #9199172 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs. → Bug 1672317 - [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs.
Attachment #9203582 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy. → Bug 1672317 - [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy.
Attachment #9199705 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part5: Migrate tests to use L10nFileSource. → Bug 1672317 - [l10nfilesource] part5: Migrate tests to use L10nFileSource.
Attachment #9203583 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part6: Migrate callsites to L10nFileSource. → Bug 1672317 - [l10nfilesource] part6: Migrate callsites to L10nFileSource.
Attachment #9203584 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource. → Bug 1672317 - [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource.
Attachment #9203585 - Attachment description: WIP: Bug 1672317 - [l10nfilesource] part8: Remove JSM FileSource. → Bug 1672317 - [l10nfilesource] part8: Remove JSM FileSource.

All oranges seems to be known intermittents.

Updated try: https://treeherder.mozilla.org/jobs?repo=try&revision=a8f60f13b1aefbb3aa86b925404b2242d72105b4

I consider this patchset ready to land and plan to land today.

Pushed by zbraniecki@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/944805ef4561 [l10nfilesource] part1: Update fluent-rs to 0.15.0. r=dminor https://hg.mozilla.org/integration/autoland/rev/d850981cf33c [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback. r=dminor https://hg.mozilla.org/integration/autoland/rev/1aaa22cf7c13 [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs. r=nika,emilio https://hg.mozilla.org/integration/autoland/rev/3e84f9a521f4 [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy. r=preferences-reviewers,Gijs https://hg.mozilla.org/integration/autoland/rev/b78f3e332d57 [l10nfilesource] part5: Migrate tests to use L10nFileSource. r=platform-i18n-reviewers,gregtatum https://hg.mozilla.org/integration/autoland/rev/a5d67460e7bb [l10nfilesource] part6: Migrate callsites to L10nFileSource. r=platform-i18n-reviewers,gregtatum https://hg.mozilla.org/integration/autoland/rev/cce54c20ec3c [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource. r=platform-i18n-reviewers,nordzilla https://hg.mozilla.org/integration/autoland/rev/8367ff19d3ba [l10nfilesource] part8: Remove JSM FileSource. r=platform-i18n-reviewers,nordzilla
Pushed by zbraniecki@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/13ac04e00f52 [l10nfilesource] part1: Update fluent-rs to 0.15.0. r=dminor https://hg.mozilla.org/integration/autoland/rev/efeead8f81e5 [l10nfilesource] part2: Vendor in l10nregistry-rs and fluent-fallback. r=dminor https://hg.mozilla.org/integration/autoland/rev/9435cf572c9a [l10nfilesource] part3: Introduce L10nFileSource backed by l10nregistry-rs. r=nika,emilio https://hg.mozilla.org/integration/autoland/rev/894fcec565df [l10nfilesource] part4: Fix preferences browser_languages_subdialog.js text to be not racy. r=preferences-reviewers,Gijs https://hg.mozilla.org/integration/autoland/rev/5bb93c95e982 [l10nfilesource] part5: Migrate tests to use L10nFileSource. r=platform-i18n-reviewers,gregtatum https://hg.mozilla.org/integration/autoland/rev/532d547483d5 [l10nfilesource] part6: Migrate callsites to L10nFileSource. r=platform-i18n-reviewers,gregtatum https://hg.mozilla.org/integration/autoland/rev/59dde867351d [l10nfilesource] part7: Migrate L10nRegistry to use L10nFileSource. r=platform-i18n-reviewers,nordzilla https://hg.mozilla.org/integration/autoland/rev/0af27e343a57 [l10nfilesource] part8: Remove JSM FileSource. r=platform-i18n-reviewers,nordzilla
Regressions: 1723044
Pushed by geoff@darktrojan.net: https://hg.mozilla.org/comm-central/rev/36f310410d8c Port bug 1672317 - Migrate calendar to use L10nFileSource. rs=bustage-fix
Attachment #9233935 - Attachment description: WIP: Bug 1672317 - Follow-up: Add missing Cargo.lock update. → Bug 1672317 - Follow-up: Add missing Cargo.lock update.
Pushed by malexandru@mozilla.com: https://hg.mozilla.org/mozilla-central/rev/86517dddee70 Follow-up: Add missing Cargo.lock update. a=fix
Regressions: 1723688

(In reply to Pulsebot from comment #27)

Pushed by zbraniecki@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/0af27e343a57
[l10nfilesource] part8: Remove JSM FileSource.
r=platform-i18n-reviewers,nordzilla

== Change summary for alert #30776 (as of Wed, 04 Aug 2021 10:41:54 GMT) ==

Regressions:

Ratio Suite Test Platform Options Absolute values (old vs new)
3% welcome ContentfulSpeedIndex linux1804-64-shippable-qr cold webrender 1,608.38 -> 1,654.00
2% welcome ContentfulSpeedIndex linux1804-64-shippable-qr cold webrender 1,617.75 -> 1,644.17

Improvements:

Ratio Suite Test Platform Options Absolute values (old vs new)
17% welcome loadtime linux1804-64-shippable-qr cold webrender 93.17 -> 77.33
17% welcome fnbpaint linux1804-64-shippable-qr cold webrender 96.33 -> 80.29

For up to date results, see: https://treeherder.mozilla.org/perfherder/alerts?id=30776

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: