Skip to content

fix: sort deps deterministically to prevent worst case scenario orderings#6

Merged
jmank88 merged 1 commit into
jmank88:mainfrom
ecPablo:ecpablo/sort-deps-deterministically
May 11, 2026
Merged

fix: sort deps deterministically to prevent worst case scenario orderings#6
jmank88 merged 1 commit into
jmank88:mainfrom
ecPablo:ecpablo/sort-deps-deterministically

Conversation

@ecPablo

@ecPablo ecPablo commented May 11, 2026

Copy link
Copy Markdown
Contributor

transitiveReduction() mutates s.deps in place while the inner search reads s.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.

@jmank88 jmank88 marked this pull request as ready for review May 11, 2026 11:05
@jmank88 jmank88 merged commit f3c6f98 into jmank88:main May 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants