TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Use trie for removeStringLiteralsMatchedByTemplateLiterals

Open eps1lon opened this issue 8 months ago • 4 comments

Started out as a guess that template literal checking should be done using a trie which led to removeStringLiteralsMatchedByTemplateLiterals being the major contributor in CPU traces.

Actual implementation is vibe-coded with Claude Opus 4.6 (1M context)

Tested this in our internal apps (~90s tsc duration before). Ideally we'd run this against TypeScript's extensive benchmarks since this a space vs runtime tradeoff so we might want to opt out of building the trie for small unions.

Optimize removeStringLiteralsMatchedByTemplateLiterals by building a prefix trie from TemplateLiteralType patterns and using O(L) trie traversal per string literal instead of O(m) linear scan across all templates. StringMappingType templates (which cannot be trie-indexed) are checked separately.

Fixes #63342

eps1lon avatar Apr 02 '26 20:04 eps1lon

Is this different than https://github.com/microsoft/TypeScript/pull/59759?

jakebailey avatar Apr 02 '26 20:04 jakebailey

Looks like the same at a glance. Yours is already doing some size estimation it seems. I haven't accounted for opting out of trie-based search for small types.

eps1lon avatar Apr 02 '26 20:04 eps1lon

The trie is built only from texts[0] (initial prefix) of each template literal, but a string literal could match a later segment — e.g., "foobar" matching `foo${number}bar` where "bar" is in texts[1]. Since findMatchingTemplateLiteralInTrie only traverses the prefix path, this case would fall through to linear search but still may not be found correctly. Does the trie need to index non-prefix segments too?

afurm avatar Apr 13 '26 07:04 afurm

Nice, a trie for this matching—smart and fast 🚀😄 Explore here →

Subweave map of microsoft/typescript#63343

Maintainer? Turn off weaves from non-maintainers → Carefully crafted by Subweave · 🧶 used ~96k LLM tokens

subweave-bot avatar Jul 07 '26 11:07 subweave-bot