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

hvncat: Added inbounds annotations that improve performance #41200

Merged
merged 22 commits into from
Jul 1, 2023
Merged
Changes from 19 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
22 changes: 15 additions & 7 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@ function hvncat_fill!(A::Array, row_first::Bool, xs::Tuple)
nrc = nr * nc
na = prod(size(A)[3:end])
k = 1
for d ∈ 1:na
@inbounds for d ∈ 1:na
dd = nrc * (d - 1)
for i ∈ 1:nr
Ai = dd + i
Expand All @@ -2422,7 +2422,7 @@ function hvncat_fill!(A::Array, row_first::Bool, xs::Tuple)
end
end
else
for k ∈ eachindex(xs)
@inbounds for k ∈ eachindex(xs)
A[k] = xs[k]
Copy link
Contributor

Choose a reason for hiding this comment

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

Would

Suggested change
@inbounds for k eachindex(xs)
A[k] = xs[k]
for k eachindex(A, xs)
A[k] = xs[k]

let us avoid @inbounds and retain same performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wasn't aware of the multi-arg eachindex, but looks like no:

julia> eachindex([1, 2, 3], (2, 3, 4))
ERROR: MethodError: no method matching keys(::Vector{Int64}, ::Tuple{Int64, Int64, Int64})

end
end
Expand Down Expand Up @@ -2609,28 +2609,36 @@ function _typed_hvncat_shape(::Type{T}, shape::NTuple{N, Tuple}, row_first, as::
return A
end

function hvncat_fill!(A::AbstractArray{T, N}, scratch1::Vector{Int}, scratch2::Vector{Int}, d1::Int, d2::Int, as::Tuple{Vararg}) where {T, N}
function hvncat_fill!(A::AbstractArray{T, N}, scratch1::Vector{Int}, scratch2::Vector{Int},
d1::Int, d2::Int, as::Tuple) where {T, N}
N > 1 || throw(ArgumentError("dimensions of the destination array must be at least 2"))
length(scratch1) == length(scratch2) == N ||
throw(ArgumentError("scratch vectors must have as many elements as the destination array has dimensions"))
0 < d1 < 3 &&
BioTurboNick marked this conversation as resolved.
Show resolved Hide resolved
0 < d2 < 3 &&
d1 != d2 ||
throw(ArgumentError("d1 and d2 must be either 1 or 2, exclusive."))
outdims = size(A)
offsets = scratch1
inneroffsets = scratch2
for a ∈ as
if isa(a, AbstractArray)
for ai ∈ a
Ai = hvncat_calcindex(offsets, inneroffsets, outdims, N)
@inbounds Ai = hvncat_calcindex(offsets, inneroffsets, outdims, N)
BioTurboNick marked this conversation as resolved.
Show resolved Hide resolved
A[Ai] = ai

for j ∈ 1:N
@inbounds for j ∈ 1:N
inneroffsets[j] += 1
inneroffsets[j] < cat_size(a, j) && break
inneroffsets[j] = 0
end
end
else
Ai = hvncat_calcindex(offsets, inneroffsets, outdims, N)
@inbounds Ai = hvncat_calcindex(offsets, inneroffsets, outdims, N)
A[Ai] = a
end

for j ∈ (d1, d2, 3:N...)
@inbounds for j ∈ (d1, d2, 3:N...)
offsets[j] += cat_size(a, j)
offsets[j] < outdims[j] && break
offsets[j] = 0
Expand Down