-
Notifications
You must be signed in to change notification settings - Fork 3.2k
perf: create report -> transactions map to optimize performance #55297
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
perf: create report -> transactions map to optimize performance #55297
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
?
const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION); |
const allTransactions = useMemo(() => getAllReportTransactions(iouReportID, transactions), [iouReportID, transactions]); |
App/src/components/MoneyReportHeader.tsx
Line 118 in 5d25d69
const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION); |
App/src/pages/ReportDetailsPage.tsx
Line 111 in 5d25d69
const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION); |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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 |
src/components/MoneyReportHeader.tsx
Outdated
const hasOnlyPendingTransactions = useMemo(() => { | ||
return !transactions?.some((t) => { | ||
const isTransactionPending = isExpensifyCardTransaction(t) && isPending(t); | ||
return !isTransactionPending; | ||
}); | ||
}, [transactions]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done ✅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return transactions?.every((t) => isExpensifyCardTransaction(t) && isPending(t)); | |
return transactions?.every((t) => isExpensifyCardTransaction(t) && isPending(t)) ?? false; |
src/components/MoneyReportHeader.tsx
Outdated
@@ -203,7 +205,7 @@ function MoneyReportHeader({policy, report: moneyRequestReport, transactionThrea | |||
const {nonHeldAmount, fullAmount, hasValidNonHeldAmount} = getNonHeldAndFullAmount(moneyRequestReport, shouldShowPayButton); | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM and tests well
@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
getOrderedReportIDs
execution 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 Issues
section aboveTests
sectionOffline steps
sectionQA steps
sectiontoggleReport
and notonIconClick
)myBool && <MyComponent />
.src/languages/*
files and using the translation methodSTYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)Design
label and/or tagged@Expensify/design
so the design team can review the changes.ScrollView
component to make it scrollable when more elements are added to the page.main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.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