Skip to content

Commit b7d02c9

Browse files
perf: faster nativeView accessors (NativeScript#10279)
`this.nativeViewProtected` is faster than `this.ios` Co-authored-by: Nathan Walker <walkerrunpdx@gmail.com>
1 parent a14becd commit b7d02c9

3 files changed

Lines changed: 25 additions & 22 deletions

File tree

packages/core/ui/core/view/index.ios.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,13 +897,13 @@ export class View extends ViewCommon implements ViewDefinition {
897897
if (!updateSuspended) {
898898
CATransaction.begin();
899899
}
900-
901-
if (this.nativeViewProtected) {
900+
const view = this.nativeViewProtected;
901+
if (view) {
902902
if (value instanceof UIColor) {
903-
this.nativeViewProtected.backgroundColor = value;
903+
view.backgroundColor = value;
904904
} else {
905905
iosBackground.createBackgroundUIColor(this, (color: UIColor) => {
906-
this.nativeViewProtected.backgroundColor = color;
906+
view.backgroundColor = color;
907907
});
908908
this._setNativeClipToBounds();
909909
}
@@ -917,9 +917,10 @@ export class View extends ViewCommon implements ViewDefinition {
917917
}
918918

919919
_setNativeClipToBounds() {
920-
if (this.nativeViewProtected) {
920+
const view = this.nativeViewProtected;
921+
if (view) {
921922
const backgroundInternal = this.style.backgroundInternal;
922-
this.nativeViewProtected.clipsToBounds = (this.nativeViewProtected instanceof UIScrollView || backgroundInternal.hasBorderWidth() || backgroundInternal.hasBorderRadius()) && !backgroundInternal.hasBoxShadow();
923+
view.clipsToBounds = (view instanceof UIScrollView || backgroundInternal.hasBorderWidth() || backgroundInternal.hasBorderRadius()) && !backgroundInternal.hasBoxShadow();
923924
}
924925
}
925926

packages/core/ui/layouts/layout-base.ios.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ export class LayoutBase extends LayoutBaseCommon {
2323

2424
_setNativeClipToBounds() {
2525
if (this.clipToBounds) {
26-
if (this.nativeViewProtected) {
27-
this.nativeViewProtected.clipsToBounds = true;
26+
const view = this.nativeViewProtected;
27+
if (view) {
28+
view.clipsToBounds = true;
2829
}
2930
} else {
3031
super._setNativeClipToBounds();

packages/core/ui/list-view/index.ios.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ export class ListView extends ListViewBase {
275275

276276
_setNativeClipToBounds() {
277277
// Always set clipsToBounds for list-view
278-
if (this.ios) {
279-
this.ios.clipsToBounds = true;
278+
const view = this.nativeViewProtected;
279+
if (view) {
280+
view.clipsToBounds = true;
280281
}
281282
}
282283

@@ -286,7 +287,7 @@ export class ListView extends ListViewBase {
286287
if (this._isDataDirty) {
287288
this.refresh();
288289
}
289-
this.ios.delegate = this._delegate;
290+
this.nativeViewProtected.delegate = this._delegate;
290291
}
291292

292293
// @ts-ignore
@@ -313,7 +314,7 @@ export class ListView extends ListViewBase {
313314
}
314315

315316
private _scrollToIndex(index: number, animated = true) {
316-
if (!this.ios) {
317+
if (!this.nativeViewProtected) {
317318
return;
318319
}
319320

@@ -326,7 +327,7 @@ export class ListView extends ListViewBase {
326327
index = itemsLength - 1;
327328
}
328329

329-
this.ios.scrollToRowAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), UITableViewScrollPosition.Top, animated);
330+
this.nativeViewProtected.scrollToRowAtIndexPathAtScrollPositionAnimated(NSIndexPath.indexPathForItemInSection(index, 0), UITableViewScrollPosition.Top, animated);
330331
} else if (Trace.isEnabled()) {
331332
Trace.write(`Cannot scroll listview to index ${index} when listview items not set`, Trace.categories.Binding);
332333
}
@@ -341,7 +342,7 @@ export class ListView extends ListViewBase {
341342
});
342343

343344
if (this.isLoaded) {
344-
this.ios.reloadData();
345+
this.nativeViewProtected.reloadData();
345346
this.requestLayout();
346347
this._isDataDirty = false;
347348
} else {
@@ -350,7 +351,7 @@ export class ListView extends ListViewBase {
350351
}
351352

352353
public isItemAtIndexVisible(itemIndex: number): boolean {
353-
const indexes: NSIndexPath[] = Array.from(this.ios.indexPathsForVisibleRows);
354+
const indexes: NSIndexPath[] = Array.from(this.nativeViewProtected.indexPathsForVisibleRows);
354355

355356
return indexes.some((visIndex) => visIndex.row === itemIndex);
356357
}
@@ -365,7 +366,7 @@ export class ListView extends ListViewBase {
365366

366367
public _onRowHeightPropertyChanged(oldValue: CoreTypes.LengthType, newValue: CoreTypes.LengthType) {
367368
const value = layout.toDeviceIndependentPixels(this._effectiveRowHeight);
368-
const nativeView = this.ios;
369+
const nativeView = this.nativeViewProtected;
369370
if (value < 0) {
370371
nativeView.rowHeight = UITableViewAutomaticDimension;
371372
nativeView.estimatedRowHeight = DEFAULT_HEIGHT;
@@ -395,7 +396,7 @@ export class ListView extends ListViewBase {
395396
const changed = this._setCurrentMeasureSpecs(widthMeasureSpec, heightMeasureSpec);
396397
super.measure(widthMeasureSpec, heightMeasureSpec);
397398
if (changed) {
398-
this.ios.reloadData();
399+
this.nativeViewProtected.reloadData();
399400
}
400401
}
401402

@@ -432,7 +433,7 @@ export class ListView extends ListViewBase {
432433
return height;
433434
}
434435

435-
return this.ios.estimatedRowHeight;
436+
return this.nativeViewProtected.estimatedRowHeight;
436437
}
437438

438439
public _prepareCell(cell: ListViewCell, indexPath: NSIndexPath): number {
@@ -499,10 +500,10 @@ export class ListView extends ListViewBase {
499500
}
500501

501502
[separatorColorProperty.getDefault](): UIColor {
502-
return this.ios.separatorColor;
503+
return this.nativeViewProtected.separatorColor;
503504
}
504505
[separatorColorProperty.setNative](value: Color | UIColor) {
505-
this.ios.separatorColor = value instanceof Color ? value.ios : value;
506+
this.nativeViewProtected.separatorColor = value instanceof Color ? value.ios : value;
506507
}
507508

508509
[itemTemplatesProperty.getDefault](): KeyedTemplate[] {
@@ -512,7 +513,7 @@ export class ListView extends ListViewBase {
512513
this._itemTemplatesInternal = new Array<KeyedTemplate>(this._defaultTemplate);
513514
if (value) {
514515
for (let i = 0, length = value.length; i < length; i++) {
515-
this.ios.registerClassForCellReuseIdentifier(ListViewCell.class(), value[i].key);
516+
this.nativeViewProtected.registerClassForCellReuseIdentifier(ListViewCell.class(), value[i].key);
516517
}
517518
this._itemTemplatesInternal = this._itemTemplatesInternal.concat(value);
518519
}
@@ -524,7 +525,7 @@ export class ListView extends ListViewBase {
524525
return DEFAULT_HEIGHT;
525526
}
526527
[iosEstimatedRowHeightProperty.setNative](value: CoreTypes.LengthType) {
527-
const nativeView = this.ios;
528+
const nativeView = this.nativeViewProtected;
528529
const estimatedHeight = Length.toDevicePixels(value, 0);
529530
nativeView.estimatedRowHeight = estimatedHeight < 0 ? DEFAULT_HEIGHT : estimatedHeight;
530531
}

0 commit comments

Comments
 (0)