Skip to content

[shared_preferences] Defer SharedPreferences fetch in LegacySharedPreferencesPlugin until first use on Android - #13431

Merged
auto-submit[bot] merged 1 commit into
flutter:mainfrom
xRossKoh:defer-legacy-shared-preferences-init
Oct 27, 2026
Merged

auto-submit[bot] merged 1 commit into
flutter:mainfrom
xRossKoh:defer-legacy-shared-preferences-init

Conversation

@xRossKoh

Copy link
Copy Markdown
Contributor

Defers getSharedPreferences in LegacySharedPreferencesPlugin out of onAttachedToEngine until first use, avoiding synchronous disk I/O on the Android main thread during startup.

The following screenshot shows that the time taken for RegisterFlutterPlugin significantly decreases with the change, which validates the fix:
shared_preferences_registration_comparison_perfetto

I also previously implemented this in my app, which was later negated by an upgrade of the shared_preferences packages into g3 and caused cold startup latency regression alerts on my app. Hoping to upstream the fix to get this optimization to all Flutter users.

flutter/flutter#195313 will be fixed by this PR.

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request defers the initialization of SharedPreferences in LegacySharedPreferencesPlugin until its first use to avoid main-thread disk I/O during plugin registration, and adds a corresponding unit test to verify this behavior. Feedback suggests using Kotlin's idiomatic by lazy delegate for thread-safe lazy initialization instead of manual double-checked locking.

@xRossKoh
xRossKoh force-pushed the defer-legacy-shared-preferences-init branch 2 times, most recently from bb0e061 to b9277f6 Compare October 26, 2026 07:04
@xRossKoh xRossKoh changed the title [shared_preferences] Defer getSharedPreferences in LegacySharedPreferencesPlugin until first use on Android [shared_preferences] Defer SharedPreferences fetch in LegacySharedPreferencesPlugin until first use on Android Oct 26, 2026
@xRossKoh
xRossKoh force-pushed the defer-legacy-shared-preferences-init branch 3 times, most recently from fc41867 to 61f8f9e Compare October 26, 2026 07:23
@jiahaog jiahaog added the CICD Run CI/CD label Oct 26, 2026
@xRossKoh
xRossKoh force-pushed the defer-legacy-shared-preferences-init branch from 61f8f9e to aaa3561 Compare October 27, 2026 02:30
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 27, 2026
@xRossKoh
xRossKoh force-pushed the defer-legacy-shared-preferences-init branch from aaa3561 to ed783fd Compare October 27, 2026 03:29
@jiahaog jiahaog added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Oct 27, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 27, 2026
@auto-submit

auto-submit Bot commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/packages/13431, because This PR has not met approval requirements for merging. The PR author is not a member of flutter-hackers and needs 1 more review(s) in order to merge this PR.

  • Merge guidelines: A PR needs at least one approved review if the author is already part of flutter-hackers or two member reviews if the author is not a member of flutter-hackers before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@auto-submit

auto-submit Bot commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/packages/13431, because - The status or check suite Dashboard Checks has failed. Please fix the issues identified (or deflake) before re-applying this label.

@jiahaog jiahaog added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 27, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 27, 2026
@auto-submit

auto-submit Bot commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/packages/13431, because - The status or check suite Dashboard Checks has failed. Please fix the issues identified (or deflake) before re-applying this label.

…gacySharedPreferencesPlugin` executing synchronous disk I/O on Android main thread during startup

Fixes eager SharedPreferences fetch in `LegacySharedPreferencesPlugin` executing synchronous disk I/O on Android main thread during startup by deferring the fetch until first use.

Fixes flutter/flutter#195313
@xRossKoh
xRossKoh force-pushed the defer-legacy-shared-preferences-init branch from ed783fd to 2574aeb Compare October 27, 2026 06:00
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 27, 2026
@jiahaog jiahaog added autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD labels Oct 27, 2026
@auto-submit
auto-submit Bot merged commit 587ee23 into flutter:main Oct 27, 2026
13 checks passed
@xRossKoh
xRossKoh deleted the defer-legacy-shared-preferences-init branch October 27, 2026 06:20
GhagSagar23 pushed a commit to Kryonex-Labs/flutter that referenced this pull request Oct 27, 2026
flutter/packages@23a05b6...a8549ae

2026-10-27 [email protected] [material_ui]
Deprecate the black/white parameters in `Typography.material2021`
(flutter/packages#13443)
2026-10-27 [email protected] [shared_preferences] Defer
SharedPreferences fetch in `LegacySharedPreferencesPlugin` until first
use on Android (flutter/packages#13431)
2026-10-27 [email protected] [material_ui] Fix MenuAnchor
non-Unicode shortcut labels on JS web (flutter/packages#13370)
2026-10-27 [email protected] [material_ui]
Add Checkbox.markInsets (flutter/packages#12690)
2026-10-27 [email protected]
[vector_graphics_compiler] Handle coordinate lists on text and tspan
(flutter/packages#13287)
2026-10-27 [email protected]
[cupertino_ui] Unskip web tests that pass on CanvasKit and Skwasm:
nav_bar_transition_test, refresh_test, route_test,
text_field_cursor_test (flutter/packages#13439)
2026-10-27 [email protected] [material_ui] Fix
`TextTheme` and `Typography` `debugFillProperties` defaults
(flutter/packages#13374)
2026-10-27 [email protected] [material_ui]
Update out-of-date typography documentation (flutter/packages#13372)
2026-10-27 [email protected] [shared_preferences_foundation] Fix web
compilation of apps that import the package (flutter/packages#13447)
2026-10-26 [email protected] Fix AI guidelines link in PR template
(flutter/packages#13411)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
suryaKommana2662 pushed a commit to suryaKommana2662/flutter that referenced this pull request Oct 29, 2026
flutter/packages@23a05b6...a8549ae

2026-10-27 [email protected] [material_ui]
Deprecate the black/white parameters in `Typography.material2021`
(flutter/packages#13443)
2026-10-27 [email protected] [shared_preferences] Defer
SharedPreferences fetch in `LegacySharedPreferencesPlugin` until first
use on Android (flutter/packages#13431)
2026-10-27 [email protected] [material_ui] Fix MenuAnchor
non-Unicode shortcut labels on JS web (flutter/packages#13370)
2026-10-27 [email protected] [material_ui]
Add Checkbox.markInsets (flutter/packages#12690)
2026-10-27 [email protected]
[vector_graphics_compiler] Handle coordinate lists on text and tspan
(flutter/packages#13287)
2026-10-27 [email protected]
[cupertino_ui] Unskip web tests that pass on CanvasKit and Skwasm:
nav_bar_transition_test, refresh_test, route_test,
text_field_cursor_test (flutter/packages#13439)
2026-10-27 [email protected] [material_ui] Fix
`TextTheme` and `Typography` `debugFillProperties` defaults
(flutter/packages#13374)
2026-10-27 [email protected] [material_ui]
Update out-of-date typography documentation (flutter/packages#13372)
2026-10-27 [email protected] [shared_preferences_foundation] Fix web
compilation of apps that import the package (flutter/packages#13447)
2026-10-26 [email protected] Fix AI guidelines link in PR template
(flutter/packages#13411)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD p: shared_preferences platform-android

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants