Skip to content

Persist the vulkan shader cache on teardown - #197017

Merged
auto-submit[bot] merged 3 commits into
flutter:masterfrom
walley892:vulkan_cache
Nov 21, 2026
Merged

auto-submit[bot] merged 3 commits into
flutter:masterfrom
walley892:vulkan_cache

Conversation

@walley892

@walley892 walley892 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Impeller's Vulkan backend on Android writes its pipeline cache to disk only once every 50 rasterized frames. Apps that are usually opened briefly may never reach that point, so they never write a pipeline cache, and every launch has
to compile pipelines from scratch.

This PR makes it so SurfaceContextVK::TeardownSwapchain now calls PipelineLibraryVK::PersistPipelineCacheToDisk(), saving the cache when the swapchain is torn down. This means that short sessions will warm the on-disk cache for the next launch.

In the lifetime of a Flutter app on Android, this most commonly happens when:

  1. The app is backgrounded
  2. The screen turns off

So app scenarios that are typically opened only for a fewer than 50 rasterized frames (e.x., showing a QR code) will still have a warmed Vulkan shader cache.

Also fixes a race condition where a pipeline could be skipped from persistence in the pipeline cache (#197081)

Fixes #197087

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant in-code documentation (doc comments with ///).
  • If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. e: impeller Impeller rendering backend issues and features requests labels Nov 19, 2026
@walley892

Copy link
Copy Markdown
Contributor Author

/gemini review

@walley892 walley892 added the CICD Run CI/CD label Nov 19, 2026

@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 modifies the Vulkan backend to persist the pipeline cache to disk during swapchain teardown when backgrounding the application, supported by new unit tests and mock Vulkan updates. Review feedback addresses potential lifetime issues where VkPipelineCache outlives VkDevice during shutdown, risks of background thread suspension on mobile platforms before cache persistence completes, thread-safety concerns for cache_dirty_, and a suggestion to reuse the locked device pointer.

@walley892
walley892 requested a review from gaaclarke November 19, 2026 23:46
@walley892
walley892 marked this pull request as ready for review November 19, 2026 23:46

@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 ensures that the Vulkan pipeline cache is safely persisted to disk when backgrounding the application or when the cache outlives the context. It introduces a strong reference to the device holder during persistence, triggers persistence on swapchain teardown, and adds corresponding unit tests. Feedback suggests using std::atomic::exchange on cache_dirty_ to prevent a potential race condition, and marking the strong_device variable with [[maybe_unused]] to avoid compiler warnings.

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This PR is pretty light on description considering the big changes we are making here.

  1. I'd like a comment that explains why it's safe to have a shared_ptr in the persist call now. Remember every shared_ptr becomes a potential place where the destructor can be called.
  2. There is a race in this implementation where is_dirty called when we request a pipeline, if a save happens it isn't really dirty until the shader is completed
  3. A decision was made to link the saving to the swapchain teardown, that decision should be documented
  4. We should update documentation on the persist call to document what threads call into it.

@walley892

Copy link
Copy Markdown
Contributor Author

I'd like a comment that explains why it's safe to have a shared_ptr in the persist call now. Remember every shared_ptr becomes a potential place where the destructor can be called.

Added. This thread is always joined and destroyed before the device.

There is a race in this implementation where is_dirty called when we request a pipeline, if a save happens it isn't really dirty until the shader is completed

It's dirty now - moved the is_dirty set to after the pipeline finishes compiling

We should update documentation on the persist call to document what threads call into it.

Done

A decision was made to link the saving to the swapchain teardown, that decision should be documented
I have a comment in TeardownSwapchain - what do you have in mind for this?

@walley892
walley892 requested a review from gaaclarke November 20, 2026 22:12
@gaaclarke

Copy link
Copy Markdown
Member

I have a comment in TeardownSwapchain - what do you have in mind for this?

I was thinking of the PR description. Its looking better. Just a short description why that is the right place to save the shaders. It's not obviously hooked to lifecycle events.

@walley892

Copy link
Copy Markdown
Contributor Author

I was thinking of the PR description. Its looking better. Just a short description why that is the right place to save the shaders. It's not obviously hooked to lifecycle events.

Beefed up the description and tied it to app lifecycle events.

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm, thanks

@walley892 walley892 added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 20, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 21, 2026
Merged via the queue into flutter:master with commit 2eaa55e Nov 21, 2026
25 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 21, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 21, 2026
flutter/flutter@b34b017...5423f94

2026-11-21 [email protected] Make an Impeller render target from a Vulkan backing store (flutter/flutter#193003)
2026-11-21 [email protected] Persist the vulkan shader cache on teardown (flutter/flutter#197017)
2026-11-21 [email protected] [flutter_tools] Preserve rolling log buffer for Chromium crash diagnostics (flutter/flutter#196037)
2026-11-21 [email protected] Roll Skia from 8845aecd8c53 to 39e0ad394168 (2 revisions) (flutter/flutter#197094)
2026-11-21 [email protected] Adds redirect for listen package in api.flutter.dev (flutter/flutter#197068)
2026-11-21 [email protected] Roll Dart SDK from c6857d58f0ec to fc86f5a42779 (1 revision) (flutter/flutter#197086)
2026-11-20 [email protected] iOS: Discard pending platform view disposals on reset (flutter/flutter#197026)
2026-11-20 [email protected] Tolerate a missing flutter/service_worker handler in the web first-frame notification (flutter/flutter#196329)
2026-11-20 [email protected] Roll Skia from 9aafbb357743 to 8845aecd8c53 (2 revisions) (flutter/flutter#197080)
2026-11-20 [email protected] Prevent crash in tessellator with extreme radii (flutter/flutter#196648)
2026-11-20 [email protected] Windows: in the flutter tool, always close the file handle in _download even when an HTTP request fails so that it is not left open and becomes undeletable (flutter/flutter#197061)
2026-11-20 [email protected] Roll Skia from b61640825c5b to 9aafbb357743 (7 revisions) (flutter/flutter#197066)
2026-11-20 [email protected] Swap dependabot reviewers (flutter/flutter#196995)
2026-11-20 [email protected] [iOS] Hide UIScrollEdgeEffect on FlutterSemanticsScrollView and scroll-to-top UIScrollView on iOS 26+ (flutter/flutter#195356)
2026-11-20 [email protected] [devicelab] Enable Vulkan GPU tracing by default on Android Impeller PerfTests (flutter/flutter#196822)
2026-11-20 [email protected] Roll Dart SDK from 3b5f45d812f8 to c6857d58f0ec (1 revision) (flutter/flutter#197063)
2026-11-20 [email protected] [flutter_tools] Delete flutter.version.json when switching channels (flutter/flutter#196943)
2026-11-20 [email protected] Update desktop TESTOWNERS (flutter/flutter#196930)
2026-11-20 [email protected] [web] Release CanvasKit WebGL contexts and surfaces when a view is removed (flutter/flutter#196816)
2026-11-20 [email protected] [Linux] Hide libunwind symbols in libflutter_linux_gtk.so (flutter/flutter#196451)
2026-11-20 [email protected] Roll Skia from a1edf5c2ea5c to b61640825c5b (1 revision) (flutter/flutter#197058)
2026-11-20 [email protected] [flutter_tools] Update --web-content-hash serving tip to cover assets and canvaskit (flutter/flutter#196908)
2026-11-20 [email protected] Send a semantics update when only the identifier changes (flutter/flutter#196460)
2026-11-20 [email protected] Roll Packages from 81b7c44 to 898818d (11 revisions) (flutter/flutter#197056)

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
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: 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

CICD Run CI/CD e: impeller Impeller rendering backend issues and features requests engine flutter/engine related. See also e: labels.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Impeller] Short-lived apps using the Vulkan backend never persist the Vulkan shader cache

2 participants