[MachineOutliner] Efficient Implementation of MachineOutliner::findCandidates() #88988
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.
We reduce the complexity of the main loop of findCandidates() method from O(n^2) to O(n log n).
We sort RS.StartIndices in SuffixTree and change replace find_if function with a simple check.
For each SuffixTree::RepeatedSubstring RS, the time complexity to find a set of candidates that do not overlap with each other is O(n^2) where n is the number of occurrence of this repeated substring (i.e., the size of RS.StartIndices). This is due to the use of the find_if method which has complexity O(n). The quadratic runtime becomes a problem when n gets larger. For clang, with [3], the maximum n goes from 12k to 100k, and the time to complete the main loop in MachineOutliner::findCandidates() goes from 17 seconds to 120 seconds.
To improve the runtime, we implement a more efficient algorithm with complexity O(n log n), using the fact that once RS.StartIndices is sorted, find_if can be achieved with an O(1) check. The O(n log n) complexity comes from the sorting. For clang, with [1], the time to complete the loop is reduced to only 28 seconds.