perf: avoid redundant work in Graph.transitive_closure - #1015
Open
hneiva wants to merge 1 commit into
Open
Conversation
The adjacency map is built over every edge before the traversal starts, which an empty node set never needs. Return early instead: on a 20k-node, 2M-edge graph an empty closure drops from ~1.3s to microseconds. Resolve `reverse` to a pair of tuple indices once rather than branching on it per edge in both loops, worth 1.1-1.4x on sparse graphs. Accept a frozenset in the input assert, since Graph.nodes is itself a frozenset and g.transitive_closure(g.nodes) previously tripped it. The four worked examples in the docstring had forward and reverse swapped. They have been wrong since 2eec861 imported the module in 2019, were never covered by a test, and contradict the prose above them, which was correct. Add coverage for the diamond, frozenset and unknown-node paths.
Merging this PR will improve performance by 36.14%
Performance Changes
Tip Curious why this is faster? Comment Comparing |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1015 +/- ##
==========================================
+ Coverage 77.47% 77.50% +0.03%
==========================================
Files 132 132
Lines 12552 12561 +9
Branches 1506 1507 +1
==========================================
+ Hits 9725 9736 +11
+ Misses 2439 2438 -1
+ Partials 388 387 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
The adjacency map is built over every edge before the traversal starts, which an empty node set never needs. Return early instead: on a 20k-node, 2M-edge graph an empty closure drops from ~1.3s to microseconds.
Resolve
reverseto a pair of tuple indices once rather than branching on it per edge in both loops, worth 1.1-1.4x on sparse graphs. Accept a frozenset in the input assert, since Graph.nodes is itself a frozenset and g.transitive_closure(g.nodes) previously tripped it.The four worked examples in the docstring had forward and reverse swapped. They have been wrong since 2eec861 imported the module in 2019, were never covered by a test, and contradict the prose above them, which was correct.
Add coverage for the diamond, frozenset and unknown-node paths.