Skip to content

Commit 5b9de07

Browse files
committed
perf(rendering): cache include source text by path (#1)
- Add include source cache read once then reused per path - Clear include cache alongside compiled cache on invalidate - Replace per-render readTextFileSync include read with cache lookup
1 parent 148a5e5 commit 5b9de07

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/core/Rendering.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class Rendering {
1313
readonly #directory: string
1414
/** Compiled template cache by path */
1515
readonly #cache = new Map<string, Types.CompileResult>()
16+
/** Cached include source text by path */
17+
readonly #includeCache = new Map<string, string>()
1618
/** Optional event emitter for render events */
1719
readonly #emit: Types.EventFn | null
1820

@@ -26,7 +28,7 @@ export class Rendering {
2628
this.#directory = options.directory ?? './views'
2729
this.#emit = emit
2830
this.#dve = new DVE({
29-
resolveInclude: (path) => Deno.readTextFileSync(this.#resolvePath(path)),
31+
resolveInclude: (path) => this.#resolveInclude(path),
3032
...(options.maxIterations !== undefined && { maxIterations: options.maxIterations }),
3133
...(options.maxRenderIterations !== undefined && {
3234
maxRenderIterations: options.maxRenderIterations
@@ -47,7 +49,9 @@ export class Rendering {
4749
* @param template - Template name to invalidate
4850
*/
4951
invalidate(template: string): void {
50-
this.#cache.delete(this.#resolvePath(template))
52+
const path = this.#resolvePath(template)
53+
this.#cache.delete(path)
54+
this.#includeCache.delete(path)
5155
if (this.#emit !== null) {
5256
this.#emit(Core.Observability.internalEvent('view:invalidated', { paths: [template] }))
5357
}
@@ -123,6 +127,23 @@ export class Rendering {
123127
return compiled
124128
}
125129

130+
/**
131+
* Resolve include source with caching.
132+
* @description Reads include text once then reuses cache.
133+
* @param template - Include template name to resolve
134+
* @returns Raw include template source text
135+
*/
136+
#resolveInclude(template: string): string {
137+
const path = this.#resolvePath(template)
138+
const cached = this.#includeCache.get(path)
139+
if (cached !== undefined) {
140+
return cached
141+
}
142+
const source = Deno.readTextFileSync(path)
143+
this.#includeCache.set(path, source)
144+
return source
145+
}
146+
126147
/**
127148
* Resolve template into file path.
128149
* @description Normalizes path and appends DVE extension.

0 commit comments

Comments
 (0)