Repository navigation
perf(ios): lay out views with fewer native calls #11544
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
Open
triniwiz
wants to merge
2
commits into
main
Choose a base branch
from
perf/ios-layout-native-reads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,9 @@ export class View extends ViewCommon { | |
| private _isTransformed = false; | ||
| private _privateFlags: number = PFLAG_LAYOUT_REQUIRED | PFLAG_FORCE_LAYOUT; | ||
| private _cachedFrame: CGRect; | ||
| private _lastLayoutPosition: { frame: CGRect; left: number; top: number; right: number; bottom: number }; | ||
| private _framePosition: Position; | ||
| private _pendingFramePosition: Position; | ||
| private _suspendCATransaction = false; | ||
| /** | ||
| * Native background states. | ||
|
|
@@ -87,6 +90,8 @@ export class View extends ViewCommon { | |
| super.disposeNativeView(); | ||
|
|
||
| this._cachedFrame = null; | ||
| this._lastLayoutPosition = null; | ||
| this._framePosition = null; | ||
| this._isLaidOut = false; | ||
| this._isTransformed = false; | ||
| } | ||
|
|
@@ -151,7 +156,7 @@ export class View extends ViewCommon { | |
| if (this.nativeViewProtected && SDK_VERSION > 10) { | ||
| // on iOS 11+ it is possible to have a changed layout frame due to safe area insets | ||
| // get the frame and adjust the position, so that onLayout works correctly | ||
| position = IOSHelper.getPositionFromFrame(this.nativeViewProtected.frame); | ||
| position = (setFrame && !this._isTransformed && this._framePosition) || IOSHelper.getPositionFromFrame(this.nativeViewProtected.frame); | ||
| } else { | ||
| position = { left, top, right, bottom }; | ||
| } | ||
|
|
@@ -244,8 +249,11 @@ export class View extends ViewCommon { | |
| nativeView.frame = frame; | ||
|
|
||
| const adjustedFrame = this.applySafeAreaInsets(frame); | ||
| if (adjustedFrame) { | ||
| if (adjustedFrame && adjustedFrame !== frame) { | ||
| nativeView.frame = adjustedFrame; | ||
| this._framePosition = null; | ||
| } else { | ||
| this._framePosition = this._pendingFramePosition; | ||
| } | ||
|
|
||
| if (transform != null) { | ||
|
|
@@ -296,14 +304,22 @@ export class View extends ViewCommon { | |
| return; | ||
| } | ||
|
|
||
| const last = this._lastLayoutPosition; | ||
| if (last && this._isLaidOut && last.frame === this._cachedFrame && last.left === left && last.top === top && last.right === right && last.bottom === bottom) { | ||
| return; | ||
| } | ||
|
|
||
| const nativeView = this.nativeViewProtected; | ||
| const frame = IOSHelper.getFrameFromPosition({ | ||
| left, | ||
| top, | ||
| right, | ||
| bottom, | ||
| }); | ||
| this._pendingFramePosition = IOSHelper.getPositionFromFramePosition(left, top, right, bottom); | ||
| this._setNativeViewFrame(nativeView, frame); | ||
| this._pendingFramePosition = null; | ||
| this._lastLayoutPosition = { frame: this._cachedFrame, left, top, right, bottom }; | ||
| } | ||
|
|
||
| public _layoutParent() { | ||
|
|
@@ -360,7 +376,6 @@ export class View extends ViewCommon { | |
| } | ||
|
|
||
| public getSafeAreaInsets(): Position { | ||
| const safeAreaInsets = this.nativeViewProtected && this.nativeViewProtected.safeAreaInsets; | ||
| const insets = { left: 0, top: 0, right: 0, bottom: 0 }; | ||
| if (this.iosIgnoreSafeArea) { | ||
| return insets; | ||
|
|
@@ -370,6 +385,7 @@ export class View extends ViewCommon { | |
| if (IOSHelper.hasIOSManagedInsetAncestor(this)) { | ||
| return insets; | ||
| } | ||
| const safeAreaInsets = this.nativeViewProtected && this.nativeViewProtected.safeAreaInsets; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (safeAreaInsets) { | ||
| insets.left = layout.round(layout.toDevicePixels(safeAreaInsets.left)); | ||
| insets.top = layout.round(layout.toDevicePixels(safeAreaInsets.top)); | ||
|
|
||
22 changes: 22 additions & 0 deletions
22
packages/core/ui/core/view/view-helper/frame-position.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { describe, it, expect, beforeAll } from 'vitest'; | ||
|
|
||
| import { IOSHelper } from '.'; | ||
|
|
||
| describe('IOSHelper.getPositionFromFramePosition', () => { | ||
| beforeAll(() => { | ||
| (global as any).CGRectMake ??= (x: number, y: number, width: number, height: number) => ({ origin: { x, y }, size: { width, height } }); | ||
| }); | ||
|
|
||
| it('matches reading the position back from the frame made for it', () => { | ||
| const positions = [ | ||
| [0, 0, 100, 50], | ||
| [10.4, 20.6, 110.5, 70.49], | ||
| [-5, 3, 2, 1], | ||
| [30, 40, 20, 30], | ||
| [1234.5678, 0.0001, 2000.123, 99.999], | ||
| ]; | ||
| for (const [left, top, right, bottom] of positions) { | ||
| expect(IOSHelper.getPositionFromFramePosition(left, top, right, bottom)).toEqual(IOSHelper.getPositionFromFrame(IOSHelper.getFrameFromPosition({ left, top, right, bottom }))); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/core/ui/core/view/view-helper/safe-area-expansion.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { describe, it, expect, beforeAll } from 'vitest'; | ||
|
|
||
| import { IOSHelper } from '.'; | ||
| import { layout } from '../../../../utils/layout-helper'; | ||
|
|
||
| type Rect = { origin: { x: number; y: number }; size: { width: number; height: number } }; | ||
| const rect = (x: number, y: number, width: number, height: number): Rect => ({ origin: { x, y }, size: { width, height } }); | ||
|
|
||
| class FakeScrollView { | ||
| constructor( | ||
| public safeAreaInsets: { top: number; left: number; bottom: number; right: number }, | ||
| public contentSize: { width: number; height: number }, | ||
| public contentOffset: { x: number; y: number }, | ||
| ) {} | ||
| } | ||
|
|
||
| class FakeControllerView { | ||
| constructor( | ||
| public frame: Rect, | ||
| private safeFrame: Rect, | ||
| private inWindow: { x: number; y: number }, | ||
| ) {} | ||
| get safeAreaLayoutGuide() { | ||
| return { layoutFrame: this.safeFrame }; | ||
| } | ||
| get bounds() { | ||
| return rect(0, 0, this.frame.size.width, this.frame.size.height); | ||
| } | ||
| convertPointToView() { | ||
| return this.inWindow; | ||
| } | ||
| } | ||
|
|
||
| function expandBeyondSafeAreaBefore(view: any, frame: Rect): Rect { | ||
| const availableSpace = IOSHelper.getAvailableSpaceFromParent(view, frame); | ||
| if (!availableSpace || !availableSpace.safeArea || !availableSpace.fullscreen) { | ||
| return frame; | ||
| } | ||
| const position = IOSHelper.getPositionFromFrame(frame); | ||
| const safeAreaPosition = IOSHelper.getPositionFromFrame(availableSpace.safeArea); | ||
| const fullscreenPosition = IOSHelper.getPositionFromFrame(availableSpace.fullscreen); | ||
| const inWindowPosition = IOSHelper.getPositionFromFrame(availableSpace.inWindow); | ||
| const adjustedPosition = position; | ||
| if (position.left && inWindowPosition.left <= safeAreaPosition.left) { | ||
| adjustedPosition.left = fullscreenPosition.left; | ||
| } | ||
| if (position.top && inWindowPosition.top <= safeAreaPosition.top) { | ||
| adjustedPosition.top = fullscreenPosition.top; | ||
| } | ||
| if (inWindowPosition.right < fullscreenPosition.right && inWindowPosition.right >= safeAreaPosition.right + fullscreenPosition.left) { | ||
| adjustedPosition.right += fullscreenPosition.right - inWindowPosition.right; | ||
| } | ||
| if (inWindowPosition.bottom < fullscreenPosition.bottom && inWindowPosition.bottom >= safeAreaPosition.bottom + fullscreenPosition.top) { | ||
| adjustedPosition.bottom += fullscreenPosition.bottom - inWindowPosition.bottom; | ||
| } | ||
|
|
||
| return rect(layout.toDeviceIndependentPixels(adjustedPosition.left), layout.toDeviceIndependentPixels(adjustedPosition.top), layout.toDeviceIndependentPixels(adjustedPosition.right - adjustedPosition.left), layout.toDeviceIndependentPixels(adjustedPosition.bottom - adjustedPosition.top)); | ||
| } | ||
|
|
||
| function random(seed: number) { | ||
| return () => { | ||
| seed = (seed * 1103515245 + 12345) & 0x7fffffff; | ||
| return seed / 0x7fffffff; | ||
| }; | ||
| } | ||
|
|
||
| describe('IOSHelper.expandBeyondSafeArea', () => { | ||
| beforeAll(() => { | ||
| (global as any).CGRectMake ??= rect; | ||
| (global as any).UIScrollView ??= FakeScrollView; | ||
| }); | ||
|
|
||
| function viewIn(container: { scrollView?: FakeScrollView; controllerView?: FakeControllerView }, location: { x: number; y: number }) { | ||
| const parent: any = container.scrollView ? { nativeViewProtected: container.scrollView } : { viewController: { view: container.controllerView } }; | ||
| return { parent: { parent }, getLocationInWindow: () => location } as any; | ||
| } | ||
|
|
||
| it('matches the previous computation inside a scroll view and a view controller', () => { | ||
| const next = random(7); | ||
| const pick = (values: number[]) => values[Math.floor(next() * values.length)]; | ||
| let expanded = 0; | ||
| for (let i = 0; i < 2000; i++) { | ||
| const insets = { top: pick([0, 47, 59.5]), left: pick([0, 44]), bottom: pick([0, 34]), right: pick([0, 44]) }; | ||
| const width = pick([390, 430, 844]); | ||
| const height = pick([844, 932, 2000]); | ||
| const location = { x: pick([0, 0.5, 10, width - 100]), y: pick([0, 20, 47, height - 50]) }; | ||
| const frame = rect(pick([0, 3.3333, 10, 44]), pick([0, 16.6667, 47]), pick([100, width, 87.6667]), pick([50, height - 34, 33.3333])); | ||
| const container = next() < 0.5 ? { scrollView: new FakeScrollView(insets, { width, height }, { x: 0, y: pick([0, 120.5]) }) } : { controllerView: new FakeControllerView(rect(0, 0, width, height), rect(insets.left, insets.top, width - insets.left - insets.right, height - insets.top - insets.bottom), { x: 0, y: pick([0, 300]) }) }; | ||
| const view = viewIn(container, location); | ||
|
|
||
| const expected = expandBeyondSafeAreaBefore(view, frame); | ||
| const actual = IOSHelper.expandBeyondSafeArea(view, frame as any) as unknown as Rect; | ||
|
|
||
| expect([actual.origin.x, actual.origin.y, actual.size.width, actual.size.height]).toEqual([expected.origin.x, expected.origin.y, expected.size.width, expected.size.height]); | ||
| if (expected.origin.y !== frame.origin.y || expected.size.height !== frame.size.height || expected.size.width !== frame.size.width) { | ||
| expanded++; | ||
| } | ||
| } | ||
| expect(expanded).toBeGreaterThan(200); | ||
| }); | ||
|
|
||
| it('returns the frame itself when nothing changes', () => { | ||
| const view = viewIn({ scrollView: new FakeScrollView({ top: 0, left: 0, bottom: 0, right: 0 }, { width: 400, height: 800 }, { x: 0, y: 0 }) }, { x: 100, y: 100 }); | ||
| const frame = rect(100, 100, 50, 50); | ||
|
|
||
| expect(IOSHelper.expandBeyondSafeArea(view, frame as any)).toBe(frame); | ||
| }); | ||
|
|
||
| it('leaves a detached view alone', () => { | ||
| const frame = rect(0, 0, 10, 10); | ||
|
|
||
| expect(IOSHelper.expandBeyondSafeArea({ getLocationInWindow: () => ({ x: 0, y: 0 }) } as any, frame as any)).toBe(frame); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isnt it a duplicate cost as you already go through parents in getFrameFromPosition? Cant we get position from frame?