-
-
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
flatten the call chain of checkbounds
a bit
#17340
Conversation
end | ||
function checkbounds{T}(::Type{Bool}, A::Union{Array{T,1},Range{T}}, i::Integer) | ||
@_inline_meta | ||
(1 <= i) & (i <= length(A)) |
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.
should this be using first
and last
for ranges?
I am surprised that julia> a = linspace(1,5,3)
3-element LinSpace{Float64}:
1.0,3.0,5.0
julia> @code_typed checkbounds(a, 2)
1-element Array{Any,1}:
LambdaInfo for checkbounds(::LinSpace{Float64}, ::Int64)
:(begin # line 226:
# meta: location abstractarray.jl checkbounds 197
SSAValue(1) = (Core.getfield)(I,1)::Int64
# meta: location abstractarray.jl _chkbounds 200
# meta: location abstractarray.jl linearindices 67
# meta: location abstractarray.jl indices1 52
# meta: location abstractarray.jl indices 44
#30 = $(Expr(:new, :(Base.##30#31)))
SSAValue(2) = #30
# meta: location range.jl size 316
# meta: location range.jl length 336
SSAValue(4) = (Core.getfield)(A,:len)::Float64
# meta: pop location
# meta: pop location
SSAValue(6) = (Base.box)(Base.Int,(Base.checked_fptosi)(Base.Int,(Base.select_value)((Base.slt_int)((Base.box)(Int64,(Base.box)(Base.Float64,(Base.sub_float)((Core.getfield)(A,:len)::Float64,(Base.box)(Float64,(Base.sitofp)(Float64,1))))),0)::Bool,(Base.box)(Base.Float64,(Base.add_float)((Base.box)(Float64,(Base.sitofp)(Float64,1)),SSAValue(4))),SSAValue(4))::Float64))
# meta: location tuple.jl map 85
SSAValue(5) = SSAValue(6)
# meta: pop location
# meta: pop location
# meta: pop location
# meta: pop location
# meta: pop location
# meta: pop location
SSAValue(0) = (Base.box)(Base.Bool,(Base.and_int)((Base.sle_int)(1,SSAValue(1))::Bool,(Base.sle_int)(SSAValue(1),(Base.select_value)((Base.slt_int)(SSAValue(5),0)::Bool,0,SSAValue(5))::Int64)::Bool))
unless SSAValue(0) goto 27
return SSAValue(0)
27:
return $(Expr(:invoke, throw_boundserror(::LinSpace{Float64}, ::Tuple{Int64}), :(Base.throw_boundserror), :(A), :(I)))
end::Bool) which should be fine. The main reason for the indirection to That said, ambiguities are not the problem they used to be, and the API has gotten general enough that there should rarely be a need to specialize it. So in contrast to |
checkbounds
is surprisingly complicated. The call chain consists of checkbounds -> checkbounds(Bool) -> _chkbounds -> checkindex and linearindices. This is especially an issue for ranges, where the bounds check code looks like:Yikes. This change simplifies it a bit, especially for
Vector
andRange
, where the call toindices
can easily be avoided.