New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix various bugs in matrix graph #427
Merged
Merged
Conversation
This file contains 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
- Extension function for directed graphs did not copy the edges - Off by one in with_capacity - Check if the lazy adjacency matrix contains node before accesing
Avoids unnecessary quadratic memory waste. Dynamic expansions continue rounding to the next power of two.
Speeds up edge inserting benchmarks by ~35%
Overlapping slices still use the iterative method.
Here is the benchmark diff for the unsafe copying vs the iterative version. I think I'll merge that change here.
|
teuron
pushed a commit
to teuron/petgraph
that referenced
this pull request
Oct 9, 2022
* Fix various bugs in matrix graph - Extension function for directed graphs did not copy the edges - Off by one in with_capacity - Check if the lazy adjacency matrix contains node before accesing * Exact allocation in MatrixGraph::with_capacity Avoids unnecessary quadratic memory waste. Dynamic expansions continue rounding to the next power of two. * Avoid unnecesary bonds checks in update_edge Speeds up edge inserting benchmarks by ~35% * Use array swaps when possible when extending Overlapping slices still use the iterative method. * Fix bug in iterative copy, add test
teuron
pushed a commit
to teuron/petgraph
that referenced
this pull request
Oct 9, 2022
* Fix various bugs in matrix graph - Extension function for directed graphs did not copy the edges - Off by one in with_capacity - Check if the lazy adjacency matrix contains node before accesing * Exact allocation in MatrixGraph::with_capacity Avoids unnecessary quadratic memory waste. Dynamic expansions continue rounding to the next power of two. * Avoid unnecesary bonds checks in update_edge Speeds up edge inserting benchmarks by ~35% * Use array swaps when possible when extending Overlapping slices still use the iterative method. * Fix bug in iterative copy, add test
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.
I made multiple changes and fixes to
MatrixGraph
:The routine for extending the adjacency matrix for directed graphs created a new matrix and tried to copy the data doing a
mem::swap
of references to slices, which was effectively a no-op so the adjacencies were lost.I changed it to inplace by-element
slice::swap
s which surprisingly seem to be faster than what was there before. This fixes Edge disappearing, or misunderstanding matrix graph? #425.I tried a version that uses an unsafe
ptr::swap_nonoverlapping
when possible in ABorgna@877edbd and it had around 15% better performance in resize-heavy tests.Many functions didn't check the adjacency matrix capacity before looking for a node data.
This was masked in the tests due to
new
always reserving a 4x4 matrix.I renamed the new capacity parameter in the extension functions to avoid confusions (was
min_node
).I made
with_capacity
reserve the exact value passed instead of rounding to the next power of two, to avoid quadratic wastage.Benchmarks: