JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.9k stars 5.49k forks source link

extensible bounds checking removal #7799

Closed ivarne closed 8 years ago

ivarne commented 10 years ago

For user written short functions that have a huge overhead from bounds checking, it would be nice if it was possible to hook into the boundscheck system with @inbounds in order to let the user be able to disable manual bounds checking inside the function.

This might be beneficial for SubString, SubArray, and many wrapping types that have to check bounds before forwarding to the internal storage.

One suggestion is to have a @bounds macro that you can wrap the optional checking code in, but that seems somewhat brittle.

mbauman commented 9 years ago

Ah, whoops, I missed the "up to 1 level." I like that. I don't think it's all that arbitrary - I think that each indexing level should need to explicitly opt-in to @inbounds if they know it'll be safe.

I also like the idea of specifying which array the indexing is inbounds for, but that might be somewhat redundant with the above. I'll have to think through that a bit more.

carnaval commented 9 years ago

I don't like the idea of specifying the array : if the array gets stored into temporaries or something then it becomes harder, and if you already went to the trouble of adding @inbounds I don't think you'd like to have to fiddle with the code to get it to actually work.

simonster commented 9 years ago

I've thought about this solution before. I don't think it would matter for performance if you're not able to eliminate bounds checks from functions that aren't inlined, but here is the worst case scenario:

function getindex(x, i)
    @checkbounds begin
        ...
        i += 1 # oops, meant to put this outside the @checkbounds block
    end
    @inbounds x.x[i]
end

Now your Travis build with --inline=no --code-coverage=user might pass even though your code doesn't work. Or you might not realize there was a bug in your code until someone changes the inlining threshold and bounds checks that were previously enabled get disabled. I'm not sure if this is a show stopper for this approach, just pointing it out.

StefanKarpinski commented 9 years ago

Jeff, is the idea that you would generate both the getindex and unsafe_getindex methods from the single definition that contains @checkbounds and then on the calling side, the @inbounds macro would rewrite getindex calls to unsafe_getindex calls? If so this seems to be a special case of a macro that can rewrite the entire top-level expression that it appears in. Maybe a candidate for @@checkbounds? Or maybe you just put a macro around the method definition too.

blakejohnson commented 9 years ago

This issue has been quiet for 4.5 months... what's necessary to actually make progress on @JeffBezanson's proposal? Are the required changes buried within codegen?

mbauman commented 8 years ago

Closed by #14474. :cake: