[go: nahoru, domu]

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_platform_view] MaskView pool to reuse maskViews. #38989

Merged
merged 6 commits into from
Jan 21, 2023

Conversation

cyanglaz
Copy link
Contributor

The maskViews are constructed and destructed every frame for every PlatformView on the screen. It slows down the performance quite a lot.
This PR adds a pool that caches maximum of 5 maskViews. These maskViews are reused every frame.
If there are more than 5 maskViews required in a frame, extra maskViews are constructed and destructed each frame. (This scenario is very unlikely in real life application)

Part of flutter/flutter#116640

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 Hixie said 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.

reuse mask view

format

format

draft

draft

fixes

test
@cyanglaz cyanglaz force-pushed the platform_view_reuse_mask_view branch from d9aa181 to ef09d5b Compare January 18, 2023 23:03
@interface FlutterClippingMaskViewPool ()

@property(assign, nonatomic) NSUInteger capacity;
@property(retain, nonatomic) NSMutableArray<FlutterClippingMaskView*>* pool;
Copy link
Member
@jmagman jmagman Jan 18, 2023

Choose a reason for hiding this comment

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

Would this retain the FlutterClippingMaskView even after the ChildClippingView is dealloc'd?
Would NSPointerArray with NSPointerFunctionsWeakMemory (I think zeroes it out with MRC but worth testing) work better here? If you could reliably know the mask views in the pool are used (because they are retained by a ChildClippingView), then you could get rid of recycleMaskViews and during insert compact, then insert at count if there was capacity.

Copy link
Member

Choose a reason for hiding this comment

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

Well never mind, retaining it after the ChildClippingView is dealloc'd is the whole point of this PR...
Can you confirm that deallocating FlutterPlatformViewsController releases the pool and the mask views?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add a test for it!

@@ -328,6 +353,7 @@ class FlutterPlatformViewsController {
fml::scoped_nsobject<FlutterMethodChannel> channel_;
fml::scoped_nsobject<UIView> flutter_view_;
fml::scoped_nsobject<UIViewController> flutter_view_controller_;
fml::scoped_nsobject<FlutterClippingMaskViewPool> mask_view_pool_;
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 reason to make this a scoped_nsobject?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I should change it to properties. I was blindly following the current pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, actually, it is in a c++ class. So I think scoped_nsobject is better that the lifecycle is automatically managed.

Copy link
Member

Choose a reason for hiding this comment

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

Which class is c++? Your new FlutterClippingMaskViewPool is a Obj-C class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FlutterPlatformViewsController is the c++ class.

@interface FlutterClippingMaskViewPool ()

@property(assign, nonatomic) NSUInteger capacity;
@property(retain, nonatomic) NSMutableArray<FlutterClippingMaskView*>* pool;
Copy link
Member

Choose a reason for hiding this comment

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

Well never mind, retaining it after the ChildClippingView is dealloc'd is the whole point of this PR...
Can you confirm that deallocating FlutterPlatformViewsController releases the pool and the mask views?

Copy link
Contributor
@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

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

Just a question on the pool capacity.

- (FlutterClippingMaskView*)getMaskViewWithFrame:(CGRect)frame {
FML_DCHECK(self.availableIndex <= self.capacity);
FlutterClippingMaskView* maskView;
if (self.availableIndex == self.capacity) {
Copy link
Contributor

Choose a reason for hiding this comment

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

it looks like the capacity is not updated? should it be increased when you add a new one?

Copy link
Contributor

Choose a reason for hiding this comment

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

we can probably just compare with pool.count, and do not keep this capacity ivar?

Copy link
Contributor Author
@cyanglaz cyanglaz Jan 19, 2023

Choose a reason for hiding this comment

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

The capacity is a static limit set for the pool. My intention is to limit the memory used by the pool. For example, (it is probably not possible in real life), I don't want the pool to cache 20 views for the whole application session.

Copy link
Contributor

Choose a reason for hiding this comment

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

gotcha. I mistakenly thought that this capacity increases like an NSMutableArray.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, yeah it could be confusing, I added a comment.

@@ -451,6 +453,17 @@ static bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
return clipCount;
}

void FlutterPlatformViewsController::ClipViewAddMaskView(UIView* clipView) {
Copy link
Contributor

Choose a reason for hiding this comment

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

supernit: ClipViewSetMaskView?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@cyanglaz cyanglaz added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 21, 2023
@auto-submit auto-submit bot merged commit 2ee8257 into flutter:main Jan 21, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 21, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 21, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 21, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Jan 21, 2023
…118938)

* 2ee82578c [ios_platform_view] MaskView pool to reuse maskViews. (flutter/engine#38989)

* c137b3b33 Roll Fuchsia Mac SDK from GvtVLigysBcywNN9T... to ZTKDeVL1HDAwsZdhl... (flutter/engine#39044)

* 3a444b366 Roll Dart SDK from 807077cc5d1b to 8c2eb20b5376 (2 revisions) (flutter/engine#39047)
@cyanglaz cyanglaz deleted the platform_view_reuse_mask_view branch January 24, 2023 19:08
ricardoamador pushed a commit to ricardoamador/engine that referenced this pull request Jan 25, 2023
* reuse maskView

reuse mask view

format

format

draft

draft

fixes

test

* comments

* fix comment

* test for releasing maskView

* updates

* fix
cyanglaz pushed a commit that referenced this pull request Feb 8, 2023
auto-submit bot pushed a commit that referenced this pull request Feb 8, 2023
cyanglaz pushed a commit that referenced this pull request Feb 8, 2023
auto-submit bot pushed a commit that referenced this pull request Feb 8, 2023
cyanglaz pushed a commit that referenced this pull request Feb 13, 2023
cyanglaz pushed a commit to cyanglaz/engine that referenced this pull request Feb 14, 2023
auto-submit bot pushed a commit that referenced this pull request Feb 14, 2023
* Revert "Revert "Revert "[ios_platform_view] MaskView pool to reuse maskViews. (#38989)" (#39490)" (#39498)"

This reverts commit 4270d74.

* fix build error
0xZOne pushed a commit to 0xZOne/engine that referenced this pull request Feb 20, 2023
…er#39608)

* Revert "Revert "Revert "[ios_platform_view] MaskView pool to reuse maskViews. (flutter#38989)" (flutter#39490)" (flutter#39498)"

This reverts commit 4270d74.

* fix build error
auto-submit bot pushed a commit that referenced this pull request Jun 20, 2023
cp #42823.

The PR fixes flutter/flutter#125620, which was a regression caused by #38989. We cannot simply revert #38989 because #38989 addressed a critical performance issue 

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
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
3 participants