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

Increase threshold for usage of compute for utf8 decoding on large strings to 50 KB #64350

Merged
merged 6 commits into from
Aug 21, 2020

Conversation

jonahwilliams
Copy link
Member

@jonahwilliams jonahwilliams commented Aug 21, 2020

Description

#36552

On a local benchmark, removing compute is faster than compute in both debug and release mode on a Pixel 4. Looking for more confirmation on other older phone types.

Debug:
314 ms load/parse asset manifest with compute
91 ms load/parse without compute

Release:
14 ms load/parse with compute
2 ms load/parse without compute

I measured perf by inserting a stopwatch/print:

diff --git a/packages/flutter/lib/src/services/asset_bundle.dart b/packages/flutter/lib/src/services/asset_bundle.dart
index 0e8bb02874..f0028a0103 100644
--- a/packages/flutter/lib/src/services/asset_bundle.dart
+++ b/packages/flutter/lib/src/services/asset_bundle.dart
@@ -178,6 +178,7 @@ abstract class CachingAssetBundle extends AssetBundle {
       return _structuredDataCache[key] as Future<T>;
     Completer<T>? completer;
     Future<T>? result;
+    var sw = Stopwatch()..start();
     loadString(key, cache: false).then<T>(parser).then<void>((T value) {
       result = SynchronousFuture<T>(value);
       _structuredDataCache[key] = result!;
@@ -197,6 +198,9 @@ abstract class CachingAssetBundle extends AssetBundle {
     // completer for it to use when it does run.
     completer = Completer<T>();
     _structuredDataCache[key] = completer.future;
+    completer.future.whenComplete(() {
+      print('LoadStructuredDataTook: ${sw.elapsedMilliseconds}');
+    });
     return completer.future;
   }

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@flutter-dashboard flutter-dashboard bot added the framework flutter/packages/flutter repository. See also f: labels. label Aug 21, 2020
@jonahwilliams
Copy link
Member Author

That stopwatch is actually measuring UTF8 decode and JSON parse, but only UTF8 decode ran on an isolate. Locally it takes about 100 microseconds on 18 KB (the size of the flutter gallery asset manifest)

@jonahwilliams jonahwilliams changed the title Remove usage of compute for utf8 decoding on large strings Increase threshold for usage of compute for utf8 decoding on large strings Aug 21, 2020
if (data.lengthInBytes < 10 * 1024) {
// 10KB takes about 3ms to parse on a Pixel 2 XL.
// See: https://github.com/dart-lang/sdk/issues/31954
if (data.lengthInBytes < 50 * 1024) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps add a comment regarding "50 * 1024" number?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jonahwilliams jonahwilliams changed the title Increase threshold for usage of compute for utf8 decoding on large strings Increase threshold for usage of compute for utf8 decoding on large strings to 50 KB Aug 21, 2020
@chingjun
Copy link
Contributor

While this change LGTM, I wouldn't say that it fixes #36552, it only delayed the issue until AssetManifest.json is larger than 50KB.

Since AssetManifest is only loaded once in the app lifetime, what if we could load AssetManifest without using compute regardless of size?

@jonahwilliams
Copy link
Member Author

Yeah, I agree this doesn't completely fix the issue. I'm not sure about always skipping compute because I could imagine the asset manifest causing a significant amount of jank if it were truly that big - but mostly due to JSON parsing.

Another approach would be to revisit running code-generation to create a const map of the asset manifest

@jonahwilliams jonahwilliams merged commit f07f412 into flutter:master Aug 21, 2020
@jonahwilliams jonahwilliams deleted the remove_compute branch August 21, 2020 21:34
jonahwilliams added a commit that referenced this pull request Aug 22, 2020
jonahwilliams added a commit that referenced this pull request Aug 22, 2020
smadey pushed a commit to smadey/flutter that referenced this pull request Aug 27, 2020
…rings to 50 KB (flutter#64350)

On a local benchmark, removing compute is faster than compute in both debug and release mode on a Pixel 4. On a MotoG4, a much larger text sample (800 Kb) takes 50 ms, so we cannot simple remove the limit. Increase to 50 Kb which should take at most a ms or two on an older device, and only microseconds on newerones.
smadey pushed a commit to smadey/flutter that referenced this pull request Aug 27, 2020
mingwandroid pushed a commit to mingwandroid/flutter that referenced this pull request Sep 6, 2020
…rings to 50 KB (flutter#64350)

On a local benchmark, removing compute is faster than compute in both debug and release mode on a Pixel 4. On a MotoG4, a much larger text sample (800 Kb) takes 50 ms, so we cannot simple remove the limit. Increase to 50 Kb which should take at most a ms or two on an older device, and only microseconds on newerones.
mingwandroid pushed a commit to mingwandroid/flutter that referenced this pull request Sep 6, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants