perf(app-store): dedupe location options with a Set instead of a linear scan - #30020
perf(app-store): dedupe location options with a Set instead of a linear scan#30020Chirag6722 wants to merge 1 commit into
Conversation
…ar scan getLocationGroupedOptions deduped each location option by scanning the category array with .find(), then rebuilt that array with a spread. Both are O(n) per credential, so building the options was quadratic in the number of installed apps. Tracks the values already collected per category in a Set and pushes onto the existing array, making each insert O(1). Output is unchanged: the first option for a value still wins and insertion order is preserved. Closes calcom#29959
|
Welcome to Cal.diy, @Chirag6722! Thanks for opening this pull request. A few things to keep in mind:
A maintainer will review your PR soon. Thanks for contributing! |
|
Marked ready for review — the change is complete, not a work in progress. I opened it as a draft because On that: the only red check is the external-contributor gate, which fails in 3s and skips everything downstream. So For what it is worth on review effort: the diff is +12/-4 in one file, and the verification is in the description — 400 randomised inputs comparing the old and new dedupe by deep equality with 0 mismatches, plus timings showing 339.7ms to 2.1ms at n=8000. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthrough
Merge Risk: ⚪ Minimal · up to This change improves location-option deduplication performance while preserving output order and first-match behavior; no actionable merge-blocking risk remains beyond normal checks and review. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
What is the problem
getLocationGroupedOptionsdeduped location options with a linear scan, and then rebuilt the array it was scanning:Both halves are O(n) per credential, so the loop is quadratic in the number of installed apps. #29959 reports the
.find(); the spread is a second, independent O(n) that also copies the whole array on every insert.The fix
Keep a
Setof the values already collected per category andpushonto the existing array, so each insert is O(1).Semantics are deliberately unchanged: the first option for a given value still wins, and insertion order is preserved.
Verification
The requirement in the issue is that output stays identical, so I checked that directly rather than reasoning about it. Both algorithms were run over 400 randomised inputs (up to 60 entries, 4 categories, 12 distinct values, so duplicates are frequent) and compared by deep equality:
Timing the two on a single category shows the change in growth:
The old path grows faster than linearly as
ndoubles; the new one does not. At 8000 options that is 339.7 ms to 2.1 ms.Those numbers come from a standalone harness reproducing both variants of the dedupe, not from the app, because reaching this function requires Prisma plus
getEnabledAppsFromCredentials. I have not added a Vitest case for the same reason: there is currently no test coveringgetLocationGroupedOptions, and the mocking needed to reach it is considerably larger than this diff. Happy to add one if you would rather the dedupe contract were pinned in the suite.Scope
One file, +12/-4.
defaultLocations.forEachjust below uses the same spread-to-append pattern, and I left it alone on purpose: it iterates a fixed, small list rather than user credentials, so it is not quadratic in anything an account controls, and changing it would add diff without addressing the issue. Say the word if you would like it converted for consistency.Closes #29959