Skip to content
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

RFC: make boundscheck a value #22826

Merged
merged 3 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,8 @@ julia> getindex(A, "a")
function getindex end

# This is more complicated than it needs to be in order to get Win64 through bootstrap
getindex(A::Array, i1::Int) = arrayref(A, i1)
getindex(A::Array, i1::Int, i2::Int, I::Int...) = (@_inline_meta; arrayref(A, i1, i2, I...)) # TODO: REMOVE FOR #14770
@eval getindex(A::Array, i1::Int) = arrayref($(Expr(:boundscheck)), A, i1)
@eval getindex(A::Array, i1::Int, i2::Int, I::Int...) = (@_inline_meta; arrayref($(Expr(:boundscheck)), A, i1, i2, I...)) # TODO: REMOVE FOR #14770

# Faster contiguous indexing using copy! for UnitRange and Colon
function getindex(A::Array, I::UnitRange{Int})
Expand Down Expand Up @@ -798,8 +798,9 @@ x` is converted by the compiler to `(setindex!(a, x, i, j, ...); x)`.
"""
function setindex! end

setindex!(A::Array{T}, x, i1::Int) where {T} = arrayset(A, convert(T,x)::T, i1)
setindex!(A::Array{T}, x, i1::Int, i2::Int, I::Int...) where {T} = (@_inline_meta; arrayset(A, convert(T,x)::T, i1, i2, I...)) # TODO: REMOVE FOR #14770
@eval setindex!(A::Array{T}, x, i1::Int) where {T} = arrayset($(Expr(:boundscheck)), A, convert(T,x)::T, i1)
@eval setindex!(A::Array{T}, x, i1::Int, i2::Int, I::Int...) where {T} =
(@_inline_meta; arrayset($(Expr(:boundscheck)), A, convert(T,x)::T, i1, i2, I...)) # TODO: REMOVE FOR #14770

# These are redundant with the abstract fallbacks but needed for bootstrap
function setindex!(A::Array, x, I::AbstractVector{Int})
Expand Down Expand Up @@ -905,7 +906,7 @@ end

function push!(a::Array{Any,1}, @nospecialize item)
_growend!(a, 1)
arrayset(a, item, length(a))
arrayset(true, a, item, length(a))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what exactly arrayset(true, ...) does, but isn't it safe to skip the bounds check here? The only case I can see is when _growend!() would fail to grow an empty array.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, grow can also fail due to finalizers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bound check doesn't really help catching that though....

return a
end

Expand Down
2 changes: 1 addition & 1 deletion base/codevalidation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const VALID_EXPR_HEADS = ObjectIdDict(
:enter => 1:1,
:leave => 1:1,
:inbounds => 1:1,
:boundscheck => 1:1,
:boundscheck => 0:0,
:copyast => 1:1,
:meta => 0:typemax(Int),
:global => 1:1,
Expand Down
21 changes: 10 additions & 11 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ function append_any(xs...)
ccall(:jl_array_grow_end, Void, (Any, UInt), out, 16)
l += 16
end
Core.arrayset(out, y, i)
Core.arrayset(true, out, y, i)
i += 1
end
end
Expand All @@ -388,7 +388,7 @@ function append_any(xs...)
end

# simple Array{Any} operations needed for bootstrap
setindex!(A::Array{Any}, @nospecialize(x), i::Int) = Core.arrayset(A, x, i)
@eval setindex!(A::Array{Any}, @nospecialize(x), i::Int) = Core.arrayset($(Expr(:boundscheck)), A, x, i)

"""
precompile(f, args::Tuple{Vararg{Any}})
Expand All @@ -413,18 +413,16 @@ section of the Metaprogramming chapter of the manual for more details and exampl
esc(@nospecialize(e)) = Expr(:escape, e)

macro boundscheck(blk)
# hack: use this syntax since it avoids introducing line numbers
:($(Expr(:boundscheck,true));
$(esc(blk));
$(Expr(:boundscheck,:pop)))
return Expr(:if, Expr(:boundscheck), esc(blk))
end

"""
@inbounds(blk)

Eliminates array bounds checking within expressions.

In the example below the bound check of array A is skipped to improve performance.
In the example below the in-range check for referencing
element `i` of array `A` is skipped to improve performance.

```julia
function sum(A::AbstractArray)
Expand All @@ -442,9 +440,10 @@ end
for out-of-bounds indices. The user is responsible for checking it manually.
"""
macro inbounds(blk)
:($(Expr(:inbounds,true));
$(esc(blk));
$(Expr(:inbounds,:pop)))
return Expr(:block,
Expr(:inbounds, true),
esc(blk),
Expr(:inbounds, :pop))
end

macro label(name::Symbol)
Expand Down Expand Up @@ -565,7 +564,7 @@ function vector_any(@nospecialize xs...)
n = length(xs)
a = Vector{Any}(n)
@inbounds for i = 1:n
Core.arrayset(a,xs[i],i)
Core.arrayset(false, a, xs[i], i)
end
a
end
Expand Down
Loading