Skip to content

Commit

Permalink
Add inbounds annotation in map! computation (#30624)
Browse files Browse the repository at this point in the history
* Add inbounds annotation in `map!`

* address code review comments
  • Loading branch information
YingboMa authored and KristofferC committed Jan 25, 2019
1 parent cb7a569 commit f9645ff
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,8 @@ concatenate_setindex!(R, X::AbstractArray, I...) = (R[I...] = X)

function map!(f::F, dest::AbstractArray, A::AbstractArray) where F
for (i,j) in zip(eachindex(dest),eachindex(A))
dest[i] = f(A[j])
val = f(@inbounds A[j])
@inbounds dest[i] = val
end
return dest
end
Expand Down Expand Up @@ -2051,19 +2052,28 @@ map(f, ::AbstractSet) = error("map is not defined on sets")
## 2 argument
function map!(f::F, dest::AbstractArray, A::AbstractArray, B::AbstractArray) where F
for (i, j, k) in zip(eachindex(dest), eachindex(A), eachindex(B))
dest[i] = f(A[j], B[k])
@inbounds a, b = A[j], B[k]
val = f(a, b)
@inbounds dest[i] = val
end
return dest
end

## N argument

@inline ith_all(i, ::Tuple{}) = ()
@inline ith_all(i, as) = (as[1][i], ith_all(i, tail(as))...)
function ith_all(i, as)
@_propagate_inbounds_meta
return (as[1][i], ith_all(i, tail(as))...)
end

function map_n!(f::F, dest::AbstractArray, As) where F
for i = LinearIndices(As[1])
dest[i] = f(ith_all(i, As)...)
idxs1 = LinearIndices(As[1])
@boundscheck LinearIndices(dest) == idxs1 && all(x -> LinearIndices(x) == idxs1, As)
for i = idxs1
@inbounds I = ith_all(i, As)
val = f(I...)
@inbounds dest[i] = val
end
return dest
end
Expand Down

0 comments on commit f9645ff

Please sign in to comment.