Skip to content

Commit

Permalink
feat: Optimise RCTKeyWindow() calls in RCTForceTouchAvailable method (#…
Browse files Browse the repository at this point in the history
…41935)

Summary:
This PR optimises RCTKeyWindow() calls in `RCTForceTouchAvailable` method. This method was calling RCTKeyWindow hundreds of times while scrolling on the screen.

Before:

On the video you can see that this function is being called **350 times** just from simple list scrolling. RCTKeyWindow is looping over app windows so it's not a cheap operation.

https://github.com/facebook/react-native/assets/52801365/5b69cbd6-d148-4d06-b672-bd7b60472c13

After: the function is called only few times at the start of the app to get initial layout measurements.

Solution: I think we can check just once for the force touch capabilities as devices can't change it on the fly

bypass-github-export-checks

## Changelog:

[IOS] [FIXED] - Optimise RCTKeyWindow() calls in RCTForceTouchAvailable method

Pull Request resolved: #41935

Test Plan: CI Green

Reviewed By: dmytrorykun

Differential Revision: D52172510

Pulled By: cipolleschi

fbshipit-source-id: 881a3125a2af4376ce65d785d8eee09c7d8f1f16
  • Loading branch information
okwasniewski authored and facebook-github-bot committed Dec 19, 2023
1 parent 21784e2 commit 90fb73e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
6 changes: 2 additions & 4 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,10 @@ BOOL RCTForceTouchAvailable(void)
static BOOL forceSupported;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
forceSupported =
[UITraitCollection class] && [UITraitCollection instancesRespondToSelector:@selector(forceTouchCapability)];
forceSupported = [UITraitCollection currentTraitCollection].forceTouchCapability == UIForceTouchCapabilityAvailable;
});

return forceSupported &&
(RCTKeyWindow() ?: [UIView new]).traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
return forceSupported;
}

NSError *RCTErrorWithMessage(NSString *message)
Expand Down
3 changes: 1 addition & 2 deletions packages/react-native/React/UIUtils/RCTUIUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ RCTDimensions RCTGetDimensions(CGFloat fontScale)
UIScreen *mainScreen = UIScreen.mainScreen;
CGSize screenSize = mainScreen.bounds.size;

UIView *mainWindow;
mainWindow = RCTKeyWindow();
UIView *mainWindow = RCTKeyWindow();
// We fallback to screen size if a key window is not found.
CGSize windowSize = mainWindow ? mainWindow.bounds.size : screenSize;

Expand Down

0 comments on commit 90fb73e

Please sign in to comment.