Use trie for removeStringLiteralsMatchedByTemplateLiterals
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
Is this different than https://github.com/microsoft/TypeScript/pull/59759?
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.
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?
Nice, a trie for this matching—smart and fast 🚀😄 Explore here →
Maintainer? Turn off weaves from non-maintainers → Carefully crafted by Subweave · 🧶 used ~96k LLM tokens