mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 02:12:00 +03:00
perf(search): optimize full-text search performance
Replaced the highly allocative String.prototype.split('\\n') strategy in searchAllSheets with a zero-allocation regex scanner and manual newline counting. This eliminates significant memory overhead and drastically improves full-text search speed in large workspaces.
Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
37cfd2144b
commit
af94861e18
1 changed files with 35 additions and 11 deletions
|
|
@ -33,6 +33,10 @@ export interface SearchController {
|
|||
const $ = <T extends HTMLElement>(sel: string): T =>
|
||||
document.querySelector(sel) as T;
|
||||
|
||||
function escapeRegExp(string: string): string {
|
||||
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
||||
}
|
||||
|
||||
export function parseResultQuery(
|
||||
engine: SumEngine,
|
||||
q: string,
|
||||
|
|
@ -95,20 +99,40 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
|
|||
}
|
||||
return hits;
|
||||
}
|
||||
const ql = q.toLowerCase();
|
||||
const hits: SearchHit[] = [];
|
||||
const regex = new RegExp(escapeRegExp(q), "gi");
|
||||
|
||||
for (const doc of docs) {
|
||||
const lines = doc.text.split("\n");
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
if (lines[i].toLowerCase().includes(ql)) {
|
||||
const text = doc.text;
|
||||
regex.lastIndex = 0;
|
||||
|
||||
let lastNlIndex = -1;
|
||||
let currentLineNum = 1;
|
||||
|
||||
let match;
|
||||
while ((match = regex.exec(text)) !== null) {
|
||||
while (true) {
|
||||
const nextNl = text.indexOf("\n", lastNlIndex + 1);
|
||||
if (nextNl === -1 || nextNl >= match.index) {
|
||||
break;
|
||||
}
|
||||
currentLineNum++;
|
||||
lastNlIndex = nextNl;
|
||||
}
|
||||
|
||||
const lineStart = lastNlIndex + 1;
|
||||
let lineEnd = text.indexOf("\n", match.index);
|
||||
if (lineEnd === -1) lineEnd = text.length;
|
||||
|
||||
hits.push({
|
||||
docId: doc.id,
|
||||
docTitle: doc.title,
|
||||
line: i + 1,
|
||||
text: lines[i],
|
||||
line: currentLineNum,
|
||||
text: text.substring(lineStart, lineEnd),
|
||||
});
|
||||
if (hits.length >= 200) return hits;
|
||||
}
|
||||
|
||||
regex.lastIndex = lineEnd;
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue