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

[iOS] Flush layer pool after platform view dispose #54056

Merged
merged 3 commits into from
Jul 23, 2024

Conversation

cbracken
Copy link
Member

@cbracken cbracken commented Jul 23, 2024

FlutterPlatformViewLayerPool is a pool of iOS layers used for rendering platform views on iOS. When layers are no longer needed, we currently mark them available for re-use but we never actually flush them and thus recover the memory associated with these layers once we know that the layers are no longer needed.

We now flush the layer pool on SubmitFrame if a previously-used layer is no longer used in the current frame. In theory, this could cause a performance regression in the case where an additional layer is needed every second or third frame, but we should keep it simple on the first pass and only complicate things later if warranted.

Fixes: flutter/flutter#152053

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C , Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or the PR is test-exempt. See testing the engine for instructions on writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

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

`FlutterPlatformViewLayerPool` is a pool of iOS layers used for
rendering platform views on iOS. When layers are no longer needed, we
currently mark them available for re-use but we never actually flush
them and thus recover the memory associated with these layers once we
know that the platformview is no longer used.

We now flush the layer pool to recover unused memory after platform
views have been disposed.

See: flutter/flutter#152053
@cbracken
Copy link
Member Author

Taking a look at how to sneak a test in here.

Copy link
Member

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

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

Changes look reasonable to me, modulo a test.

@jason-simmons
Copy link
Member

LGTM

@cbracken cbracken marked this pull request as ready for review July 23, 2024 23:06
@cbracken
Copy link
Member Author

Added a test.

Copy link
Member

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

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

LGTM

auto pool = flutter::FlutterPlatformViewLayerPool{};

// Add layers to the pool.
auto layer1 = pool.GetLayer(gr_context.get(), ios_context, MTLPixelFormatBGRA8Unorm);
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to check this is actually released? Or is that not a concern? I know the __weak/@autoreleasepool pattern won't work with non-Obj-C objects.

Copy link
Member Author

Choose a reason for hiding this comment

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

The pool holds these in std::shared_ptr<FlutterPlatformViewLayer> so they'll be cleaned up via RAII. If we templatized FlutterPlatformViewLayerPool we could stick in some other class and manipulate a count in the ctor/dtor that we could then test against, but that probably wouldn't give us much more benefit, since it still wouldn't show that the real FlutterPlatformView layer properly releases its memory -- it uses C smart pointers for all but the GrDirectContext so I trust it, but it's still not tested.

An integration test that looks at overall memory is probably the way to do something like that.

auto-submit bot pushed a commit that referenced this pull request Jul 23, 2024
Since this method isn't mutating the pool, we should mark it const.

Cleanup spotted while working on #54056.

No test because no semantic change. The compiler is the test.

[C  , Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
@cbracken cbracken added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 23, 2024
@auto-submit auto-submit bot merged commit e1952a3 into flutter:main Jul 23, 2024
29 checks passed
@cbracken cbracken deleted the flush-unused-layers branch July 23, 2024 23:55
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jul 24, 2024
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 platform-ios
Projects
None yet
5 participants