fix: sort deps deterministically to prevent worst case scenario orderings#6
Merged
jmank88 merged 1 commit intoMay 11, 2026
Merged
Conversation
…s that could increase runtime drastically
jmank88
approved these changes
May 11, 2026
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.
transitiveReduction()mutatess.depsin place while the inner search readss.deps[child]for many child vertices. Which rows are already “thinned” depends on how far the outer loop has progressed. The outer loop used for m, deps := range s.deps, i.e. Go map iteration order, which is intentionally non-deterministic.On large inputs—especially a single stdin stream built from many go mod graph runs (e.g. nested modules concatenated by gomods graph | modgraph)—runtime varied wildly. In my case the process appeared to hang or take impractically long; the same graph logic under a stable outer order completed in reasonable time.
Change
Iterate outer modules with slices.Sorted(maps.Keys(s.deps)) so processing order is deterministic and tends to reduce high-fan-out modules in a stable way, shrinking adjacency lists before later BFS work reads them.