Skip to content
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

fix(android): refactor to make less calls to native #9119

Merged

Conversation

@farfromrefug
Copy link
Collaborator

@farfromrefug farfromrefug commented Dec 30, 2020

This PR simplifies the android code a bit to make less calls to android native constants. Thus less JNI bridge calls =>faster

@cla-bot cla-bot bot added the cla: yes label Dec 30, 2020
packages/core/ui/core/view/index.android.ts Show resolved Hide resolved
@@ -860,30 +863,32 @@ export class View extends ViewCommon {
[horizontalAlignmentProperty.setNative](value: HorizontalAlignment) {
const nativeView = this.nativeViewProtected;
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
const gravity = lp.gravity;
const weight = lp.weight;
Copy link
Contributor

@NathanaelA NathanaelA Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would eliminate these two extra variables. And revert the rest of the statements back to using lp.gravity and lp.weight as all you have done is added additional memory usage (which has to be GC'd) for no purpose and now have mixed values throughout below. You assign lp.gravity from gravity on line 872. This just confuses the source for people who later read it.

Copy link
Collaborator Author

@farfromrefug farfromrefug Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I would not. I forgot a if lp.gravity which makes 2 calls to native accesser. Need to fix this. The idea is to make sure we don't access those native variables multiple times => multiple jni calls

@@ -898,30 +903,32 @@ export class View extends ViewCommon {
[verticalAlignmentProperty.setNative](value: VerticalAlignment) {
const nativeView = this.nativeViewProtected;
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
const gravity = lp.gravity;
const height = lp.height;
Copy link
Contributor

@NathanaelA NathanaelA Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

// Set only if params gravity exists.
if (lp.gravity !== undefined) {
switch (value) {
case 'left':
lp.gravity = android.view.Gravity.LEFT | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
if (lp.weight < 0) {
lp.gravity = 3 | (gravity & VERTICAL_GRAVITY_MASK); // android.view.Gravity.LEFT
Copy link
Contributor

@NathanaelA NathanaelA Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is 3, 1, 5, 7. Definitely would be better to create the const at the top so that the code is easier to read.

Copy link
Collaborator Author

@farfromrefug farfromrefug Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember i copied the way it was done somewhere else. And I didn't want to unecessarly create global module variables.
But sure I ll change it

@NathanWalker NathanWalker added this to the 8.1 milestone Jul 12, 2021
@NathanWalker NathanWalker changed the base branch from master to release/8.1.0 Aug 11, 2021
@NathanWalker NathanWalker merged commit bca4d95 into NativeScript:release/8.1.0 Aug 11, 2021
3 checks passed
@farfromrefug farfromrefug deleted the faster_android_alignments branch Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants