Skip to content

Commit

Permalink
fix: resolved one TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillzyusko committed Sep 11, 2024
1 parent 8622670 commit 01162fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/libs/SuffixUkkonenTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]" 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[];
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -212,4 +214,4 @@ function testEmojis() {
return performanceProfile(searchString, 'smile');
}

export {makeTree, stringToArray, testEmojis};
export {makeTree, testEmojis};
8 changes: 2 additions & 6 deletions src/pages/ChatFinderPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -159,12 +159,8 @@ function ChatFinderPage({betas, isSearchingForReports, navigation}: ChatFinderPa
console.log(searchString.substring(0, 20));

Check failure on line 159 in src/pages/ChatFinderPage/index.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
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();
Expand Down

0 comments on commit 01162fe

Please sign in to comment.