From 01162fee73c8147556d65e4b5990f0a25d855d7e Mon Sep 17 00:00:00 2001 From: kirillzyusko Date: Wed, 11 Sep 2024 17:39:36 +0200 Subject: [PATCH] fix: resolved one TODO --- src/libs/SuffixUkkonenTree.ts | 14 ++++++++------ src/pages/ChatFinderPage/index.tsx | 8 ++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/libs/SuffixUkkonenTree.ts b/src/libs/SuffixUkkonenTree.ts index 217588fae5fa..52a7ebb2b7d9 100644 --- a/src/libs/SuffixUkkonenTree.ts +++ b/src/libs/SuffixUkkonenTree.ts @@ -28,15 +28,16 @@ function stringToArray(input: string) { } /** - * Makes a tree from an input string, which has been converted by {@link stringToArray}. + * Makes a tree from an input string * **Important:** As we only support an alphabet of 26 characters, the input string should only contain characters from a-z. * Thus, all input data must be cleaned before being passed to this function. * If you then use this tree for search you should clean your search input as well (so that a search query of "testuser@myEmail.com" becomes "testusermyemailcom"). */ -function makeTree(a: number[]) { +function makeTree(searchString: string) { + const a = stringToArray(searchString); const N = 25000; // TODO: i reduced this number from 1_000_000 down to this, for faster performance - however its possible that it needs to be bigger for larger search strings const start = performance.now(); - const t = Array.from({length: N}, () => Array(ALPHABET_SIZE).fill(-1)) as number[][]; + const t = Array.from({length: N}, () => Array(ALPHABET_SIZE).fill(-1) as number[]); const l = Array(N).fill(0) as number[]; const r = Array(N).fill(0) as number[]; const p = Array(N).fill(0) as number[]; @@ -183,8 +184,9 @@ function makeTree(a: number[]) { } function performanceProfile(input: string, search = 'sasha') { - // TODO: For emojis we could precalculate the stringToArray or even the makeTree function during build time using a babel plugin - const {build, findSubstring} = makeTree(stringToArray(input)); + // TODO: For emojis we could precalculate the makeTree function during build time using a babel plugin + // maybe babel plugin that just precalculates the result of function execution (so that it can be generic purpose plugin) + const {build, findSubstring} = makeTree(input); const buildStart = performance.now(); build(); @@ -212,4 +214,4 @@ function testEmojis() { return performanceProfile(searchString, 'smile'); } -export {makeTree, stringToArray, testEmojis}; +export {makeTree, testEmojis}; diff --git a/src/pages/ChatFinderPage/index.tsx b/src/pages/ChatFinderPage/index.tsx index cbdf5ec739c1..f7860d4cc1e3 100644 --- a/src/pages/ChatFinderPage/index.tsx +++ b/src/pages/ChatFinderPage/index.tsx @@ -18,7 +18,7 @@ import type {RootStackParamList} from '@libs/Navigation/types'; import * as OptionsListUtils from '@libs/OptionsListUtils'; import Performance from '@libs/Performance'; import type {OptionData} from '@libs/ReportUtils'; -import {makeTree, stringToArray} from '@libs/SuffixUkkonenTree'; +import {makeTree} from '@libs/SuffixUkkonenTree'; import * as Report from '@userActions/Report'; import Timing from '@userActions/Timing'; import CONST from '@src/CONST'; @@ -159,12 +159,8 @@ function ChatFinderPage({betas, isSearchingForReports, navigation}: ChatFinderPa console.log(searchString.substring(0, 20)); console.log('building search strings', performance.now() - start); - // TODO: stringToArray is probably also an implementation detail we want to hide from the developer start = performance.now(); - const numbers = stringToArray(searchString); - console.log('stringToArray', performance.now() - start); - start = performance.now(); - const tree = makeTree(numbers); + const tree = makeTree(searchString); console.log('makeTree', performance.now() - start); start = performance.now(); tree.build();