Use trie for removeStringLiteralsMatchedByTemplateLiterals - #3331
Use trie for removeStringLiteralsMatchedByTemplateLiterals#3331Sebastian "Sebbie" Silbermann (eps1lon) wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR ports the TypeScript change to speed up removeStringLiteralsMatchedByTemplateLiterals by indexing template-literal patterns by their leading text, reducing repeated full scans when pruning redundant string-literal constituents during union reduction.
Changes:
- Introduces an internal trie node type for indexing template-literal prefixes.
- Adds trie construction + lookup helpers to find a matching template literal efficiently.
- Refactors
removeStringLiteralsMatchedByTemplateLiteralsto use the trie (when beneficial) and to separately handle template literals vs string-mapping patterns.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/checker/types.go | Adds templateLiteralTrieNode type to support prefix trie indexing. |
| internal/checker/relater.go | Adds trie build + lookup helpers that leverage texts[0] as the prefix key for template-literal matching. |
| internal/checker/checker.go | Refactors string-literal pruning to use trie-based matching for template literals and preserves string-mapping handling. |
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. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
430ac1c to
d7118cd
Compare
c216139 to
b318e23
Compare
| } | ||
|
|
||
| func (c *Checker) buildTemplateLiteralTrieFromTypes(templateTypes []*Type) *templateLiteralTrieNode { | ||
| root := &templateLiteralTrieNode{} |
There was a problem hiding this comment.
For this, you will probably benefit from using core.Arena to avoid a bunch of small allocs of these nodes, given they're all generated once in this func. Can just write var arena core.Arena[templateLiteralTrieNode] and then do arena.New() instead of &templateLiteralTrieNode{}.
There was a problem hiding this comment.
Ugh, so, I never hit send on this :(
|
TypeScript Bot (@typescript-bot) perf test this faster |
|
Jake Bailey (@jakebailey) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Fixes microsoft/TypeScript#63342
Port of microsoft/TypeScript#63343
Fixes microsoft/TypeScript#63342
Actual implementation is vibe-coded with Claude Opus 4.6 (1M context)