-
-
Notifications
You must be signed in to change notification settings - Fork 12k
BUG,MAINT: Ensure ufunc override call each class only once, plus cleanup #11320
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
Conversation
numpy/core/src/umath/override.c
Outdated
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.
Would it be better to store types in an array rather than calling PyObject_Type repeatedly?
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.
OOPS, this is the wrong function, I don't need a reference to the type at all, I just need Py_TYPE, which is very fast.
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 think this is wrong elsewhere in the function too...
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.
OK, I think I got them all.
numpy/core/src/multiarray/methods.c
Outdated
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.
nit: remove a layer of parens here
numpy/core/src/multiarray/methods.c
Outdated
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.
Shouldn't be comparing with NULL - this returns a boolean
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.
duh, how could I have missed all but one of those.
numpy/core/src/multiarray/methods.c
Outdated
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.
has_override shouldn't be an npy_bool if it contains -1
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.
Indeed. Leftover. I do wonder to what extent I should even check that args is a tuple and kwds a dict; one can presumably only get here via python, so this should be OK. What do you think? remove those tests?
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.
Could write them as asserts
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.
Good idea, done in array_ufunc itself.
numpy/core/tests/test_umath.py
Outdated
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.
Test might be clearer and more reorderable if you did c.count = c_sub.count = 0 each time (or just recreated the arrays)
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.
OK, does make more sense. Done.
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.
A comment explaining the purpose of this would be nice
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.
Something like /* Like the python `__op__` and `__rop__` overloads, a type is not visited more than once */
|
First commit is good to go, only minor nits on the second. |
|
Would prefer to see the move commit separately - it makes it harder to review (reviewing per-commit results in comments being marked outdated) and rebase with it part of the same PR. |
Overall, it likely doesn't matter much for performance, but it is more logical and more consistent with what python does: reverse operators are not called if the forward one of a given class already returned NotImplemented.
Code was getting too convoluted and both can be optimized in different ways.
This way, only one simple routine is left.
Where we args is guaranteed to be a tuple, and were out is useful immediately.
d9068e6 to
a22b713
Compare
| goto fail; | ||
| } | ||
| if (with_override != NULL) { | ||
| for (j = 0; j < num_override_args; j++) { |
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.
The int j can go on the line above this - there's no need to introduce it 70 lines earlier
a22b713 to
16c39af
Compare
|
Makes sense about the review, though perhaps it is not so bad to check what is here directly... |
fa2c834 to
2ac7463
Compare
Bug since the former increases reference counts, which were not taken into account.
2ac7463 to
56b0d57
Compare
|
Replaced by #11338 - which has the commits better organized (and likely solves the failure due to a poor |
This started as a simple fix for #11306, ensuring that as we collect arguments and overrides, we skip those that have the same class as ones collected already. But looking at the code (mostly my own...), I found a reference counting bug (first commit), and also realized that for the
ndarrayside, where all that is needed is to know whether there are any overrides, the code was needlessly slow. But splitting that out, there was no need any more for much of the code to be in thesrc/privatedirectory, so I moved it. As a result, this is quite a lot of shuffling, but I think it makes the whole a lot clearer and cleaner.Left really is the annoyance of not having access to the
PyArrayAPI - thus, this should be revisited oncemultiarrayandumathhave been merged.fixes #11306