mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 01:02:01 +03:00
perf(workspace): optimize sheet lookup by title to O(1)
Migrated `findSheetByTitle` from an O(N) array search using `.find()` to an O(1) Map lookup. The cache `titleMap` is lazily evaluated and properly cleared during `invalidate` and `invalidateAll`. To ensure functional correctness, Map initialization employs `!this.titleMap.has(key)` to preserve the original first-match-wins semantics of `.find()`. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
984275711a
commit
2477b18759
1 changed files with 12 additions and 1 deletions
|
|
@ -27,6 +27,7 @@ export class Workspace {
|
||||||
private cache = new Map<string, SheetExports>();
|
private cache = new Map<string, SheetExports>();
|
||||||
private resolving: Set<string> = new Set();
|
private resolving: Set<string> = new Set();
|
||||||
private parsedRefsCache = new Map<string, { text: string; refs: string[] }>();
|
private parsedRefsCache = new Map<string, { text: string; refs: string[] }>();
|
||||||
|
private titleMap: Map<string, SheetSource> | null = null;
|
||||||
|
|
||||||
constructor(private engine: SumEngine, private sheets: () => SheetSource[]) {}
|
constructor(private engine: SumEngine, private sheets: () => SheetSource[]) {}
|
||||||
|
|
||||||
|
|
@ -64,6 +65,7 @@ export class Workspace {
|
||||||
invalidateAll(): void {
|
invalidateAll(): void {
|
||||||
this.cache.clear();
|
this.cache.clear();
|
||||||
this.parsedRefsCache.clear();
|
this.parsedRefsCache.clear();
|
||||||
|
this.titleMap = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -99,8 +101,17 @@ export class Workspace {
|
||||||
}
|
}
|
||||||
|
|
||||||
private findSheetByTitle(title: string): SheetSource | undefined {
|
private findSheetByTitle(title: string): SheetSource | undefined {
|
||||||
|
if (!this.titleMap) {
|
||||||
|
this.titleMap = new Map();
|
||||||
|
for (const s of this.sheets()) {
|
||||||
|
const key = s.title.trim().toLowerCase();
|
||||||
|
if (!this.titleMap.has(key)) {
|
||||||
|
this.titleMap.set(key, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
const needle = title.trim().toLowerCase();
|
const needle = title.trim().toLowerCase();
|
||||||
return this.sheets().find((s) => s.title.trim().toLowerCase() === needle);
|
return this.titleMap.get(needle);
|
||||||
}
|
}
|
||||||
|
|
||||||
private resolveXRef(sheetTitle: string, key: string): XRefResolution {
|
private resolveXRef(sheetTitle: string, key: string): XRefResolution {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue