-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Optimise range() iteration despite type casts on the step value #2519
Comments
Should add that if cdef double mysum2(double[:] a) nogil:
cdef size_t i
cdef double r = 0
for i from <size_t>0 <= i < <size_t>len(a) by <size_t>1:
r += a[i]
return r |
This is a known issue. When using |
To be a bit clearer, dropping the |
The problem is that a) Cython has to know the step value at translation time in order to generate the loop comparisons in the right direction, and b) the exact details about We could probably special case a couple of more expressions here, but there are always limits to the assumptions that Cython can make. |
Trying Handling For signed types, would it be reasonable for Cython to generate some code that looks roughly like the following? Or is there something I'm missing? if (step > 0) {
for (i = start; i < stop; i += step) {
...
}
} else {
for (i = start; i > stop; i += step) {
...
}
} |
The problem with that example is the duplication of the And the problem with |
That's fair. Let's play with it a bit then. What about this? for (i = start; (step > 0) ? (i < stop) : (i > stop); i += step) {
...
} Hmm...thought |
That would work--it would be an extra condition on each loop but likely faster than Python (and any decent C compiler should optimize away a |
We could always turn this into an
Don't think we should do anything. The user can trivially generate the same problematic code with the old |
Cython compiles this without issues.
Which doesn't mean that it should stay that way.
|
Agreed, but that is an orthogonal issue. |
Is this the right function to be looking at? Should one be changing this? Or is there a better way to go about this? Edit: Should one be changing this block as well? |
The tree restructuring code for the Also see PR #368. |
Hello, I find a bit sad to be obliged to convert from the natural python code to |
I'm having this issue too. @jimy-byerley how does the |
It's a deprecated piece of syntax that should no longer be used in new code. It will probably be removed at some point. https://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#integer-for-loops |
It's probably not trivial to get this to work, but it first calls for a bunch of tests to see what should work and what shouldn't. |
So the best choice is to just do a |
That's what I have done so far. 😐 There was nothing else at the time |
Given the code below, all of the functions compile except for
mysum2
.The errors generated by Cython for
mysum2
are as follows.Details about the environment used below. This was run on my Mac, but expect the error to occur independently of the OS used.
The text was updated successfully, but these errors were encountered: