Skip to content

Commit

Permalink
feat: Optimise RCTKeyWindow() calls in RCTForceTouchAvailable method (f…
Browse files Browse the repository at this point in the history
…acebook#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: facebook#41935

Test Plan: CI Green

Reviewed By: dmytrorykun

Differential Revision: D52172510

Pulled By: cipolleschi

fbshipit-source-id: 881a3125a2af4376ce65d785d8eee09c7d8f1f16
  • Loading branch information
okwasniewski authored and Saadnajmi committed Jan 15, 2024
1 parent 131e062 commit 4877381
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 @@ -634,12 +634,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;
}
#endif // [macOS]

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 @@ -15,8 +15,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;
#else // [macOS
Expand Down

0 comments on commit 4877381

Please sign in to comment.