Use faster algorithm for topological sort#20790
Merged
Conversation
ilevkivskyi
approved these changes
Feb 12, 2026
Member
ilevkivskyi
left a comment
There was a problem hiding this comment.
Interesting. I guess this will also help with ordering large SCCs (like in case of torch) where this may be called multiple times.
| from mypy.errors import Errors | ||
| from mypy.fscache import FileSystemCache | ||
| from mypy.graph_utils import strongly_connected_components, topsort | ||
| from mypy.graph_utils import strongly_connected_components, topsort, topsort2 |
Member
There was a problem hiding this comment.
If this will be around for a while, we may choose a better name, otherwise this is fine.
Collaborator
Author
There was a problem hiding this comment.
|'m planning to rename the new one to topsort and drop the old one pretty soon, once I've measured the performance in our large codebase (probably within a week or so).
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
In a large codebase, up to 9% of CPU was used in
topsortwhen doing a small incremental run. This should make it significantly faster (I've verified that it's faster at least when using synthetic data).Use Kahn's algorithm, since it's O(V + E) rather than O(depth * V) for the original algorithm.
Description of the algorithm: https://www.geeksforgeeks.org/dsa/topological-sorting-indegree-based-solution/
perf_compare.pyshowed a small improvement in self check performance, but the difference is below the noise floor. This will likely mostly help with larger codebases.Keep the old
topsortfunction around for now, so that we can test that the new and old functions behave identically in tests. I'll remove the old one afterwards.I used coding agent assist for this, but I did the implementation in multiple small increments.