perf: create report -> transactions map to optimize performance#55297
perf: create report -> transactions map to optimize performance#55297carlosmiceli merged 15 commits intoExpensify:mainfrom
Conversation
|
@rayane-djouah Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
|
@rayane-djouah this one's ready! |
Reviewer Checklist
Screenshots/VideosAndroid: NativeScreen.Recording.2025-01-22.at.8.48.29.PM.movAndroid: mWeb ChromeScreen.Recording.2025-01-22.at.8.49.50.PM.moviOS: NativeSimulator.Screen.Recording.-.iPhone.15.Pro.Max.-.2025-01-22.at.20.46.03.mp4iOS: mWeb SafariSimulator.Screen.Recording.-.iPhone.15.Pro.Max.-.2025-01-22.at.20.43.02.mp4MacOS: Chrome / SafariScreen.Recording.2025-01-22.at.8.11.42.PM.movScreen.Recording.2025-01-22.at.7.51.47.PM.movMacOS: DesktopScreen.Recording.2025-01-22.at.7.29.29.PM.mov |
src/libs/TransactionUtils/index.ts
Outdated
| if (transactions) { | ||
| return Object.values(transactions).filter((transaction): transaction is Transaction => !!transaction && transaction.reportID === reportID); | ||
| } |
There was a problem hiding this comment.
Can we optimize this for components when transactions are provided also? can we consider using useOnyx with a selector in components to fetch only the relevant transactions for a specific reportID?
App/src/components/MoneyReportHeader.tsx
Line 118 in 5d25d69
App/src/pages/ReportDetailsPage.tsx
Line 111 in 5d25d69
This will reduce computations when transactions change in Onyx.
Pseudocode:
function transactionsByReportIDSelector(transactions: OnyxCollection<Transaction>, reportID: string): Transaction[] {
return Object.values(transactions).filter((transaction): transaction is Transaction => !!transaction && transaction.reportID === reportID);
}
const [reportTransactions, reportTransactionsMetadata] = useOnyx({
key: ONYXKEYS.COLLECTION.TRANSACTION,
selector: (transactions) => transactionsByReportIDSelector(transactions, reportID),
});There was a problem hiding this comment.
Thanks @rayane-djouah for bringing this, I think we can do even better 😀 Looks like all this work is redundant because in each scenario we pass in the whole initial list of transactions. This means we're not filtering any subset (2nd parameter), but essentially re-do the same work multiple time.
I've also stumbled upon this PR which introduced the same improvement (but based on Array, not Set). I think we can unify all usages of getAllReportTransactions to just getReportTransactions that is there in ReportUtils (instead of TransactionUtils). Implementation with the 2nd parameter is not being used anywhere correctly.
You're also right about moving to useOnyx wherever possible! I'll update the PR today and test this on my end.
src/components/MoneyReportHeader.tsx
Outdated
| const hasOnlyPendingTransactions = allTransactions.length > 0 && allTransactions.every((t) => TransactionUtils.isExpensifyCardTransaction(t) && TransactionUtils.isPending(t)); | ||
| const transactionIDs = allTransactions.map((t) => t.transactionID); | ||
| const hasOnlyPendingTransactions = | ||
| transactions?.some((t) => { |
There was a problem hiding this comment.
@rayane-djouah we have to triple check if this is logically the same as before - should now bail out earlier though because of the some() loop whenever possible.
|
I have refactored this one to rely on an existing map in |
|
@rayane-djouah we'll get conflicts resolved on Monday morning & ensure it still works as good as initially (given the amount of updates I had to push). I'll let you know once it's done! |
|
resolved conficts ✅ |
|
also fixed the lint-changed failing job |
| const hasOnlyPendingTransactions = useMemo(() => { | ||
| return !transactions?.some((t) => { | ||
| const isTransactionPending = isExpensifyCardTransaction(t) && isPending(t); | ||
| return !isTransactionPending; | ||
| }); | ||
| }, [transactions]); |
There was a problem hiding this comment.
Both .some() and .every() short-circuit: .some() stops at the first truthy value, and .every() stops at the first falsy value. There's no performance difference between them, as both will exit after checking the same number of elements for the same dataset.
Let's use .every() for better readability:
| const hasOnlyPendingTransactions = useMemo(() => { | |
| return !transactions?.some((t) => { | |
| const isTransactionPending = isExpensifyCardTransaction(t) && isPending(t); | |
| return !isTransactionPending; | |
| }); | |
| }, [transactions]); | |
| const hasOnlyPendingTransactions = useMemo(() => { | |
| return transactions?.every((t) => isExpensifyCardTransaction(t) && isPending(t)); | |
| }, [transactions]); |
There was a problem hiding this comment.
ah you're right @rayane-djouah, totally forgot about that! Thanks for catching it.
src/components/MoneyReportHeader.tsx
Outdated
| const hasOnlyPendingTransactions = allTransactions.length > 0 && allTransactions.every((t) => isExpensifyCardTransaction(t) && isPending(t)); | ||
| const transactionIDs = allTransactions.map((t) => t.transactionID) ?? []; | ||
| const hasOnlyPendingTransactions = useMemo(() => { | ||
| return transactions?.every((t) => isExpensifyCardTransaction(t) && isPending(t)); |
There was a problem hiding this comment.
| return transactions?.every((t) => isExpensifyCardTransaction(t) && isPending(t)); | |
| return transactions?.every((t) => isExpensifyCardTransaction(t) && isPending(t)) ?? false; |
src/components/MoneyReportHeader.tsx
Outdated
| const isAnyTransactionOnHold = hasHeldExpensesReportUtils(moneyRequestReport?.reportID); | ||
| const displayedAmount = isAnyTransactionOnHold && canAllowSettlement && hasValidNonHeldAmount ? nonHeldAmount : formattedAmount; | ||
| const isMoreContentShown = shouldShowNextStep || shouldShowStatusBar || (shouldShowAnyButton && shouldUseNarrowLayout); | ||
| const isMoreContentShown = shouldShowNextStep ?? shouldShowStatusBar ?? (shouldShowAnyButton && shouldUseNarrowLayout); |
There was a problem hiding this comment.
| const isMoreContentShown = shouldShowNextStep ?? shouldShowStatusBar ?? (shouldShowAnyButton && shouldUseNarrowLayout); | |
| const isMoreContentShown = shouldShowNextStep || shouldShowStatusBar || (shouldShowAnyButton && shouldUseNarrowLayout); |
src/components/MoneyReportHeader.tsx
Outdated
| const hasOnlyPendingTransactions = allTransactions.length > 0 && allTransactions.every((t) => isExpensifyCardTransaction(t) && isPending(t)); | ||
| const transactionIDs = allTransactions.map((t) => t.transactionID) ?? []; | ||
| const hasOnlyPendingTransactions = useMemo(() => { | ||
| return transactions?.every((t) => isExpensifyCardTransaction(t) && isPending(t)) ?? false; |
There was a problem hiding this comment.
.every return true for empty array, we need to check the length before
| return transactions?.every((t) => isExpensifyCardTransaction(t) && isPending(t)) ?? false; | |
| return !!transactions && transactions.length > 0 && transactions.every((t) => isExpensifyCardTransaction(t) && isPending(t)); |
|
@carlosmiceli all yours! |
|
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
|
🚀 Deployed to staging by https://github.com/carlosmiceli in version: 9.0.89-0 🚀
|
|
🚀 Deployed to production by https://github.com/yuwenmemon in version: 9.0.89-8 🚀
|
Explanation of Change
This change reduces the
getOrderedReportIDsexecution time on accounts with a lot of reports and transactions by creating a map of reports and their corresponding transactions instead of filtering transactions on each report. It speeds up the TTI of the app and fixes the issue linked below.Fixed Issues
$ #54802
PROPOSAL: #54802 (comment)
Tests
Offline tests
n/a
QA Steps
// TODO: These must be filled out, or the issue title must include "[No QA]."
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectiontoggleReportand notonIconClick)myBool && <MyComponent />.src/languages/*files and using the translation methodSTYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.ScrollViewcomponent to make it scrollable when more elements are added to the page.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Android: Native
android.mov
Android: mWeb Chrome
android-web.mov
iOS: Native
ios.mov
iOS: mWeb Safari
ios-web.mov
MacOS: Chrome / Safari
web.mov
MacOS: Desktop
desktop.mov