mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 21:31:59 +03:00
perf: optimize referencedTitles text matching in workspace
Replaces regex matchAll with manual indexOf scanning for parsing references, significantly improving text scanning performance. Uses sticky regex fallback for bare identifiers. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
37cfd2144b
commit
d8d2903670
1 changed files with 23 additions and 1 deletions
|
|
@ -19,6 +19,7 @@ interface SheetExports {
|
||||||
}
|
}
|
||||||
|
|
||||||
const XREF_SCAN_RE = /@(?:\[([^\]]+)\]|([\p{L}_][\p{L}\d_]*))\./gu;
|
const XREF_SCAN_RE = /@(?:\[([^\]]+)\]|([\p{L}_][\p{L}\d_]*))\./gu;
|
||||||
|
const XREF_BARE_RE_STICKY = /@([\p{L}_][\p{L}\d_]*)\./guy;
|
||||||
|
|
||||||
function escapeRegExp(s: string): string {
|
function escapeRegExp(s: string): string {
|
||||||
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||||
|
|
@ -104,7 +105,28 @@ export class Workspace {
|
||||||
if (cached && cached.text === text) return cached.refs;
|
if (cached && cached.text === text) return cached.refs;
|
||||||
}
|
}
|
||||||
const out: string[] = [];
|
const out: string[] = [];
|
||||||
for (const m of text.matchAll(XREF_SCAN_RE)) out.push((m[1] ?? m[2]).trim());
|
let i = 0;
|
||||||
|
while ((i = text.indexOf("@", i)) !== -1) {
|
||||||
|
if (i + 1 >= text.length) break;
|
||||||
|
|
||||||
|
if (text[i + 1] === "[") {
|
||||||
|
const endBracket = text.indexOf("]", i + 2);
|
||||||
|
if (endBracket !== -1 && endBracket + 1 < text.length && text[endBracket + 1] === ".") {
|
||||||
|
out.push(text.substring(i + 2, endBracket).trim());
|
||||||
|
i = endBracket + 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
XREF_BARE_RE_STICKY.lastIndex = i;
|
||||||
|
const m = XREF_BARE_RE_STICKY.exec(text);
|
||||||
|
if (m !== null) {
|
||||||
|
out.push(m[1]); // no trim needed for bare identifier regex match
|
||||||
|
i = XREF_BARE_RE_STICKY.lastIndex;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
if (id) {
|
if (id) {
|
||||||
this.parsedRefsCache.set(id, { text, refs: out });
|
this.parsedRefsCache.set(id, { text, refs: out });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue