cache referencedTitles per sheet in invalidate()'s fixed-point loop

invalidate() re-scans every non-dirty sheet's text with a regex on
every pass of its while loop, even though the text hasn't changed
between passes — cache the parsed refs per sheet id, keyed by text
identity, so repeated passes reuse the same result.
This commit is contained in:
Horizontal42 2026-07-08 14:10:36 +03:00
parent e774546818
commit 80bb94674d

View file

@ -26,6 +26,7 @@ function escapeRegExp(s: string): string {
export class Workspace {
private cache = new Map<string, SheetExports>();
private resolving: string[] = [];
private parsedRefsCache = new Map<string, { text: string; refs: string[] }>();
constructor(private engine: SumEngine, private sheets: () => SheetSource[]) {}
@ -47,7 +48,7 @@ export class Workspace {
changed = false;
for (const s of this.sheets()) {
if (dirty.has(s.id)) continue;
for (const title of this.referencedTitles(s.text)) {
for (const title of this.referencedTitles(s.text, s.id)) {
const target = this.findSheetByTitle(title);
if (target && dirty.has(target.id)) {
dirty.add(s.id);
@ -62,6 +63,7 @@ export class Workspace {
invalidateAll(): void {
this.cache.clear();
this.parsedRefsCache.clear();
}
/**
@ -83,9 +85,16 @@ export class Workspace {
return out;
}
private referencedTitles(text: string): string[] {
private referencedTitles(text: string, id?: string): string[] {
if (id) {
const cached = this.parsedRefsCache.get(id);
if (cached && cached.text === text) return cached.refs;
}
const out: string[] = [];
for (const m of text.matchAll(XREF_SCAN_RE)) out.push((m[1] ?? m[2]).trim());
if (id) {
this.parsedRefsCache.set(id, { text, refs: out });
}
return out;
}