From ca6fa6d3819631b085daf31b0bc3614957d89f54 Mon Sep 17 00:00:00 2001 From: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> Date: Wed, 29 Jul 2026 14:14:49 +0300 Subject: [PATCH] cache findSheetByTitle lookups --- src/workspace.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/workspace.ts b/src/workspace.ts index 1791450..08198f8 100644 --- a/src/workspace.ts +++ b/src/workspace.ts @@ -28,6 +28,7 @@ export class Workspace { private cache = new Map(); private resolving: Set = new Set(); private parsedRefsCache = new Map(); + private titleCache: Map | null = null; constructor(private engine: SumEngine, private sheets: () => SheetSource[]) {} @@ -47,6 +48,7 @@ export class Workspace { /** Drop cached exports for a sheet and everything that transitively depends on it. */ invalidate(sheetId: string): void { + this.titleCache = null; const dirty = new Set([sheetId]); let changed = true; while (changed) { @@ -67,6 +69,7 @@ export class Workspace { } invalidateAll(): void { + this.titleCache = null; this.cache.clear(); this.parsedRefsCache.clear(); } @@ -105,7 +108,16 @@ export class Workspace { private findSheetByTitle(title: string): SheetSource | undefined { const needle = title.trim().toLowerCase(); - return this.sheets().find((s) => s.title.trim().toLowerCase() === needle); + if (this.titleCache === null) { + this.titleCache = new Map(); + for (const s of this.sheets()) { + const key = s.title.trim().toLowerCase(); + if (!this.titleCache.has(key)) { + this.titleCache.set(key, s); + } + } + } + return this.titleCache.get(needle); } private resolveXRef(sheetTitle: string, key: string): XRefResolution {