Skip to content

Commit

Permalink
revise boundscheck structure
Browse files Browse the repository at this point in the history
it is much easier if this value gets treated as a normal parameter
as that allows all of the normal control flow logic to apply
rather than require a complete reimplementation of it
  • Loading branch information
vtjnash committed Aug 26, 2017
1 parent 9f61878 commit ae9cc56
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 390 deletions.
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))
return a
end

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

0 comments on commit ae9cc56

Please sign in to comment.