-
-
Notifications
You must be signed in to change notification settings - Fork 5.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
Avoid boundchecking iteration #15291
Conversation
This will allow us to avoid sprinkinling |
@@ -232,6 +232,11 @@ collect(itr) = collect(eltype(itr), itr) | |||
## Iteration ## | |||
start(A::Array) = 1 | |||
next(a::Array,i) = (a[i],i+1) | |||
function unsafe_next(a::Array,i) | |||
@inbounds begin | |||
return next(a,i) |
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.
Not sure if this is still the case, but @inbounds return
was difficult for inference to reason about at one point in recent history. You're probably more aware of the current design than I am, but just FYI: #13461.
would it be possible to use the new |
I think boundscheck is too coarse since if lowering insert a unsafe_next has the advantage that the contract is only about the iterator passed as an argument, which is the only thing for-loop lowering guarantees |
Add an unsafe_next method which contract is that the iteration protocol was followed correctly.
I'm open to suggestions for design, maybe unsafe_next is too painful and it apparently could use the new boundscheck thing according to Jameson. In any case it is kind of silly that we boundcheck one of the only guaranteed safe indexing construct in the language :-) |
I guess an alternative approach would be to directly inject |
Or, write next(a::Array, i) = (@boundscheck checkbounds(a, i); @inbounds r=a[i]; (r, i+1)) Though, to ellide the bounds check you still need to decorate the |
Closing, as I believe we do this now via |
Do we? I think this is about removing bounds check automatically for |
Any chance of resurrecting this PR in some form? |
Add an unsafe_next method which contract is that the iteration protocol was followed correctly. It has a generic fallback to next.
Lowering uses this instead of next. Wrapper iterators can also forward it.
This is demonstrated for Enumerate in this commit.
With this we can proudly say we compile a dead loop to nothing, yay...