Skip to content

Cache unchanged templates during IDE editing via TemplateContext#1093

Draft
johanrd wants to merge 1 commit intotyped-ember:mainfrom
johanrd:preprocess-caching
Draft

Cache unchanged templates during IDE editing via TemplateContext#1093
johanrd wants to merge 1 commit intotyped-ember:mainfrom
johanrd:preprocess-caching

Conversation

@johanrd
Copy link
Copy Markdown
Contributor

@johanrd johanrd commented Mar 16, 2026

  • Introduces TemplateContext, a per-template cache slot that persists across rewriteModule() calls
  • When a template is unchanged between keystrokes, both preprocess() and templateToTypescript() are skipped entirely
  • VirtualGtsCode holds a TemplateContext[] passed through the pipeline — one slot per template position, no unbounded growth
  • CLI path passes no contexts — zero overhead, no regression

Benchmarks

ts-template-imports-app test app — JS edit (template unchanged)

File Templates Lines Before After Speedup
Playground.gts 3 62 762µs 327µs 2.3x
Greeting.gts 2 23 215µs 138µs 1.6x
augmented.gts 1 15 81µs 51µs 1.6x
with-declaration-consumer.gts 1 19 129µs 43µs 3.0x
colocated/index.gts 1 14 99µs 68µs 1.5x

Speedup scales with template size. On a production app with larger templates on a M1 mac:

File Templates Template lines JS/TS lines Before After Speedup
a.gts 1 1335 539 287.7ms 5.7ms 50x
b.gts 1 510 147 33.1ms 1.5ms 22x
c.gts 1 325 764 18.6ms 5.0ms 4x
d.gts 16 446 440 13.6ms 4.9ms 3x

Memory

After 1000 keystrokes: 1 slot for a single-template file. No growth.

CLI (ember-tsc)

No TemplateContext is passed in the CLI path — all cache checks are skipped. No regression.

Test plan

  • 144/144 transform tests pass (140 existing + 4 new cache hit/miss tests)
  • Verify IDE responsiveness on a large .gts file
  • Verify ember-tsc CLI produces identical output

When editing a .gts file, templates that haven't changed now skip both
the @glimmer/syntax preprocess() parse and the full templateToTypescript()
transform. Each template gets a persistent TemplateContext slot (held by
VirtualGtsCode) that stores the last template string, AST, and transform
result — one slot per template position, no unbounded growth.

The CLI path is unaffected — no TemplateContext is passed, so all cache
checks are skipped.
@johanrd johanrd force-pushed the preprocess-caching branch from b482433 to 4450598 Compare March 16, 2026 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant