Skip to content

Instantly share code, notes, and snippets.

@Ashoat

Ashoat/.patch Secret

Created October 27, 2023 18:19
Show Gist options
  • Save Ashoat/fc1c91a61009de0e9959527454be8236 to your computer and use it in GitHub Desktop.
Save Ashoat/fc1c91a61009de0e9959527454be8236 to your computer and use it in GitHub Desktop.
diff --git a/lib/components/chat-mention-provider.react.js b/lib/components/chat-mention-provider.react.js
index 2daededbd..8059de95a 100644
--- a/lib/components/chat-mention-provider.react.js
+++ b/lib/components/chat-mention-provider.react.js
@@ -77,6 +77,7 @@ function getChatMentionCandidates(threadInfos: {
chatMentionCandidatesObj: ChatMentionCandidatesObj,
communityThreadIDForGenesisThreads: { +[id: string]: string },
} {
+ const start = Date.now();
const result = {};
const visitedGenesisThreads = new Set();
const communityThreadIDForGenesisThreads = {};
@@ -170,6 +171,7 @@ function getChatMentionCandidates(threadInfos: {
}
result[currentThreadCommunity][currentThreadID] = currentThreadInfo;
}
+ console.log(`getChatMentionCandidates took ${Date.now() - start}ms`);
return {
chatMentionCandidatesObj: result,
communityThreadIDForGenesisThreads,
@@ -210,25 +212,39 @@ function useChatMentionSearchIndex(
+[id: string]: SentencePrefixSearchIndex,
} {
return React.useMemo(() => {
+ const start = Date.now();
const result = {};
for (const communityThreadID in chatMentionCandidatesObj) {
const searchIndex = new SentencePrefixSearchIndex();
const searchIndexEntries = [];
+ const start2 = Date.now();
for (const threadID in chatMentionCandidatesObj[communityThreadID]) {
searchIndexEntries.push({
id: threadID,
uiName: chatMentionCandidatesObj[communityThreadID][threadID].uiName,
});
}
+ if (Date.now() - start2 >= 2) {
+ console.log(`community ${communityThreadID} took ${Date.now() - start2}ms to collect searchIndexEntries`);
+ }
// Sort the keys so that the order of the search result is consistent
+ const start3 = Date.now();
searchIndexEntries.sort(({ uiName: uiNameA }, { uiName: uiNameB }) =>
uiNameA.localeCompare(uiNameB),
);
+ if (Date.now() - start3 >= 2) {
+ console.log(`community ${communityThreadID} took ${Date.now() - start3}ms to sort searchIndexEntries`);
+ }
+ const start4 = Date.now();
for (const { id, uiName } of searchIndexEntries) {
searchIndex.addEntry(id, uiName);
}
+ if (Date.now() - start4 >= 2) {
+ console.log(`community ${communityThreadID} took ${Date.now() - start4}ms to add ${searchIndexEntries.length} entries into a RadixTree. ${searchIndex.getAndResetCounter()}ms was for tokenization`);
+ }
result[communityThreadID] = searchIndex;
}
+ console.log(`useChatMentionSearchIndex took ${Date.now() - start}ms. total of ${Object.keys(result).length} SearchIndexes`);
return result;
}, [chatMentionCandidatesObj]);
}
diff --git a/lib/shared/sentence-prefix-search-index.js b/lib/shared/sentence-prefix-search-index.js
index bc85bd7cf..ed6f75d6e 100644
--- a/lib/shared/sentence-prefix-search-index.js
+++ b/lib/shared/sentence-prefix-search-index.js
@@ -10,6 +10,7 @@ const tokenize = new Tokenizer().re(/\S+/);
class SentencePrefixSearchIndex extends SearchIndex {
entries: Set<string>;
+ counter: number = 0;
constructor() {
super(tokenize);
@@ -17,7 +18,13 @@ class SentencePrefixSearchIndex extends SearchIndex {
}
addEntry(id: string, rawText: string) {
+ const start = Date.now();
const keywords = this.tokenize(rawText);
+ //console.log(`${rawText} -> ${JSON.stringify(keywords)}`);
+ this.counter += Date.now() - start;
+ if (start - Date.now() >= 2) {
+ console.log(`tokenize() took ${Date.now() - start} for ${id} (${rawText})`);
+ }
for (const keyword of keywords) {
const value = rawText.slice(keyword.index).toLowerCase();
this.addAllPrefixes(id, value);
@@ -28,6 +35,12 @@ class SentencePrefixSearchIndex extends SearchIndex {
getSearchResults(query: string): string[] {
return this.radixTree.getAllMatchingPrefix(query.toLowerCase());
}
+
+ getAndResetCounter(): number {
+ const { counter } = this;
+ this.counter = 0;
+ return counter;
+ }
}
export default SentencePrefixSearchIndex;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment