Skip to content

Improve the performance of report name generation#12399

Merged
Beamanator merged 13 commits intomainfrom
tgolen-performant-report-name-generation
Nov 4, 2022
Merged

Improve the performance of report name generation#12399
Beamanator merged 13 commits intomainfrom
tgolen-performant-report-name-generation

Conversation

@tgolen
Copy link
Contributor

@tgolen tgolen commented Nov 2, 2022

This speeds up the report name generation by about 200%. I tested this on an account with 4,000 reports. Before the change, it took 30ms to generate all the report names. After this change, it only takes 10ms.

Fixed Issues

$ #11609

Tests

  1. Have an account with several chats with other users
  2. Some of the users should have first and last names, while others shouldn't
  3. Verify in the LHN that the report names look correct. For people with names, the names should be shown, for people without names, their email/phone should be shown
  • Verify that no errors appear in the JS console

QA Steps

Same as the tests above

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

PR Reviewer Checklist

The reviewer will copy/paste it into a new comment and complete it after the author checklist is completed

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.
### Screenshots/Videos
Web

2022-11-02_13-39-12

Mobile Web - Chrome

2022-11-02_13-40-37

Mobile Web - Safari

2022-11-02_13-40-54

Desktop

2022-11-02_13-40-46

iOS

2022-11-02_14-04-30

Android

2022-11-02_14-01-26

@tgolen tgolen requested a review from a team as a code owner November 2, 2022 20:08
@tgolen tgolen self-assigned this Nov 2, 2022
@melvin-bot melvin-bot bot requested review from Beamanator and eVoloshchak and removed request for a team November 2, 2022 20:08
@tgolen tgolen marked this pull request as draft November 2, 2022 21:09
@tgolen
Copy link
Contributor Author

tgolen commented Nov 2, 2022

I ran into some really fundamental problems with the optionlistutils, so I am putting this back into draft mode to see if I can figure it out.

@tgolen tgolen marked this pull request as ready for review November 2, 2022 21:40
@tgolen
Copy link
Contributor Author

tgolen commented Nov 2, 2022

OK, I figured it out and this is good to go again.

@Beamanator
Copy link
Contributor

Durn conflicts :'(

Beamanator
Beamanator previously approved these changes Nov 3, 2022
Copy link
Contributor

@Beamanator Beamanator left a comment

Choose a reason for hiding this comment

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

Overall code looking great, had only a few minor comments that are mainly NAB - will test now

}
reportName = ReportUtils.getReportName(report, policies);
} else {
reportName = ReportUtils.getDisplayNameForParticipant(logins[0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just wondering, why use logins[0] here instead of personalDetail.login like the following 2 lines?

}

/**
* Gets the personal details for a login by looking in the allPersonalDetails from Onyx. If it doesn't exist in Onyx, then a default object is constructed.
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB

Suggested change
* Gets the personal details for a login by looking in the allPersonalDetails from Onyx. If it doesn't exist in Onyx, then a default object is constructed.
* Gets the personal details for a login by looking in the allPersonalDetails Onyx key. If it doesn't exist in Onyx, then a default object is constructed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see where you're going with the suggestion, but allPersonalDetails isn't actually an Onyx key. If you want the comment to reference the actual Onyx key, then it should say something like:

by looking in the ONYXKEYS.PERSONAL_DETAILS Onyx key.

The comment is currently referencing the local variable that the data from the Onyx key is stored in, so I think the comment is correct as originally authored.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok definitely fair points! I'm still not a huge fan of the way the first sentence is worded, so how about this instead:

Suggested change
* Gets the personal details for a login by looking in the allPersonalDetails from Onyx. If it doesn't exist in Onyx, then a default object is constructed.
* Gets the personal details for a login by looking in the allPersonalDetails data from Onyx. If it doesn't exist in Onyx, then a default object is constructed.

Mainly I don't like the allPersonalDetails bit, am I being too picky? Yes, maybe :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I've updated this with a bit of a different suggestion and being slightly more verbose.

Comment on lines +103 to +106
// Normally, the spread operator would be used here to clone the report and prevent the need to reassign the params.
// However, this code needs to be very performant to handle thousands of reports, so in the interest of speed, we're just going to disable this lint rule and add
// the reportDisplayName property to the report object directly.
// eslint-disable-next-line no-param-reassign
Copy link
Contributor

Choose a reason for hiding this comment

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

Nifty 👍 I can't think of any downside to this, worst case is reportDisplayName stays on the report object even when it's not needed, but I can't see how that would cause any problems 👍

Copy link
Member

Choose a reason for hiding this comment

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

Here we are 😁 This tricked us that we were using report.reportDisplayName (or report.displayName) across the App thinking it's a natural prop. This backfired once we started passing cloned reports to this function. Cloned reports are due to the use of withOnyx selector #21406

Copy link
Contributor

Choose a reason for hiding this comment

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

I strongly disagree with allowing mutations into our code, it creates hard to debug bugs and is hard to track the states of objects.

Copy link
Contributor

Choose a reason for hiding this comment

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

Now there is a second property getting mutated below this one:

// eslint-disable-next-line no-param-reassign
report.iouReportAmount = ReportUtils.getMoneyRequestTotal(report, allReportsDict);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm fine with finding a way to remove the mutations as long as the code remains performant.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not totally sure but I think we can leave the mutation now since we are mutating a clone. I don't see much benefit in cloning a clone.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess that makes it a bit better because we are only mutating the copy we pass down this tree, right?

const reportActionsSelector = (reportActions) =>
reportActions &&
_.map(reportActions, (reportAction) => ({
errors: lodashGet(reportAction, 'errors', []),
message: [
{
moderationDecision: {decision: lodashGet(reportAction, 'message[0].moderationDecision.decision')},
},
],
}));

Still a bad practice in react to mutate props in general, you have to follow up from where the prop is coming to verify that this is not causing more side effects and it is also hard to assure that things are getting rendered when they should down the line.

I'm fine with finding a way to remove the mutations as long as the code remains performant.

That is fair, do you know how to reproduce the performance issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The best way to ensure it doesn't regress on performance is to add some timers around this code, and then time it before/after making the change.

@Beamanator
Copy link
Contributor

Actually will probably wait to test till conflicts are resolved, so I don't have to retest :D

Copy link
Contributor Author

@tgolen tgolen left a comment

Choose a reason for hiding this comment

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

Updated with conflicts resolved!

}

/**
* Gets the personal details for a login by looking in the allPersonalDetails from Onyx. If it doesn't exist in Onyx, then a default object is constructed.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see where you're going with the suggestion, but allPersonalDetails isn't actually an Onyx key. If you want the comment to reference the actual Onyx key, then it should say something like:

by looking in the ONYXKEYS.PERSONAL_DETAILS Onyx key.

The comment is currently referencing the local variable that the data from the Onyx key is stored in, so I think the comment is correct as originally authored.

@eVoloshchak
Copy link
Contributor

Oops, just noticed this PR
I don't have the bandwidth right now, could you unassign me please?

@tgolen tgolen removed the request for review from eVoloshchak November 3, 2022 13:54
@Beamanator
Copy link
Contributor

Beamanator commented Nov 3, 2022

Shucksadoodle, I'm not able to test this on all platforms today, but most likely will be able to tomorrow 🙏 (Tested on web and so far that looks perfecto)

@Beamanator
Copy link
Contributor

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.
### Screenshots/Videos
Web Screen Shot 2022-11-04 at 10 49 03 AM
Mobile Web - Chrome

Screen Shot 2022-11-04 at 10 54 20 AM

Mobile Web - Safari

Screen Shot 2022-11-04 at 11 03 32 AM

Desktop Screen Shot 2022-11-04 at 11 04 29 AM
iOS

Screen Shot 2022-11-04 at 11 03 14 AM

Android

Screen Shot 2022-11-04 at 10 52 46 AM

Copy link
Contributor

@Beamanator Beamanator left a comment

Choose a reason for hiding this comment

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

:shipit:

@Beamanator Beamanator merged commit e434efa into main Nov 4, 2022
@Beamanator Beamanator deleted the tgolen-performant-report-name-generation branch November 4, 2022 09:07
@melvin-bot melvin-bot bot added the Emergency label Nov 4, 2022
@melvin-bot
Copy link

melvin-bot bot commented Nov 4, 2022

@Beamanator looks like this was merged without the checklist test passing. Please add a note explaining why this was done and remove the Emergency label if this is not an emergency.

@Beamanator
Copy link
Contributor

99.9% sure I saw the green checkmark indicating all tests were passing 🙃

@OSBotify
Copy link
Contributor

OSBotify commented Nov 4, 2022

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

OSBotify commented Nov 4, 2022

🚀 Deployed to staging by @Beamanator in version: 1.2.24-0 🚀

platform result
🤖 android 🤖 failure ❌
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

OSBotify commented Nov 8, 2022

🚀 Deployed to production by @yuwenmemon in version: 1.2.24-4 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 success ✅

1 similar comment
@OSBotify
Copy link
Contributor

OSBotify commented Nov 8, 2022

🚀 Deployed to production by @yuwenmemon in version: 1.2.24-4 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

OSBotify commented Nov 8, 2022

🚀 Deployed to production by @yuwenmemon in version: 1.2.24-4 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@marcaaron
Copy link
Contributor

This PR has been linked to a regression reported here: #12487

The fix was kind of simple and the mistake we made was that we changed a method signature and didn't update a usage.

One thing we can learn from this is to be hyper vigilant about method signature changes and always double check that any usages of a function are updated.

We also would have caught this automatically if we used some kind of type checking like TypeScript.

@Beamanator
Copy link
Contributor

Good catch @marcaaron , definitely good idea to be hyper vigilant about method signature changes, even when reviewing 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants