perf(trie): remove recursive substring traversal#780
Open
q1000treadz wants to merge 1 commit into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Triecurrently traverses strings recursively and creates a new substring at each character step withsubstring(1).This adds unnecessary work in hot paths and makes long inputs depend on JavaScript call stack depth.
Approach
Replace recursive per-character traversal with index-based iteration in:
addStringcontainsfindPrefixfindMatchesOnPathkeysWithPrefixnow uses iterative traversal only for locating the prefix node. The recursive subtree walk is intentionally preserved to avoid changing result ordering.getSize()is intentionally unchanged because it does not create substrings and is not part of the hot string traversal path.Compatibility
No public API changes.
Preserved behavior:
dictionaryremains an object created withObject.create(null)addString()still returns whether the word already existedfindPrefix()return values are unchangedfindMatchesOnPath()result order is unchangedkeysWithPrefix()result order is unchangedTests
Added regression tests for:
findPrefix()resultfindMatchesOnPath()result and orderVerified locally:
npm run build:testsnpm run lintBenchmarks
I compared the previous recursive implementation against the new iterative implementation locally.
The comparison checked:
build: inserting all words withaddStringcontains hit:contains()for words that exist in the triecontains miss:contains()for words that do not exist in the triefindPrefix: longest-prefix lookup withfindPrefix()addString,contains,findPrefix,findMatchesOnPath,keysWithPrefix, andgetSizeEnvironment:
Real word list
Synthetic datasets
The main improvement comes from avoiding recursive calls and avoiding intermediate substring allocation at each character step. The change also removes the risk of call stack overflow for very long strings.