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

[HOLD for payment 2024-09-23] Performance Improvement: Update formatjs libraries and upstream/patch date-fns-tz #47988

Closed
hurali97 opened this issue Aug 26, 2024 · 16 comments
Assignees
Labels
AutoAssignerNewDotQuality Used to assign quality issues to engineers Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2

Comments

@hurali97
Copy link
Contributor

Problem

While we were analysing the latest traces from Jason, we realised that there's substantial amount of time spent in formatjs and date-fns-tz. We also double-checked this with our heavy accounts (~15k reports) and we got the same results. Following is how things look like on baseline:

Baseline

Screenshot 2024-08-26 at 12 47 42 PM

snapshot from profile

Screenshot 2024-08-26 at 12 41 31 PM

Solution:

While analysing the traces, we curated a list of places that can be improved in formatjs and date-fns-tz. Two noticeable items are BestFitMatcher from formatjs and getDateTimeFormat from date-fns-tz. Now, since these are third party libraries, we can bump them to the latest versions and see if they are already fixing performance related issues. We compared the code for BestFitMatcher from the currently installed version formatjs/intl-locale@3.3.0 with formatjs/intl-locale@4.0.0 and there was a significant refactor of this file.

So it only made sense to bump formatjs libraries to the latest to get the performance improvements out of the box. For date-fns-tz there was no significant updates to getDateTimeFormat function, so to apply the improvements we have two options:

  • Patch date-fns-tz
  • Upstream date-fns-tz by creating a PR to it's repository

However, we don't know how long it would take for option 2 as it depends on the reviewer of that repo. We can maybe move forward by mixing both options and removing the patch, once our PR is merged and available on NPM.

The improvement for getDateTImeFormat is pretty simple. We have Intl.DateTimeFormat being re-initialised every time getDateTimeFormat is invoked. Since testDateFormatted is a static variable, we can move it outside of the function.

var dtfCache = {};

// Moved this out from `getDateTimeFormat`
    var testDateFormatted = new Intl.DateTimeFormat('en-US', {
      hour12: false,
      timeZone: 'America/New_York',
      year: 'numeric',
      month: 'numeric',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    }).format(new Date('2014-06-25T04:00:00.123Z'));
    var hourCycleSupported = testDateFormatted === '06/25/2014, 00:00:00' || testDateFormatted === '‎06‎/‎25‎/‎2014‎ ‎00‎:‎00‎:‎00';
    

function getDateTimeFormat(timeZone) {
 ...someImplementation
  return dtfCache[timeZone];
}

Below is how things look like after formatjs version bumps and date-fns-tz patch:

Screenshot 2024-08-26 at 1 03 06 PM

snapshot from profile

Screenshot 2024-08-26 at 12 42 15 PM


To summarise;

  • Bump formatjs libraries to the latest
  • Patch or Upstream date-fns-tz with the changes described above

This comment was marked as outdated.

@flyinghead

This comment was marked as outdated.

This comment was marked as outdated.

@mountiny mountiny self-assigned this Aug 26, 2024
@mountiny mountiny added AutoAssignerNewDotQuality Used to assign quality issues to engineers Bug Something is broken. Auto assigns a BugZero manager. labels Aug 26, 2024
Copy link

melvin-bot bot commented Aug 26, 2024

Current assignee @mountiny is eligible for the AutoAssignerNewDotQuality assigner, not assigning anyone new.

Copy link

melvin-bot bot commented Aug 26, 2024

Triggered auto assignment to @strepanier03 (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.

@melvin-bot melvin-bot bot added the Daily KSv2 label Aug 26, 2024
@Expensify Expensify deleted a comment Aug 26, 2024
@roryabraham
Copy link
Contributor

we can maybe move forward by mixing both options and removing the patch, once our PR is merged and available on NPM

This is generally my preferred approach. Report the issue with a minimal reproduction, and create a PR to fix it upstream. If you don't receive any feedback, proceed with a patch and create an issue to eventually remove the patch once the upstream fix is merged. If you do get feedback and the maintainer doesn't want to merge your fix for some reason, re-evaluate and open up a conversation with the team.

@roryabraham
Copy link
Contributor

looks like some good finds @hurali97 👍🏼

@melvin-bot melvin-bot bot added Reviewing Has a PR in review Weekly KSv2 and removed Daily KSv2 labels Aug 27, 2024
@hurali97
Copy link
Contributor Author

PR for date-fns-tz library is here. Now, we can wait for two days and if there's no update from the reviewer, we will go ahead with creating the patch in Expensify.

@roryabraham
Copy link
Contributor

sounds great 👍🏼

@Ollyws
Copy link
Contributor

Ollyws commented Sep 21, 2024

Could someone process payment for my review of #48067, thanks!

@mountiny mountiny added Daily KSv2 Awaiting Payment Auto-added when associated PR is deployed to production and removed Reviewing Has a PR in review Weekly KSv2 labels Sep 22, 2024
@mountiny mountiny changed the title Performance Improvement: Update formatjs libraries and upstream/patch date-fns-tz [HOLD for payment 2024-09-23] Performance Improvement: Update formatjs libraries and upstream/patch date-fns-tz Sep 22, 2024
@mountiny
Copy link
Contributor

@Ollyws sorry, the automation failed here.

$250 to @Ollyws for their review, no regression tests required for this task cc @strepanier03

@melvin-bot melvin-bot bot removed the Overdue label Sep 22, 2024
@mountiny
Copy link
Contributor

Noting we do not want to close this but make it monthly so we can track of the upstream PR

@Ollyws
Copy link
Contributor

Ollyws commented Sep 22, 2024

Requested in ND.

@mountiny mountiny added Monthly KSv2 and removed Daily KSv2 labels Sep 22, 2024
Copy link

melvin-bot bot commented Sep 23, 2024

Skipping the payment summary for this issue since all the assignees are employees or vendors. If this is incorrect, please manually add the payment summary SO.

@garrettmknight
Copy link
Contributor

$250 approved for @Ollyws

@melvin-bot melvin-bot bot added Daily KSv2 Overdue and removed Monthly KSv2 labels Sep 28, 2024
@melvin-bot melvin-bot bot removed the Overdue label Oct 1, 2024
@mountiny
Copy link
Contributor

mountiny commented Oct 1, 2024

I think we can close now then

@mountiny mountiny closed this as completed Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoAssignerNewDotQuality Used to assign quality issues to engineers Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. Daily KSv2
Projects
None yet
Development

No branches or pull requests

8 participants
@garrettmknight @strepanier03 @Ollyws @flyinghead @mountiny @hurali97 @roryabraham and others