-
-
Notifications
You must be signed in to change notification settings - Fork 482
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
Optimize DisjointSet
#37835
Optimize DisjointSet
#37835
Conversation
Documentation preview for this PR (built with commit e24a492; changes) is ready! 🎉 |
Wouldn't it make sense to simply remove the input checks? (I think not, but I cannot really substantiate my intuition. It may make sense to leave them in, because cython is essentially not debugable.) |
Yes, I would prefer to remove them altogether. However, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's some things to try and make it a bit faster.
@mantepse I only kept the version with no input checks and renamed @tscrim Thanks for the suggestions; I want to make it as fast as possible. I did implement the recommended changes and I think it is a tiny bit faster. (It is not easy to come up with good tests.) |
5592d82
to
ae06474
Compare
Thank you. Although I am -1 on renaming |
Yes, please keep union-find. |
1298483
to
fad60ad
Compare
Typecast `union` as `void` to a-void the "C struct/union cannot be declared cpdef" error.
fad60ad
to
be8144d
Compare
Ok, I managed a workaround and now |
Calling sage: e = DisjointSet(5)
sage: e.find(2**30) |
It causes a segmentation fault indeed. What should be done? |
This avoids potential SEGFAULTs from Python calls
7c91033
to
3598557
Compare
@videlec I think you will be satisfied with the last changes. |
I believe @videlec's point is that user-facing code should not segfault on bad input (of course, internal code should not either, but there we can sanitize/control the input). So I believe your changes should have addressed this. |
Yes, I think it is good now. |
Looking at it a bit more, I don't quite understand the purpose of this PR. If you want something fast(er), why don't you use directly the |
To some extent this is a fair point. However, the Given that now I call |
From `find` and `union` of `DisjointSet_of_hashables`, respectively
d6cec11
to
245fcb1
Compare
Indeed, that looks like a good optimization!
I would not bother keeping them. |
Maybe you could add a note in the docstring of |
@videlec Cool!
I removed them and referred to |
06fa3aa
to
19a4efc
Compare
Remove `_find` & `_union`. Remove `DisjointSet_of_integers` `_d` attribute from `DisjointSet_of_hashables` (and adjust code). Add notes recommending `OP_find` and `OP_union` if no input checking needed. Touch-up docstrings.
19a4efc
to
68726cf
Compare
f49a345
to
50a8549
Compare
Looks good to me. Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will take @videlec's comment to mean a positive review.
A metamorphosis of `graphic_matroid.py` into `graphic_matroid.pyx` and `graphic_matroid.pxd`. Things should be somewhat faster; test, e.g., the `GraphicMatroid`'s `_circuit` function. An indirect and diluted test that shows improvement can be seen by running `%time matroids.CompleteGraphic(8).circuits()` (~half the time). `For the reviewer:` The changes of this PR are only inside the `matroids` module. The rest come from sagemath#37835. ### ⌛ Dependencies sagemath#37835: Deeper improvements of `DisjointSet`. URL: sagemath#37839 Reported by: gmou3 Reviewer(s): gmou3, Kwankyu Lee, Travis Scrimshaw
A metamorphosis of `graphic_matroid.py` into `graphic_matroid.pyx` and `graphic_matroid.pxd`. Things should be somewhat faster; test, e.g., the `GraphicMatroid`'s `_circuit` function. An indirect and diluted test that shows improvement can be seen by running `%time matroids.CompleteGraphic(8).circuits()` (~half the time). `For the reviewer:` The changes of this PR are only inside the `matroids` module. The rest come from sagemath#37835. ### ⌛ Dependencies sagemath#37835: Deeper improvements of `DisjointSet`. URL: sagemath#37839 Reported by: gmou3 Reviewer(s): gmou3, Kwankyu Lee, Travis Scrimshaw
A metamorphosis of `graphic_matroid.py` into `graphic_matroid.pyx` and `graphic_matroid.pxd`. Things should be somewhat faster; test, e.g., the `GraphicMatroid`'s `_circuit` function. An indirect and diluted test that shows improvement can be seen by running `%time matroids.CompleteGraphic(8).circuits()` (~half the time). `For the reviewer:` The changes of this PR are only inside the `matroids` module. The rest come from sagemath#37835. ### ⌛ Dependencies sagemath#37835: Deeper improvements of `DisjointSet`. URL: sagemath#37839 Reported by: gmou3 Reviewer(s): gmou3, Kwankyu Lee, Travis Scrimshaw
The methods
find
andunion
of the classDisjointSet_of_hashables
pass through an unnecessary layer of input checks that slow down the code. I avoided these checks, cythonized more of the code, and edited the docstrings.I can attest to the fact that these changes improve speed, and in a follow-up PR (#37839) I mention a test that confirms this.