firebase / flutterfire

šŸ”„ A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.57k stars 3.94k forks source link

šŸ› [firebase_core] Firebase.initializeApp() slows down the app startup without using CPU #8837

Closed dvoloshyn closed 1 year ago

dvoloshyn commented 2 years ago

Bug report

await Firebase.initializeApp() takes significant time drastically slowing down the app startup while not being CPU bound. All the examples on the Internet are running this in the main() function so it's expected to be fast. Google Play Pre-launch report warns about slow startup on low end devices (in my case it took 7.5 seconds till first frame). If there are some tricks that could reduce this time they should be clearly documented. (I do not consider async init after first frame a viable option as it would greatly complicate the app structure).

Steps to reproduce

  1. Run the following code on a real android device: flutter run -d 47f367ff --trace-startup --profile
    Click To Expand
final _task = TimelineTask(filterKey: "main");

void main() async {
  _task.start("Trigger async Firebase init");
  WidgetsFlutterBinding.ensureInitialized();
  final opts = DefaultFirebaseOptions.currentPlatform;
  final appFuture = Firebase.initializeApp(options: opts);
  _task.finish();

  _task.start("Await Firebase init");
  final app = await appFuture;
  log(app.name);
  _task.finish();

  _task.start("Trigger other Firebase-independent async inits");
  MobileAds.instance.initialize();
  _task.finish();

  _task.start("runApp");
  runApp(
    const MaterialApp(home: Scaffold(body: Center(child: Text("HelloWorld")))),
  );
  _task.finish();
}

  1. Review the startup trace: start_up_timeline.txt
  2. Absolute values are small because I'm testing on a high end device. Notice that firebase init takes more than half of the initialization time (127 ms out of total 226 ms). Also notice that it's not CPU bound.
  3. Run the following code on a real android device (swap firebase and admob init, this way I tried to do something else useful while firebase was initializing): flutter run -d 47f367ff --trace-startup --profile
    Click To Expand
final _task = TimelineTask(filterKey: "main");

void main() async {
  _task.start("Trigger async Firebase init");
  WidgetsFlutterBinding.ensureInitialized();
  final opts = DefaultFirebaseOptions.currentPlatform;
  final appFuture = Firebase.initializeApp(options: opts);
  _task.finish();

  _task.start("Trigger other Firebase-independent async inits");
  MobileAds.instance.initialize();
  _task.finish();

  _task.start("Await Firebase init");
  final app = await appFuture;
  log(app.name);
  _task.finish();

  _task.start("runApp");
  runApp(
    const MaterialApp(home: Scaffold(body: Center(child: Text("HelloWorld")))),
  );
  _task.finish();
}

  1. Review the startup trace: start_up_timeline.txt
  2. Notice that startup time grew more than 50% (very weird) and now firebase init takes even more time (264 ms out of total 370 ms). And it's also not CPU bound.

Expected behavior

Firebase.initializeApp() should:

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` Doctor summary (to see all details, run flutter doctor -v): [āœ“] Flutter (Channel stable, 3.0.1, on Manjaro Linux 5.17.9-1-MANJARO, locale en_US.UTF-8) [āœ“] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) [āœ“] Chrome - develop for the web [āœ“] Linux toolchain - develop for Linux desktop [āœ“] Android Studio (version 2021.1) [āœ“] Android Studio (version 2021.2) [āœ“] IntelliJ IDEA Ultimate Edition (version 2022.1) [āœ“] Connected device (3 available) [āœ“] HTTP Host Availability ā€¢ No issues found! ```

Flutter dependencies

Click To Expand ``` Dart SDK 2.17.1 Flutter SDK 3.0.1 dependencies: - badges 2.0.2 [flutter] - cloud_firestore 3.1.17 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - cupertino_icons 1.0.4 - firebase_analytics 9.1.9 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter] - firebase_app_check 0.0.6+13 [firebase_app_check_platform_interface firebase_app_check_web firebase_core firebase_core_platform_interface flutter] - firebase_app_installations 0.1.0+14 [firebase_app_installations_platform_interface firebase_app_installations_web firebase_core firebase_core_platform_interface flutter] - firebase_auth 3.3.19 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 1.17.1 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 2.8.1 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_dynamic_links 4.2.5 [firebase_core firebase_core_platform_interface firebase_dynamic_links_platform_interface flutter meta plugin_platform_interface] - firebase_in_app_messaging 0.6.0+15 [firebase_core firebase_core_platform_interface firebase_in_app_messaging_platform_interface flutter meta] - firebase_performance 0.8.0+13 [firebase_core firebase_core_platform_interface firebase_performance_platform_interface firebase_performance_web flutter] - firebase_remote_config 2.0.8 [firebase_core firebase_core_platform_interface firebase_remote_config_platform_interface firebase_remote_config_web flutter] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] - flutter_contacts 1.1.4 [flutter] - flutter_riverpod 2.0.0-dev.9 [collection flutter meta riverpod state_notifier] - flutterfire_ui 0.4.1+2 [cloud_firestore collection crypto desktop_webview_auth email_validator firebase_auth firebase_core firebase_database firebase_dynamic_links flutter flutter_facebook_auth flutter_localizations flutter_svg google_sign_in sign_in_with_apple twitter_login] - go_router 3.1.1 [collection flutter flutter_web_plugins logging meta] - google_mobile_ads 1.2.0 [meta flutter] - infinite_scroll_pagination 3.2.0 [flutter sliver_tools] - logging 1.0.2 - package_info_plus 1.4.2 [flutter package_info_plus_platform_interface package_info_plus_linux package_info_plus_macos package_info_plus_windows package_info_plus_web] - share_plus 4.0.5 [meta mime flutter share_plus_platform_interface share_plus_linux share_plus_macos share_plus_windows share_plus_web] - url_launcher 6.1.2 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] dev dependencies: - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph] - icons_launcher 1.1.9 [args image path universal_io yaml] - integration_test 0.0.0 [flutter flutter_driver flutter_test path vm_service archive async boolean_selector characters charcode clock collection crypto fake_async file matcher material_color_utilities meta source_span stack_trace stream_channel string_scanner sync_http term_glyph test_api typed_data vector_math webdriver] - lint 1.8.2 transitive dependencies: - archive 3.1.11 [crypto path] - args 2.3.1 - async 2.8.2 [collection meta] - boolean_selector 2.1.0 [source_span string_scanner] - characters 1.2.0 - charcode 1.3.1 - clock 1.1.0 - cloud_firestore_platform_interface 5.5.7 [collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 2.6.16 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js] - collection 1.16.0 - crypto 3.0.1 [collection typed_data] - desktop_webview_auth 0.0.9 [crypto flutter http] - email_validator 2.0.1 - fake_async 1.3.0 [clock collection] - ffi 1.2.1 - file 6.1.2 [meta path] - firebase 9.0.3 [http http_parser js] - firebase_analytics_platform_interface 3.1.7 [firebase_core flutter meta plugin_platform_interface] - firebase_analytics_web 0.4.0+14 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - firebase_app_check_platform_interface 0.0.4+7 [firebase_core flutter meta plugin_platform_interface] - firebase_app_check_web 0.0.5+13 [firebase_app_check_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - firebase_app_installations_platform_interface 0.1.1+7 [firebase_core flutter meta plugin_platform_interface] - firebase_app_installations_web 0.1.0+14 [firebase_app_installations_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - firebase_auth_platform_interface 6.2.7 [firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 3.3.16 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.4.0 [collection flutter meta plugin_platform_interface] - firebase_core_web 1.6.4 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_crashlytics_platform_interface 3.2.7 [collection firebase_core flutter meta plugin_platform_interface] - firebase_database 9.0.15 [firebase_core firebase_core_platform_interface firebase_database_platform_interface firebase_database_web flutter] - firebase_database_platform_interface 0.2.1+7 [collection firebase_core flutter meta plugin_platform_interface] - firebase_database_web 0.2.0+14 [firebase_core firebase_core_web firebase_database_platform_interface flutter flutter_web_plugins js] - firebase_dynamic_links_platform_interface 0.2.3+3 [firebase_core flutter meta plugin_platform_interface] - firebase_in_app_messaging_platform_interface 0.2.1+7 [firebase_core flutter meta plugin_platform_interface] - firebase_performance_platform_interface 0.1.1+7 [firebase_core flutter plugin_platform_interface] - firebase_performance_web 0.1.0+13 [firebase firebase_core firebase_core_web firebase_performance_platform_interface flutter flutter_web_plugins js] - firebase_remote_config_platform_interface 1.1.7 [firebase_core flutter meta plugin_platform_interface] - firebase_remote_config_web 1.0.13 [firebase_core firebase_core_web firebase_remote_config_platform_interface flutter flutter_web_plugins js] - flutter_driver 0.0.0 [file flutter flutter_test fuchsia_remote_debug_protocol path meta vm_service webdriver archive async boolean_selector characters charcode clock collection crypto matcher material_color_utilities platform process source_span stack_trace stream_channel string_scanner sync_http term_glyph test_api typed_data vector_math] - flutter_facebook_auth 4.3.4 [flutter flutter_facebook_auth_platform_interface flutter_facebook_auth_web] - flutter_facebook_auth_platform_interface 3.1.2 [flutter plugin_platform_interface] - flutter_facebook_auth_web 3.1.2 [flutter flutter_web_plugins js flutter_facebook_auth_platform_interface] - flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math] - flutter_svg 1.1.0 [flutter meta path_drawing vector_math xml] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math] - fuchsia_remote_debug_protocol 0.0.0 [process vm_service file meta path platform] - google_sign_in 5.3.2 [flutter google_sign_in_android google_sign_in_ios google_sign_in_platform_interface google_sign_in_web] - google_sign_in_android 5.2.8 [flutter google_sign_in_platform_interface] - google_sign_in_ios 5.3.1 [flutter google_sign_in_platform_interface] - google_sign_in_platform_interface 2.1.3 [flutter quiver] - google_sign_in_web 0.10.1+2 [flutter flutter_web_plugins google_sign_in_platform_interface js] - http 0.13.4 [async http_parser meta path] - http_parser 4.0.1 [collection source_span string_scanner typed_data] - image 3.2.0 [archive meta xml] - intl 0.17.0 [clock path] - js 0.6.4 - matcher 0.12.11 [stack_trace] - material_color_utilities 0.1.4 - meta 1.7.0 - mime 1.0.2 - package_info_plus_linux 1.0.5 [package_info_plus_platform_interface flutter path] - package_info_plus_macos 1.3.0 [flutter] - package_info_plus_platform_interface 1.0.2 [flutter meta plugin_platform_interface] - package_info_plus_web 1.0.5 [flutter flutter_web_plugins http meta package_info_plus_platform_interface] - package_info_plus_windows 1.0.5 [package_info_plus_platform_interface ffi flutter win32] - path 1.8.1 - path_drawing 1.0.0 [vector_math meta path_parsing flutter] - path_parsing 1.0.0 [vector_math meta] - petitparser 5.0.0 [meta] - platform 3.1.0 - plugin_platform_interface 2.1.2 [meta] - process 4.2.4 [file path platform] - quiver 3.1.0 [matcher] - riverpod 2.0.0-dev.9 [collection meta stack_trace state_notifier] - share_plus_linux 3.0.0 [share_plus_platform_interface file flutter meta url_launcher] - share_plus_macos 3.0.1 [share_plus_platform_interface flutter] - share_plus_platform_interface 3.0.3 [flutter meta mime plugin_platform_interface] - share_plus_web 3.0.1 [share_plus_platform_interface url_launcher flutter flutter_web_plugins meta] - share_plus_windows 3.0.1 [share_plus_platform_interface flutter meta url_launcher] - sign_in_with_apple 3.3.0 [flutter meta sign_in_with_apple_platform_interface sign_in_with_apple_web] - sign_in_with_apple_platform_interface 1.0.0 [flutter plugin_platform_interface meta] - sign_in_with_apple_web 1.0.1 [flutter flutter_web_plugins sign_in_with_apple_platform_interface js] - sky_engine 0.0.99 - sliver_tools 0.2.6 [flutter] - source_span 1.8.2 [collection path term_glyph] - stack_trace 1.10.0 [path] - state_notifier 0.7.2+1 [meta] - stream_channel 2.1.0 [async] - string_scanner 1.1.0 [charcode source_span] - sync_http 0.3.0 - term_glyph 1.2.0 - test_api 0.4.9 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher] - twitter_login 4.2.3 [flutter crypto http] - typed_data 1.3.0 [collection] - universal_io 2.0.4 [collection crypto meta typed_data] - url_launcher_android 6.0.17 [flutter url_launcher_platform_interface] - url_launcher_ios 6.0.17 [flutter url_launcher_platform_interface] - url_launcher_linux 3.0.1 [flutter url_launcher_platform_interface] - url_launcher_macos 3.0.1 [flutter url_launcher_platform_interface] - url_launcher_platform_interface 2.0.5 [flutter plugin_platform_interface] - url_launcher_web 2.0.11 [flutter flutter_web_plugins url_launcher_platform_interface] - url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface] - vector_math 2.1.2 - vm_service 8.2.2 - webdriver 3.0.0 [archive matcher path stack_trace sync_http] - win32 2.6.1 [ffi] - xml 6.1.0 [collection meta petitparser] - yaml 3.1.1 [collection source_span string_scanner] ```

darshankawar commented 2 years ago

@dvoloshyn Thanks for the detailed report. Is the performance slower on lower end devices as compared to high end ones ? Or is it similar ?

Also, does your app use splash screen ? If so, can you maybe try to confirm if it is implemented properly and that isn't causing the app startup ?

dvoloshyn commented 2 years ago

Performance is much lower on low end devices. Google Play reported 7.5 seconds startup on Nokia 1. Don't have a low end physical device to test myself.

According to the startup trace I attached it is clearly waiting for Firebase.initializeApp(). I use native android splash screen. Nothing custom:

<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>
</layer-list>
darshankawar commented 2 years ago

Thanks for the update. I also don't have a lower version Android device to verify this. Do you have details on range of Android devices it is affecting ? Is it specific to Nokia only or other makes as well ?

dvoloshyn commented 2 years ago

Here is the Google Play pre-launch report I received (cold start time in milliseconds):

Nokia Nokia 1 - 7.67K ms
xiaomi Redmi 6A - 2.73K ms
samsung Galaxy S9 - 1.05K ms
google Pixel 5 - 730 ms
samsung Galaxy S20 5G - 667 ms
darshankawar commented 2 years ago

Thanks for the quick update. I am keeping issue open and labeling for further insights from the team.

/cc @russellwheatley

dvoloshyn commented 2 years ago

I got hold of a low end device SM-G7102 with Android 4.4.2. Time to first frame was 1662 ms. Firebase.initializeApp() took 1115 ms. That is 67% of the whole application startup. Attached the start_up_timeline.txt Is it expected?

gibona commented 1 year ago

We have monitored similar behavior in OfficeSuite app which is using pure firebase SDK without flutter.

russellwheatley commented 1 year ago

As @gibona touched on, this is likely just the Firebase android SDKs start up time. I would encourage you to create a barebones android app and call the equivalent of Firebase.initializeApp() on android and record start up time. I can't see what FlutterFire can do to speed it up either way. Please share your findings here if you get the chance.

google-oss-bot commented 1 year ago

Hey @dvoloshyn. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot commented 1 year ago

Since there haven't been any recent updates here, I am going to close this issue.

@dvoloshyn if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.