julia> function myfunc(activated::AbstractArray{Bool,N}, gridsize, padsize) where {N}
ax = axes(activated)
for d = 1:N
axd = ntuple(i -> i == d ? (gridsize[d]+1:padsize[d]) : ax[i], Val{N}())
activated[CartesianIndices(axd)] .= false
end
nothing
end
julia> function myfunc_ur(activated::AbstractArray{Bool,4}, gridsize, padsize)
activated[(gridsize[1]+1:padsize[1]),:,:,:] .= false
activated[:,(gridsize[2]+1:padsize[2]),:,:] .= false
activated[:,:,(gridsize[3]+1:padsize[3]),:] .= false
activated[:,:,:,(gridsize[4]+1:padsize[4])] .= false
nothing
end
julia> function foo(activated::Array{Bool,N}, gridsize, padsize) where {N}
for k=1:N
g=gridsize[k]
p=padsize[k]
pref = ntuple(i->Colon(), k-1)
suff = ntuple(i->Colon(), N-k)
v=view(activated, pref..., g+1:p, suff...)
v .= false
end
end
julia> activated = rand(Bool, (2,2,2,2)); gridsize=[1,1,1,1]; padsize=[2,2,2,2];
julia> using BenchmarkTools
julia> @btime myfunc_ur($activated, $gridsize, $padsize)
164.119 ns (4 allocations: 320 bytes)
julia> @btime myfunc($activated, $gridsize, $padsize)
4.207 μs (36 allocations: 1.25 KiB)
julia> @btime foo($activated, $gridsize, $padsize)
5.275 μs (37 allocations: 1.31 KiB)
But the fast version needs to be @generated, which is in practice too much of a hassle. Some way of static unrolling would be pretty nice for that.