⚡ Bolt: cache referencedTitles regex execution

Introduces `parsedRefsCache` in `Workspace` to prevent repeatedly executing the heavy `XREF_SCAN_RE` regex across the same unchanged sheet text during cache invalidations.

Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
google-labs-jules[bot] 2026-07-08 09:14:43 +00:00
parent 1b61ec35cc
commit b3c07c8244
2 changed files with 14 additions and 2 deletions

3
.jules/bolt.md Normal file
View file

@ -0,0 +1,3 @@
## 2025-02-18 - Repeated Regex Execution Caching
**Learning:** In recursive or looping traversal like graph cycle detection (e.g. invalidation of multiple sheets), running heavy Regex (`matchAll`) repeatedly on the exact same text is extremely slow and compounds exponentially.
**Action:** Introduce text-based short-circuiting caches (e.g., matching the original `text` property against the cached payload string) where text extraction logic like Regex or parsing runs purely on deterministic inputs.

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;
}