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 Jul 16, 2017
1 parent 526b83e commit 377fbac
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 398 deletions.
11 changes: 6 additions & 5 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,8 @@ done(a::Array,i) = (@_inline_meta; i == length(a)+1)
## Indexing: getindex ##

# 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 @@ -612,8 +612,9 @@ function getindex(A::Array{S}, I::Range{Int}) where S
end

## Indexing: setindex! ##
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 @@ -696,7 +697,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 @@ -198,7 +198,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 @@ -207,7 +207,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)

function precompile(@nospecialize(f), args::Tuple)
ccall(:jl_compile_hint, Int32, (Any,), Tuple{Core.Typeof(f), args...}) != 0
Expand All @@ -227,18 +227,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 @@ -256,9 +254,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 @@ -379,7 +378,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 377fbac

Please sign in to comment.