-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Use a trie to quickly skip templates in union creation #59759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jakebailey
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
jakebailey:fun-literal-template-literal-thing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2590,3 +2590,96 @@ export function isNodeLikeSystem(): boolean { | |
&& !(process as any).browser | ||
&& typeof require !== "undefined"; | ||
} | ||
|
||
/** @internal */ | ||
export interface PrefixSuffixTrie<T extends {}> { | ||
iterateAllMatches(input: string): Iterable<T>; | ||
set(prefix: string, suffix: string, fn: (value: T | undefined) => T | undefined): void; | ||
hasAnyMatch(input: string): boolean; | ||
} | ||
|
||
/** @internal */ | ||
export function createPrefixSuffixTrie<T extends {}>(): PrefixSuffixTrie<T> { | ||
interface Trie<T extends {}> { | ||
children: Record<string, Trie<T>> | undefined; | ||
value: T | undefined; | ||
} | ||
|
||
function createTrie<T extends {}>(): Trie<T> { | ||
return { | ||
children: undefined, | ||
value: undefined, | ||
}; | ||
} | ||
|
||
const root = createTrie<Trie<T>>(); | ||
|
||
function* iterateAllMatches(input: string) { | ||
let node = root; | ||
|
||
if (node.value) { | ||
yield* iterateSuffix(node.value, 0); | ||
} | ||
|
||
for (let i = 0; i < input.length; i++) { | ||
const child = node.children?.[input[i]]; | ||
if (!child) break; | ||
if (child.value) { | ||
yield* iterateSuffix(child.value, i + 1); | ||
} | ||
node = child; | ||
} | ||
|
||
return; | ||
|
||
function* iterateSuffix(node: Trie<T>, start: number) { | ||
if (node.value) { | ||
yield node.value; | ||
} | ||
|
||
for (let i = input.length - 1; i >= start; i--) { | ||
const child = node.children?.[input[i]]; | ||
if (!child) break; | ||
if (child.value) { | ||
yield child.value; | ||
} | ||
node = child; | ||
} | ||
} | ||
} | ||
|
||
function set(prefix: string, suffix: string, fn: (value: T | undefined) => T | undefined) { | ||
let prefixNode = root; | ||
|
||
for (let i = 0; i < prefix.length; i++) { | ||
const char = prefix[i]; | ||
const children = prefixNode.children ??= {}; | ||
const child = children[char] ??= createTrie(); | ||
prefixNode = child; | ||
} | ||
|
||
let suffixNode = prefixNode.value ??= createTrie(); | ||
|
||
for (let i = suffix.length - 1; i >= 0; i--) { | ||
const char = suffix[i]; | ||
const children = suffixNode.children ??= {}; | ||
const child = children[char] ??= createTrie(); | ||
suffixNode = child; | ||
} | ||
|
||
suffixNode.value = fn(suffixNode.value); | ||
} | ||
|
||
function hasAnyMatch(input: string) { | ||
for (const _ of iterateAllMatches(input)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
Comment on lines
+2673
to
+2678
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused in this PR but would be used in #59058. |
||
|
||
return { | ||
iterateAllMatches, | ||
set, | ||
hasAnyMatch, | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An interesting thing about building
trie
s dynamically is that they can grow quite large in memory consumed rather quickly when you do something like this and always create newtrie
nodes for every new match state. When you're matching a whole dictionary, for example, this can quickly become untenably large. All is not lost, however - there are a large number of sub-trie
s that are identical (for example, the-es
suffix trie), and you can save quite a bit of space at the cost of some time by compressing the trie by unifying all those subtrees. So rather than having atrie
likeyou compress it like so
or, better yet,
Which, a quick search seems to indicate is referred to either as a "compressed trie" or just a "suffix tree". Extra memory efficiency, since JS is GC'd, can change the breakpoint where it's worth it to swap between the naive scan and the trie-based structure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought about compressing the trie into a proper radix tree, but wasn't sure if that would be worth it in the end (and the implementation does get more complicated). But I can give it a try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main problem is just that the edges being strings means that I don't know how to walk down them without iterating over each of them...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess radix tress ensure that the children are keyed on equal length prefixes, so that would work.
However, your example coming together like this I don't think works out for our use case here; otherwise as we walk down the graph, we'll merge together with template literals that don't actually match (because they came from another branch) and not reduce the number of inferences that much.
A proper radix tree doesn't converge like this so would at least still be optimal, I think, so I may attempt that if I have a change.