Skip to content

Commit 7986e47

Browse files
committed
Performance improvements after profiling the android flexbox example
1 parent d6d016b commit 7986e47

4 files changed

Lines changed: 63 additions & 36 deletions

File tree

‎tns-core-modules/ui/core/bindable/bindable.ts‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,33 @@ const contextKey = "context";
2424
const paramsRegex = /\[\s*(['"])*(\w*)\1\s*\]/;
2525
const bc = bindingConstants;
2626
const emptyArray = [];
27+
const propertiesCache = {};
2728

2829
function getProperties(property: string): Array<string> {
29-
let result: Array<string> = emptyArray;
30-
if (property) {
31-
// first replace all '$parents[..]' with a safe string
32-
// second removes all ] since they are not important for property access and not needed
33-
// then split properties either on '.' or '['
34-
const parentsMatches = property.match(parentsRegex);
35-
result = property.replace(parentsRegex, "parentsMatch")
36-
.replace(/\]/g, "")
37-
.split(/\.|\[/);
38-
39-
let parentsMatchesCounter = 0;
40-
for (let i = 0, resultLength = result.length; i < resultLength; i++) {
41-
if (result[i] === "parentsMatch") {
42-
result[i] = parentsMatches[parentsMatchesCounter++];
43-
}
44-
}
30+
if (!property) {
31+
return emptyArray;
32+
}
33+
34+
let result: Array<string> = propertiesCache[property];
35+
if (result) {
36+
return result;
4537
}
4638

39+
// first replace all '$parents[..]' with a safe string
40+
// second removes all ] since they are not important for property access and not needed
41+
// then split properties either on '.' or '['
42+
const parentsMatches = property.match(parentsRegex);
43+
result = property.replace(parentsRegex, "parentsMatch")
44+
.replace(/\]/g, "")
45+
.split(/\.|\[/);
46+
47+
let parentsMatchesCounter = 0;
48+
for (let i = 0, resultLength = result.length; i < resultLength; i++) {
49+
if (result[i] === "parentsMatch") {
50+
result[i] = parentsMatches[parentsMatchesCounter++];
51+
}
52+
}
53+
propertiesCache[property] = result;
4754
return result;
4855
}
4956

‎tns-core-modules/ui/image/image.android.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
ImageSource, ImageBase, stretchProperty, imageSourceProperty, srcProperty, tintColorProperty, Color,
33
isDataURI, isFileOrResourcePath, RESOURCE_PREFIX
44
} from "./image-common";
5-
import { path, knownFolders } from "../../file-system";
5+
import { knownFolders } from "../../file-system";
66

77
export * from "./image-common";
88

99
const FILE_PREFIX = "file:///";
1010
const ASYNC = "async";
1111

12+
let AndroidImageView: typeof org.nativescript.widgets.ImageView;
13+
1214
interface ImageLoadedListener {
1315
new (owner: Image): org.nativescript.widgets.image.Worker.OnImageLoadedListener;
1416
}
@@ -45,9 +47,12 @@ export class Image extends ImageBase {
4547
public useCache = true;
4648

4749
public createNativeView() {
50+
if (!AndroidImageView) {
51+
AndroidImageView = org.nativescript.widgets.ImageView;
52+
}
4853
initializeImageLoadedListener();
4954

50-
const imageView = new org.nativescript.widgets.ImageView(this._context);
55+
const imageView = new AndroidImageView(this._context);
5156
const listener = new ImageLoadedListener(this);
5257
imageView.setImageLoadedListener(listener);
5358
(<any>imageView).listener = listener;
@@ -92,7 +97,7 @@ export class Image extends ImageBase {
9297
} else {
9398
let fileName = value;
9499
if (fileName.indexOf("~/") === 0) {
95-
fileName = path.join(knownFolders.currentApp().path, fileName.replace("~/", ""));
100+
fileName = knownFolders.currentApp().path + "/" + fileName.replace("~/", "");
96101
}
97102

98103
imageView.setUri(FILE_PREFIX + fileName, this.decodeWidth, this.decodeHeight, this.useCache, async);

‎tns-core-modules/ui/label/label.android.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { TextBase, WhiteSpace, whiteSpaceProperty } from "../text-base";
33

44
export * from "../text-base";
55

6+
let TextView: typeof android.widget.TextView;
7+
68
export class Label extends TextBase implements LabelDefinition {
79
nativeView: android.widget.TextView;
810

@@ -14,7 +16,10 @@ export class Label extends TextBase implements LabelDefinition {
1416
}
1517

1618
public createNativeView() {
17-
return new android.widget.TextView(this._context);
19+
if (!TextView) {
20+
TextView = android.widget.TextView;
21+
}
22+
return new TextView(this._context);
1823
}
1924

2025
public initNativeView(): void {

‎tns-core-modules/ui/layouts/flexbox-layout/flexbox-layout.android.ts‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ import {
1212

1313
export * from "./flexbox-layout-common";
1414

15+
let widgetFlexboxLayout: typeof org.nativescript.widgets.FlexboxLayout;
16+
let widgetLayoutParams: typeof org.nativescript.widgets.FlexboxLayout.LayoutParams;
17+
1518
function makeNativeSetter<T>(setter: (lp: org.nativescript.widgets.FlexboxLayout.LayoutParams, value: T) => void) {
1619
return function(this: View, value: T) {
1720
const nativeView: android.view.View = this.nativeView;
18-
const lp = nativeView.getLayoutParams() || new org.nativescript.widgets.FlexboxLayout.LayoutParams();
19-
if (lp instanceof org.nativescript.widgets.FlexboxLayout.LayoutParams) {
21+
const lp = nativeView.getLayoutParams() || new widgetLayoutParams();
22+
if (lp instanceof widgetLayoutParams) {
2023
setter(lp, value);
2124
nativeView.setLayoutParams(lp);
2225
}
@@ -80,7 +83,11 @@ export class FlexboxLayout extends FlexboxLayoutBase {
8083
nativeView: org.nativescript.widgets.FlexboxLayout;
8184

8285
public createNativeView() {
83-
return new org.nativescript.widgets.FlexboxLayout(this._context);
86+
if (!widgetFlexboxLayout) {
87+
widgetFlexboxLayout = org.nativescript.widgets.FlexboxLayout;
88+
widgetLayoutParams = widgetFlexboxLayout.LayoutParams;
89+
}
90+
return new widgetFlexboxLayout(this._context);
8491
}
8592

8693
public disposeNativeView() {
@@ -127,29 +134,32 @@ export class FlexboxLayout extends FlexboxLayoutBase {
127134
super._updateNativeLayoutParams(child);
128135

129136
const lp = <org.nativescript.widgets.FlexboxLayout.LayoutParams>child.nativeView.getLayoutParams();
130-
lp.order = child.order;
131-
lp.flexGrow = child.flexGrow;
132-
lp.flexShrink = child.flexShrink;
133-
lp.wrapBefore = child.flexWrapBefore;
134-
lp.alignSelf = alignSelfMap[child.alignSelf];
137+
const style = child.style;
138+
lp.order = style.order;
139+
lp.flexGrow = style.flexGrow;
140+
lp.flexShrink = style.flexShrink;
141+
lp.wrapBefore = style.flexWrapBefore;
142+
lp.alignSelf = alignSelfMap[style.alignSelf];
135143
child.nativeView.setLayoutParams(lp);
136144
}
137145

138146
public _setChildMinWidthNative(child: View): void {
139147
child._setMinWidthNative(0);
140-
const lp = child.nativeView.getLayoutParams();
141-
if (lp instanceof org.nativescript.widgets.FlexboxLayout.LayoutParams) {
142-
lp.minWidth = Length.toDevicePixels(child.minWidth, 0);
143-
child.nativeView.setLayoutParams(lp);
148+
const nativeView = child.nativeView;
149+
const lp = nativeView.getLayoutParams();
150+
if (lp instanceof widgetLayoutParams) {
151+
lp.minWidth = Length.toDevicePixels(child.style.minWidth, 0);
152+
nativeView.setLayoutParams(lp);
144153
}
145154
}
146155

147156
public _setChildMinHeightNative(child: View): void {
148157
child._setMinHeightNative(0);
149-
const lp = child.nativeView.getLayoutParams();
150-
if (lp instanceof org.nativescript.widgets.FlexboxLayout.LayoutParams) {
151-
lp.minHeight = Length.toDevicePixels(child.minHeight, 0);
152-
child.nativeView.setLayoutParams(lp);
158+
const nativeView = child.nativeView;
159+
const lp = nativeView.getLayoutParams();
160+
if (lp instanceof widgetLayoutParams) {
161+
lp.minHeight = Length.toDevicePixels(child.style.minHeight, 0);
162+
nativeView.setLayoutParams(lp);
153163
}
154164
}
155165
}

0 commit comments

Comments
 (0)