From b7d02c9c8616035674a0002754ce89408ba785a8 Mon Sep 17 00:00:00 2001 From: farfromrefuge Date: Mon, 1 May 2023 21:56:13 +0000 Subject: [PATCH] perf: faster nativeView accessors (#10279) `this.nativeViewProtected` is faster than `this.ios` Co-authored-by: Nathan Walker --- packages/core/ui/core/view/index.ios.ts | 13 ++++----- packages/core/ui/layouts/layout-base.ios.ts | 5 ++-- packages/core/ui/list-view/index.ios.ts | 29 +++++++++++---------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/packages/core/ui/core/view/index.ios.ts b/packages/core/ui/core/view/index.ios.ts index f30052e5b..cc36654ad 100644 --- a/packages/core/ui/core/view/index.ios.ts +++ b/packages/core/ui/core/view/index.ios.ts @@ -897,13 +897,13 @@ export class View extends ViewCommon implements ViewDefinition { if (!updateSuspended) { CATransaction.begin(); } - - if (this.nativeViewProtected) { + const view = this.nativeViewProtected; + if (view) { if (value instanceof UIColor) { - this.nativeViewProtected.backgroundColor = value; + view.backgroundColor = value; } else { iosBackground.createBackgroundUIColor(this, (color: UIColor) => { - this.nativeViewProtected.backgroundColor = color; + view.backgroundColor = color; }); this._setNativeClipToBounds(); } @@ -917,9 +917,10 @@ export class View extends ViewCommon implements ViewDefinition { } _setNativeClipToBounds() { - if (this.nativeViewProtected) { + const view = this.nativeViewProtected; + if (view) { const backgroundInternal = this.style.backgroundInternal; - this.nativeViewProtected.clipsToBounds = (this.nativeViewProtected instanceof UIScrollView || backgroundInternal.hasBorderWidth() || backgroundInternal.hasBorderRadius()) && !backgroundInternal.hasBoxShadow(); + view.clipsToBounds = (view instanceof UIScrollView || backgroundInternal.hasBorderWidth() || backgroundInternal.hasBorderRadius()) && !backgroundInternal.hasBoxShadow(); } } diff --git a/packages/core/ui/layouts/layout-base.ios.ts b/packages/core/ui/layouts/layout-base.ios.ts index e6a8e3cbc..13f8fa320 100644 --- a/packages/core/ui/layouts/layout-base.ios.ts +++ b/packages/core/ui/layouts/layout-base.ios.ts @@ -23,8 +23,9 @@ export class LayoutBase extends LayoutBaseCommon { _setNativeClipToBounds() { if (this.clipToBounds) { - if (this.nativeViewProtected) { - this.nativeViewProtected.clipsToBounds = true; + const view = this.nativeViewProtected; + if (view) { + view.clipsToBounds = true; } } else { super._setNativeClipToBounds(); diff --git a/packages/core/ui/list-view/index.ios.ts b/packages/core/ui/list-view/index.ios.ts index 790abf1ed..61404c33f 100644 --- a/packages/core/ui/list-view/index.ios.ts +++ b/packages/core/ui/list-view/index.ios.ts @@ -275,8 +275,9 @@ export class ListView extends ListViewBase { _setNativeClipToBounds() { // Always set clipsToBounds for list-view - if (this.ios) { - this.ios.clipsToBounds = true; + const view = this.nativeViewProtected; + if (view) { + view.clipsToBounds = true; } } @@ -286,7 +287,7 @@ export class ListView extends ListViewBase { if (this._isDataDirty) { this.refresh(); } - this.ios.delegate = this._delegate; + this.nativeViewProtected.delegate = this._delegate; } // @ts-ignore @@ -313,7 +314,7 @@ export class ListView extends ListViewBase { } private _scrollToIndex(index: number, animated = true) { - if (!this.ios) { + if (!this.nativeViewProtected) { return; } @@ -326,7 +327,7 @@ export class ListView extends ListViewBase { index = itemsLength - 1; } - this.ios.scrollToRowAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), UITableViewScrollPosition.Top, animated); + this.nativeViewProtected.scrollToRowAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), UITableViewScrollPosition.Top, animated); } else if (Trace.isEnabled()) { Trace.write(`Cannot scroll listview to index ${index} when listview items not set`, Trace.categories.Binding); } @@ -341,7 +342,7 @@ export class ListView extends ListViewBase { }); if (this.isLoaded) { - this.ios.reloadData(); + this.nativeViewProtected.reloadData(); this.requestLayout(); this._isDataDirty = false; } else { @@ -350,7 +351,7 @@ export class ListView extends ListViewBase { } public isItemAtIndexVisible(itemIndex: number): boolean { - const indexes: NSIndexPath[] = Array.from(this.ios.indexPathsForVisibleRows); + const indexes: NSIndexPath[] = Array.from(this.nativeViewProtected.indexPathsForVisibleRows); return indexes.some((visIndex) => visIndex.row === itemIndex); } @@ -365,7 +366,7 @@ export class ListView extends ListViewBase { public _onRowHeightPropertyChanged(oldValue: CoreTypes.LengthType, newValue: CoreTypes.LengthType) { const value = layout.toDeviceIndependentPixels(this._effectiveRowHeight); - const nativeView = this.ios; + const nativeView = this.nativeViewProtected; if (value < 0) { nativeView.rowHeight = UITableViewAutomaticDimension; nativeView.estimatedRowHeight = DEFAULT_HEIGHT; @@ -395,7 +396,7 @@ export class ListView extends ListViewBase { const changed = this._setCurrentMeasureSpecs(widthMeasureSpec, heightMeasureSpec); super.measure(widthMeasureSpec, heightMeasureSpec); if (changed) { - this.ios.reloadData(); + this.nativeViewProtected.reloadData(); } } @@ -432,7 +433,7 @@ export class ListView extends ListViewBase { return height; } - return this.ios.estimatedRowHeight; + return this.nativeViewProtected.estimatedRowHeight; } public _prepareCell(cell: ListViewCell, indexPath: NSIndexPath): number { @@ -499,10 +500,10 @@ export class ListView extends ListViewBase { } [separatorColorProperty.getDefault](): UIColor { - return this.ios.separatorColor; + return this.nativeViewProtected.separatorColor; } [separatorColorProperty.setNative](value: Color | UIColor) { - this.ios.separatorColor = value instanceof Color ? value.ios : value; + this.nativeViewProtected.separatorColor = value instanceof Color ? value.ios : value; } [itemTemplatesProperty.getDefault](): KeyedTemplate[] { @@ -512,7 +513,7 @@ export class ListView extends ListViewBase { this._itemTemplatesInternal = new Array(this._defaultTemplate); if (value) { for (let i = 0, length = value.length; i < length; i++) { - this.ios.registerClassForCellReuseIdentifier(ListViewCell.class(), value[i].key); + this.nativeViewProtected.registerClassForCellReuseIdentifier(ListViewCell.class(), value[i].key); } this._itemTemplatesInternal = this._itemTemplatesInternal.concat(value); } @@ -524,7 +525,7 @@ export class ListView extends ListViewBase { return DEFAULT_HEIGHT; } [iosEstimatedRowHeightProperty.setNative](value: CoreTypes.LengthType) { - const nativeView = this.ios; + const nativeView = this.nativeViewProtected; const estimatedHeight = Length.toDevicePixels(value, 0); nativeView.estimatedRowHeight = estimatedHeight < 0 ? DEFAULT_HEIGHT : estimatedHeight; }