[Run] Replace WindowWalker's brute-force fuzzy matching algorithm with optimal DP solution #44551
+92
−64
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.
Summary of the Pull Request
Window Walker's fuzzy string matching algorithm exhibits exponential memory usage and execution time when given inputs containing repeated characters or phrases. When a user has several windows open with long titles (such as browser windows), it is straightforward to trigger a pathological case which uses up gigabytes of memory and freezes the UI. This is exacerbated by Run's lack of thread pruning, meaning work triggered by older keystrokes consumes CPU and memory until completion.
PR Checklist
Detailed Description of the Pull Request / Additional comments
The existing algorithm in
FuzzyMatching.csis greedy, creating all possible matching combinations of the search string within the candidate via itsGetAllMatchIndexes()method. After this, it selects the best match and discards the others. This may be considered reasonable for small search strings, but it causes a combinatorial explosion when there are multiple possible matches where characters or substrings repeat, even when the search string is small.The current brute-force algorithm has time complexity of O(n * m * C(n,m)) where C(n,m) = n!/(m!(n-m)!) and space complexity of O(C(n,m) * m) because it stores all possible match combinations before choosing the best.
For example, matching
"eeee"in"eeeeeeee"creates C(8,4) = 70 match combinations, which stores 70 lists with 4 integers each, plus overhead from the LINQ-based list copying and appending:Each potential sub-match may be recalculated many times.
Window Walker queries across all window titles, so this problem will be magnified if the search text happens to match multiple titles and/or if a search string containing a single repeated character is used. For browser windows, where titles may be long, this is especially problematic, and similarly for Explorer windows with longer paths.
Proposed solution
The solution presented here is to use a dynamic programming algorithm which finds the optimal match directly without generating all possibilities.
In terms of complexity, the new algorithm benefits from a single pass through its DP table and only has to store two integer arrays which are sized proportionally to the search and candidate text string lengths; so O(n * m) for both time and space, i.e. polynomial instead of exponential.
Scoring is equivalent between the old and new algorithms, based strictly on the minimum match span within the candidate string.
Implementation notes
The new algorithm tracks the best start index for matches ending at each position, eliminating the need to store all possible paths. By storing the "latest best match so far" as you scan through the search text, you are guaranteed to minimise the span length. To recreate the best match, a separate table of parent indexes is kept and iterated backwards once the DP step is complete. Reversing this provides you with the same result (or equivalent if there are multiple best matches) as the original algorithm.
For this "minimum-span" fuzzy matching method, this should be optimal as it only scans once and storage is proportional to the search and candidate strings only.
Benchmarks
A verification and benchmarking suite is here: https://github.com/daverayment/WindowWalkerBench
Results from comparing the old and new algorithms are here: https://docs.google.com/spreadsheets/d/1eXmmnN2eI3774QxXXyx1Dv4SKu78U96q28GYnpHT0_8/edit?usp=sharing
(Where "Old_Explosion" is "e" repeated 9 times. Times in nanoseconds or one millionth of a millisecond.)
It is worth noting that the results show a single string match. So matching "eeeeee" against a 99-character string took 25 MB of memory and 71 milliseconds to compute. For the new algorithm, this is reduced down to <5KB and 0.002 milliseconds. Even for a three-character repetition, the new algorithm is >150x faster with <1% of the allocations.
Real world example
Before (results still pending after more than a minute):

After (instantaneous results):

Validation Steps Performed
The verification tests in the benchmark project pass, with results identical to the original across a number of test cases, including the pathological cases identified earlier and edge cases such as single-character searches.
All unit tests under
Wox.Test, including all 38FuzzyMatcherTestentries still pass.