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

perf: Implement filtering in search page #37909

Merged

Conversation

TMisiukiewicz
Copy link
Contributor

@TMisiukiewicz TMisiukiewicz commented Mar 7, 2024

Details

Currently when using search input, the option list is generated from scratch. With this change, list is created only once and it is filtered when value of search input change. Once we implement this in all search pages, we will be able to remove searchText property from options, which will have positive impact on execution time of getOptions (around 25% improvement). From our measurements, filtering is ~12x faster than creating an option list from scratch.

Fixed Issues

$ #37619
PROPOSAL:

Tests

  1. Open Search Page
  2. Type something in the search input
  3. Verify results on the list are filtered to match the searched phase
  4. Compare the results and verify they are displayed in the same order as on production app
  5. Clear the input and type some random characters
  6. Verify "No results found" message is visible
  • Verify that no errors appear in the JS console

Offline tests

  1. Open Search Page
  2. Type something in the search input
  3. Verify results on the list are filtered to match the searched phase
  4. Compare the results and verify they are displayed in the same order as on production app
  5. Clear the input and type some random characters
  6. Verify "No results found" message is visible

QA Steps

  1. Open Search Page
  2. Type something in the search input
  3. Verify results on the list are filtered to match the searched phase
  4. Compare the results and verify they are displayed in the same order as on production app
  5. Clear the input and type some random characters
  6. Verify "No results found" message is visible
  • 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 the expected offline behavior in the Offline steps 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 tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
    • 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 the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • 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 is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
    • 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
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • 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(theme.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • 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.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test 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


// The regex below is used to remove dots only from the local part of the user email (local-part@domain)
// so that we can match emails that have dots without explicitly writing the dots (e.g: fistlast@domain will match first.last@domain)
const emailRegex = /\.(?=[^\s@]*@)/g;
Copy link
Contributor

Choose a reason for hiding this comment

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

probably you can initialize it outside of the function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as it is not used anywhere else, I'd leave it here so it's clear where and why this regex belongs to

Copy link
Contributor

Choose a reason for hiding this comment

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

rather than using a regex to replace dots in emails, can we just have filterArrayByMatch ignore dots, semicolons, dashes, etc... by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm, I think it might negatively affect the performance, because we would have to call it for each value under a specified keys. With current approach, we can limit those checks only to the keys we need.

Copy link
Contributor

Choose a reason for hiding this comment

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

would be nice to leave a comment that this is a slim version of what's available in match-sorter

} else if (keyCopy.includes('.')) {
return getNestedValues<T>(keyCopy, item);
} else {
value = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

probably you can do return []; here, instead of assign null and then check for null and return

@roryabraham roryabraham self-requested a review March 11, 2024 17:02
@TMisiukiewicz TMisiukiewicz marked this pull request as ready for review March 12, 2024 09:20
@TMisiukiewicz TMisiukiewicz requested a review from a team as a code owner March 12, 2024 09:20
@melvin-bot melvin-bot bot requested review from DylanDylann and removed request for a team March 12, 2024 09:20
@TMisiukiewicz
Copy link
Contributor Author

@roryabraham I've addressed all of your comments. I have reduced the filtering implementation by another ~50 LOC. Looking forward to get your opinion on current state of the PR

@TMisiukiewicz
Copy link
Contributor Author

I think there is some issue with reassure job, could anyone re-run it?

Copy link
Contributor

@roryabraham roryabraham left a comment

Choose a reason for hiding this comment

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

As we iterate on this, more simplifications are becoming apparent:

  • we aren't actually using different match thresholds at all
  • we certainly aren't using different match thresholds on a per-key basis
  • all "keys" are currently functions, but typed to be keys or functions that return either a string or array of string

Here's a diff that accomplishes the same thing we have here, more simply:

diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts
index 33820da834..530e7e2b6a 100644
--- a/src/libs/OptionsListUtils.ts
+++ b/src/libs/OptionsListUtils.ts
@@ -2246,39 +2246,35 @@ function filterOptions(options: Options, searchInputValue: string): Options {
         return keys;
     };
     const matchResults = searchTerms.reduceRight((items, term) => {
-        const recentReports = filterArrayByMatch(items.recentReports, term, {
-            keys: [
-                (item) => {
-                    let keys: string[] = [];
-                    if (item.text) {
-                        keys.push(item.text);
-                    }
+        const recentReports = filterArrayByMatch(items.recentReports, term, (item) => {
+            let values: string[] = [];
+            if (item.text) {
+                values.push(item.text);
+            }
 
-                    if (item.login) {
-                        keys.push(item.login);
-                        keys.push(item.login.replace(emailRegex, ''));
-                    }
+            if (item.login) {
+                values.push(item.login);
+                values.push(item.login.replace(emailRegex, ''));
+            }
 
-                    if (item.isThread) {
-                        if (item.alternateText) {
-                            keys.push(item.alternateText);
-                        }
-                        keys = keys.concat(getParticipantsLoginsArray(item));
-                    } else if (!!item.isChatRoom || !!item.isPolicyExpenseChat) {
-                        if (item.subtitle) {
-                            keys.push(item.subtitle);
-                        }
-                    } else {
-                        keys = keys.concat(getParticipantsLoginsArray(item));
-                    }
+            if (item.isThread) {
+                if (item.alternateText) {
+                    values.push(item.alternateText);
+                }
+                values = values.concat(getParticipantsLoginsArray(item));
+            } else if (!!item.isChatRoom || !!item.isPolicyExpenseChat) {
+                if (item.subtitle) {
+                    values.push(item.subtitle);
+                }
+            } else {
+                values = values.concat(getParticipantsLoginsArray(item));
+            }
 
-                    return uniqFast(keys);
-                },
-            ],
-        });
-        const personalDetails = filterArrayByMatch(items.personalDetails, term, {
-            keys: [(item) => item.participantsList?.[0]?.displayName ?? '', 'login', (item) => item.login?.replace(emailRegex, '') ?? ''],
+            return uniqFast(values);
         });
+        const personalDetails = filterArrayByMatch(items.personalDetails, term, (item) =>
+            uniqFast([item.participantsList?.[0]?.displayName ?? '', item.login?.replace(emailRegex, '') ?? '']),
+        );
 
         return {
             recentReports: recentReports ?? [],
diff --git a/src/libs/filterArrayByMatch.ts b/src/libs/filterArrayByMatch.ts
index f481a08fe0..c87ad8fac1 100644
--- a/src/libs/filterArrayByMatch.ts
+++ b/src/libs/filterArrayByMatch.ts
@@ -18,59 +18,6 @@ const MATCH_RANK = {
 
 type Ranking = ValueOf<typeof MATCH_RANK>;
 
-type RankingInfo = {
-    rankedValue: string;
-    rank: Ranking;
-    keyIndex: number;
-    keyThreshold?: Ranking;
-};
-
-type ValueGetterKey<T> = (item: T) => string | string[];
-
-type KeyAttributesOptions<T> = {
-    key: string | ValueGetterKey<T>;
-    threshold?: Ranking;
-};
-
-type KeyOption<T> = KeyAttributesOptions<T> | ValueGetterKey<T> | string;
-
-type Options<T = unknown> = {
-    keys: ReadonlyArray<KeyOption<T>>;
-    threshold?: Ranking;
-};
-type IndexableByString = Record<string, unknown>;
-
-/**
- * Gets value for key in item at arbitrarily nested keypath
- * @param item - the item
- * @param key - the potentially nested keypath or property callback
- * @returns an array containing the value(s) at the nested keypath
- */
-function getItemValues<T>(item: T, key: KeyOption<T>): string[] {
-    if (!item) {
-        return [];
-    }
-
-    const resolvedKey = typeof key === 'object' ? key.key : key;
-    const value = typeof resolvedKey === 'function' ? resolvedKey(item) : (item as IndexableByString)[resolvedKey];
-
-    if (!value) {
-        return [];
-    }
-
-    return Array.isArray(value) ? value.map(String) : [String(value)];
-}
-
-/**
- * Gets all the values for the given keys in the given item and returns an array of those values
- * @param item - the item from which the values will be retrieved
- * @param keys - the keys to use to retrieve the values
- * @return objects with {itemValue}
- */
-function getAllValuesToRank<T>(item: T, keys: ReadonlyArray<KeyOption<T>>): string[] {
-    return keys.flatMap((key) => getItemValues(item, key));
-}
-
 /**
  * Gives a rankings score based on how well the two strings match.
  * @param testString - the string to test against
@@ -140,56 +87,31 @@ function getMatchRanking(testString: string, stringToRank: string): Ranking {
     return ranking as Ranking;
 }
 
-/**
- * Gets the highest ranking for value for the given item based on its values for the given keys
- * @param item - the item to rank
- * @param keys - the keys to get values from the item for the ranking
- * @param value - the value to rank against
- * @param options - options to control the ranking
- * @returns the highest ranking
- */
-function getHighestRanking<T>(item: T, keys: ReadonlyArray<KeyOption<T>>, value: string, options: Options<T>): RankingInfo {
-    const valuesToRank = getAllValuesToRank(item, keys);
-    return valuesToRank.reduce(
-        (acc, itemValue, index) => {
-            const ranking = acc;
-            const newRank = getMatchRanking(itemValue, value);
-            let newRankedValue = ranking.rankedValue;
-
-            if (newRank > ranking.rank) {
-                ranking.rank = newRank;
-                ranking.keyIndex = index;
-                ranking.keyThreshold = options.threshold;
-                newRankedValue = itemValue;
-            }
-            return {rankedValue: newRankedValue, rank: ranking.rank, keyIndex: ranking.keyIndex, keyThreshold: ranking.keyThreshold};
-        },
-        {
-            rankedValue: item as unknown as string,
-            rank: MATCH_RANK.NO_MATCH as Ranking,
-            keyIndex: -1,
-            keyThreshold: options.threshold,
-        },
-    );
-}
-
 /**
  * Takes an array of items and a value and returns a new array with the items that match the given value
  * @param items - the items to filter
  * @param searchValue - the value to use for ranking
- * @param options - options to configure
+ * @param extractRankableValuesFromItem - an array of functions
  * @returns the new filtered array
  */
-function filterArrayByMatch<T = string>(items: readonly T[], searchValue: string, options: Options<T>): T[] {
-    const {keys, threshold = MATCH_RANK.MATCHES} = options;
+function filterArrayByMatch<T = string>(items: readonly T[], searchValue: string, extractRankableValuesFromItem: (item: T) => string[]): T[] {
+    const filteredItems = [];
+    for (const item of items) {
+        const valuesToRank = extractRankableValuesFromItem(item);
+        let itemRank: Ranking = MATCH_RANK.NO_MATCH;
+        for (const value of valuesToRank) {
+            const rank = getMatchRanking(value, searchValue);
+            if (rank > itemRank) {
+                itemRank = rank;
+            }
+        }
 
-    return items
-        .map((item) => ({...getHighestRanking(item, keys, searchValue, options), item}))
-        .filter(({rank, keyThreshold = threshold}) => rank >= Math.max(keyThreshold, threshold + 1))
-        .map(({item}) => item);
+        if (itemRank >= MATCH_RANK.MATCHES) {
+            filteredItems.push(item);
+        }
+    }
+    return filteredItems;
 }
 
 export default filterArrayByMatch;
 export {MATCH_RANK};
-
-export type {Options, KeyAttributesOptions, KeyOption, RankingInfo, ValueGetterKey};

type IndexableByString = Record<string, unknown>;

/**
* Gets value for key in item at arbitrarily nested keypath
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment description doesn't seem correct anymore?

@roryabraham roryabraham merged commit 8ae5491 into Expensify:main Apr 10, 2024
15 checks passed
@OSBotify
Copy link
Contributor

✋ 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

🚀 Deployed to staging by https://github.com/roryabraham in version: 1.4.63-0 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/mountiny in version: 1.4.63-21 🚀

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

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

Successfully merging this pull request may close these issues.

7 participants