Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in compilation time due to native asset bundling #134427

Closed
zanderso opened this issue Sep 11, 2023 · 7 comments
Closed

Regression in compilation time due to native asset bundling #134427

zanderso opened this issue Sep 11, 2023 · 7 comments
Assignees
Labels
c: performance Relates to speed or footprint issues (see "perf:" labels) P1 High-priority issues at the top of the work list perf: speed Performance issues related to (mostly rendering) speed team-tool Owned by Flutter Tool team triaged-tool Triaged by Flutter Tool team

Comments

@zanderso
Copy link
Member

SkiaPerf is reporting a 0.5 - 1s regression in debug mode compilation time due to #130494.

https://flutter-flutter-perf.skia.org/e/?begin=1694274733&end=1694422824&keys=Xb9306954671fd63586c21f618c09ccd6&num_commits=50&request_type=1&xbaroffset=36775

I'm a bit concerned because the program being compiled for the benchmark doesn't use the new feature added by the PR (?)

cc @dcharkes @christopherfujino @stuartmorgan

@zanderso zanderso added c: performance Relates to speed or footprint issues (see "perf:" labels) perf: speed Performance issues related to (mostly rendering) speed team-tool Owned by Flutter Tool team labels Sep 11, 2023
@dcharkes
Copy link
Contributor

My first hunch is that it might be the file access for all dependencies to check if there's a build.dart in the package root.

Let me take a look.

@dcharkes
Copy link
Contributor

I observe a slowdown locally on flutter build ios --release --tree-shake-icons --split-debug-info=infos/ in the hello world example after a flutter clean && flutter pub get of ~400ms. The difference between the fastest runs (out of 5) before and after the commit is 400 ms. (The runs vary all the way from 16 to 21 seconds though.)

One thing I see in the logs is the new NativeAssets Target taking 200 milliseconds:

           [   +2 ms] native_assets: Starting due to {}
           [   +2 ms] Skipping target: gen_localizations
           [   +1 ms] gen_dart_plugin_registrant: Starting due to {InvalidatedReasonKind.inputChanged: The following inputs have updated contents: /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/package_config_subset}
           [  +33 ms] gen_dart_plugin_registrant: Complete
           [ +107 ms] release_unpack_ios: Complete
           [  +60 ms] Writing native_assets.yaml.
           [   +7 ms] Writing /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/flutter_build/be2692bbfbc0b9a27fcd2422d52354c6/native_assets.yaml done.
           [        ] native_assets: Complete

In these 200 milliseconds, it spends 150 ms loading/parsing the PackagesConfig. We should already have a packages config somewhere in the Flutter tools, so we should be using that instead of loading it from disk and reparsing it.

(The reason we do work at all with the new feature being disabled, is that we give an error message if any dependency has native assets, but the feature is not enabled.)

I'll (1) make a PR to not reparse the package config, and (2) see if I can find anything else which has been slowed down.

@dcharkes
Copy link
Contributor

The time spent in the native_assets target is not actually spent doing native assets stuff. It's in awaits while other targets do sync I/O. Targets doing sync I/O identified so far release_unpack_ios and gen_dart_plugin_registrant.

However, this does increase the latency before the kernel_snapshot target is started, leading to a slower build overall.

I have made #134523 which converts a some of sync file IO and process runs to to the non-sync ones. This changes the log to:

           [   +4 ms] native_assets: Starting due to {}
           [        ] Skipping target: gen_localizations
           [   +1 ms] gen_dart_plugin_registrant: Starting due to {InvalidatedReasonKind.inputChanged: The following inputs have updated contents: /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/package_config_subset}
           [  +31 ms] Writing native_assets.yaml.
           [   +8 ms] Writing /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/flutter_build/f9451a65a465bfab70d004e21d6cc1d6/native_assets.yaml done.
           [   +1 ms] native_assets: Complete

The question now is, should I/O be sync or async in the backend?
Since the backend processes targets in parallel, I believe using sync I/O in them is bad.
Who can I ask for some insight on the architecture in the backends? @zanderso @stuartmorgan @christopherfujino

@zanderso
Copy link
Member Author

Converting to async I/O sounds like the right choice as long as the Dart code isn't holding onto any instances of RandomAccessFile.

@dcharkes
Copy link
Contributor

dcharkes commented Sep 12, 2023

When the native assets feature is enabled, computing the order in which native assets should be built with dart pub deps --json takes 250 milliseconds.

$ pwd
/Users/dacoharkes/flt/engine/flutter/examples/hello_world
$ time dart pub deps --json
# ...
real	0m0.248s
user	0m0.241s
sys	0m0.069s

I can tweak the build process slightly, so that if we have <= 1 packages with native assets, we don't have to compute the topological ordering.

cc @jonasfj once this feature is used and we have >= 2 packages with native assets, we might not want to incur a 250 milliseconds latency.

@christopherfujino christopherfujino added P1 High-priority issues at the top of the work list triaged-tool Triaged by Flutter Tool team labels Sep 12, 2023
XilaiZhang pushed a commit that referenced this issue Sep 15, 2023
Speeds up the native assets target in the backend by

1. changing other targets `gen_dart_plugin_registrant` and
`release_unpack_ios` to do async I/O,
2. not reparsing the package config, and
3. not calling `dart pub deps --json` for 0 or 1 packages (fixed
package:native_assets_builder).

* #134427

```
           [   +2 ms] native_assets: Starting due to {}
           [   +2 ms] Skipping target: gen_localizations
           [   +1 ms] gen_dart_plugin_registrant: Starting due to {InvalidatedReasonKind.inputChanged: The following inputs have updated contents: /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/package_config_subset}
           [  +33 ms] gen_dart_plugin_registrant: Complete
           [ +107 ms] release_unpack_ios: Complete
           [  +60 ms] Writing native_assets.yaml.
           [   +7 ms] Writing /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/flutter_build/be2692bbfbc0b9a27fcd2422d52354c6/native_assets.yaml done.
           [        ] native_assets: Complete
```

->

```
           [   +4 ms] native_assets: Starting due to {}
           [        ] Skipping target: gen_localizations
           [   +1 ms] gen_dart_plugin_registrant: Starting due to {InvalidatedReasonKind.inputChanged: The following inputs have updated contents: /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/package_config_subset}
           [  +31 ms] Writing native_assets.yaml.
           [   +8 ms] Writing /Users/dacoharkes/flt/engine/flutter/examples/hello_world/.dart_tool/flutter_build/f9451a65a465bfab70d004e21d6cc1d6/native_assets.yaml done.
           [   +1 ms] native_assets: Complete
```

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.


<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
@github-actions
Copy link

github-actions bot commented Oct 2, 2023

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: performance Relates to speed or footprint issues (see "perf:" labels) P1 High-priority issues at the top of the work list perf: speed Performance issues related to (mostly rendering) speed team-tool Owned by Flutter Tool team triaged-tool Triaged by Flutter Tool team
Projects
None yet
Development

No branches or pull requests

3 participants